1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpDDD\DomainDrivenDesign\Command; |
4
|
|
|
|
5
|
|
|
use PhpDDD\DomainDrivenDesign\Domain\AbstractAggregateRoot; |
6
|
|
|
use PhpDDD\DomainDrivenDesign\User\Author; |
7
|
|
|
use PhpDDD\Utils\PopulatePropertiesTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Base class for your Command. |
11
|
|
|
* The only pre-defined property is the author because we assume that a command will always be triggered |
12
|
|
|
* by somebody or something. If not set, we'll use the "robot" author. |
13
|
|
|
* |
14
|
|
|
* A command is a Plain Old PHP Object. There should be no logic and no business rules. |
15
|
|
|
* |
16
|
|
|
* Every properties should be public because data should come from your own application and you should not |
17
|
|
|
* rely on user data without checking their input. |
18
|
|
|
* It will be the responsibility of the Aggregate to validate the data coming from the Command. |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
abstract class AbstractCommand |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
use PopulatePropertiesTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List of aggregate root impacted by this command. |
26
|
|
|
* This should be set by the CommandHandler when it get all the aggregate roots according to the command's data. |
27
|
|
|
* You'll probably manage only one aggregate root per command. It's an array just in case you need more. |
28
|
|
|
* |
29
|
|
|
* See CommandHandler for more details on the purpose of this. |
30
|
|
|
* |
31
|
|
|
* @var AbstractAggregateRoot[] |
32
|
|
|
*/ |
33
|
|
|
public $aggregateRoots = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The command requester. |
37
|
|
|
* We use the same object type (Author) as for an event in order to keep the track of the Command Requester |
38
|
|
|
* in the Event ($author). |
39
|
|
|
* |
40
|
|
|
* @var Author |
41
|
|
|
*/ |
42
|
|
|
public $requester; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $data An array ['propertyName' => 'value', ...] |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $data = []) |
48
|
|
|
{ |
49
|
|
|
$data['requester'] = isset($data['requester']) ? $data['requester'] : Author::robot(); |
50
|
|
|
$this->populate($data); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param AbstractAggregateRoot $abstractAggregateRoot |
55
|
|
|
*/ |
56
|
|
|
public function addAggregateRoot(AbstractAggregateRoot $abstractAggregateRoot) |
57
|
|
|
{ |
58
|
|
|
$this->aggregateRoots[] = $abstractAggregateRoot; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.