How to get an immutable entity from its aggregate in DDD

Viewed 921

An aggregate (Article) has an entity (SmsContent) with a property (enabled) that only can change if a condition on the aggregate is met.

e.g.

<?php
//Aggregate
class Article {

    /** @var User */
    protected $user;

    /** @var SmsOutput */
    protected sms;

    ...

    public function enableSms() {
        if($this->user->hasPermission('sms')) {
            throw new PermissionDeniedException('sms');
        }
        $this->sms->enable();
        retutn $this;
    }

    public function getSms() {
        return $this->sms;
    }

    ...
}

//Entity
class SmsOutput {

    /** @var boolean */
    protected enabled = false;

    ...

    public function enable() {
        $this->enable = true;
    }

    ...
}

How should you get the SmsContent entity from the Article without being able to change the enabled property from outside the aggregate?

For example:

$article->getSms()->enable();

How is this handled in DDD?

2 Answers
Related