jeudi 26 juillet 2018

Make sure that functions are called

Im working on a project where we're creating many objects throughout the code base.

For some objects thus we decided to use factories to control the process of creating the objects and all their dependencies. This is an example of what we are trying to do:

class CreateBranchFactory implements CreateBranchInterface {

    private $branch;

    public function __construct() {
        $this->branch = new Branch();
        $this->branch->scenario = 'create';
    }

    public function createBranch($branchForm) {
        $this->branch->attributes = $branchForm->attributes;

        //Example of all the things that I need to do after creating my object
        $this->setOfficialNameAndPrinterName($branchForm->official_name, $branchForm->printer_name, $branchForm->name);
        $this->createDependencies1();
        $this->someOtherFunction();

        return $this->branch;
    }

    public function setOfficialNameAndPrinterName($offName, $printerName, $branchName) {
        $this->branch->official_name = $offName ?? $branchName;
        $this->branch->printer_name = $printerName ?? $branchName;
        $this->branch->save();
    }

    public function createDependencies1() {

    }

And to have a proper contract I created an interface for this. This interface specifies the functions that should be defined

interface CreateBranchInterface {

    public function setOfficialNameAndPrinterName(String $offName, String $printerName, String $branchName);

    public function createDependencies1();
}

My problem though, is that the contract is specifying all the functions that should be defined, but isnt controlling which functions should get called. Is there any design pattern that I can use, that makes sure that those functions get called??

Aucun commentaire:

Enregistrer un commentaire