I tried to create a rather complex domain called Vacancy
, here is the details:
Vacancy.php
class Vacancy extends AbstractEntity implements AggregateRootInterface {
private $employer;
private $title;
private $employerInformation;
private $position;
private $category;
private $employmentInformation;
private $hideSalary = false;
private $requiredPerson = 1;
private $yearsExp = 0;
private $location;
private $benefits;
private $qualification;
private $details;
private $postedOn;
private $lastPostPackage;
private $expiredOn;
private $visible = true;
public function __construct(
PackageOnHand $package,
$title,
Position $position,
JobCategory $category,
$employerInformation,
EmploymentInformation $employmentInformation,
City $location,
$qualification
) {
parent::__construct();
$this->employer = $package->getEmployer();
$this->title = $title;
$this->position = $position;
$this->category = $category;
$this->employerInformation = $employerInformation;
$this->employmentInformation = $employmentInformation;
$this->location = $location;
$this->setQualification($qualification);
$this->benefits = new ArrayCollection();
$this->details = new ArrayCollection();
}
// Bunch of setters, getters and methods I don't even want to mention
}
The EmploymentInformation
class is already encapsulates 4 required parameters, which makes it an entity (and I personally feels wrong about it).
As can be seen, this particular domain model's constructor is extended to 8 parameters, not to mention the possibility to add some more.
The problem is, all of those parameters does required to make a single Vacancy
into it's valid state.
I've read about Builder Pattern in this SO question, but it suggested that the Builder class is made into a nested class inside, which can't be done in PHP.
Is there any other pattern I can use to clear up constructor bloat?