Suppose we have Promotion as an Aggregate Root and Rules to satisfy a promotion as an Aggregate. Rules are a collection of Different Rule Elements that extends Abstract class Rule.
As I learned I can use factory method, example:
class Promotion (
PromotionIdentity $identity,
string $name,
){
$this->identity = $identity;
$this->name = $name;
$this->rules = new RuleCollection();
}
public function addRule(
RuleIdentity $ruleIdentity,
RuleType $ruleType,
array $configuration
) {
if (RuleType::EMAIL_LIST === $ruleType->value()) {
$makeRule = new EmailListRule($ruleIdentity, $configuration);
$this->rules->add($makeRule);
}
if (RuleType::MIN_ARTICLES === $ruleType->value()) {
$makeRule = new MinimumArticlesRule($ruleIdentity, $configuration);
$this->rules->add($makeRule);
}
... and so on, for example 15 rules
}
I think that this can grow a lot, I see a code smell here.
It's ok to keep this logic of rule creation inside aggregate root?, can we move this responsability of rule creation to the application service within a factory and pass the builded Rule to the addRule Method? Other Options? Thanks Friends!