1
|
|
|
<?php namespace SmoothPhp\EventSourcing; |
2
|
|
|
|
3
|
|
|
use SmoothPhp\Contracts\EventSourcing\Event; |
4
|
|
|
use SmoothPhp\Contracts\EventSourcing\AggregateRoot as AggregateRootContract; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Abstract class Entity |
8
|
|
|
* |
9
|
|
|
* @author jrdn hannah <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
abstract class Entity implements \SmoothPhp\Contracts\EventSourcing\Entity |
12
|
|
|
{ |
13
|
|
|
/** @var AggregateRoot */ |
14
|
|
|
private $aggregateRoot; |
15
|
|
|
|
16
|
|
|
/** @var \SmoothPhp\Contracts\EventSourcing\Entity[] */ |
17
|
|
|
private $children = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Event $event |
21
|
|
|
* @throws Exception\AggregateRootAlreadyRegistered |
22
|
|
|
*/ |
23
|
6 |
|
public function handleRecursively(Event $event) |
24
|
|
|
{ |
25
|
6 |
|
$this->handle($event); |
26
|
|
|
|
27
|
6 |
|
foreach ($this->children as $entity) { |
28
|
3 |
|
$entity->registerAggregateRoot($this->aggregateRoot); |
29
|
3 |
|
$entity->handleRecursively($event); |
30
|
6 |
|
} |
31
|
6 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param AggregateRootContract $aggregateRoot |
35
|
|
|
* @throws Exception\AggregateRootAlreadyRegistered |
36
|
|
|
*/ |
37
|
9 |
|
public function registerAggregateRoot(AggregateRootContract $aggregateRoot) |
38
|
|
|
{ |
39
|
9 |
|
if (null !== $this->aggregateRoot && $this->aggregateRoot !== $aggregateRoot) { |
40
|
3 |
|
throw new Exception\AggregateRootAlreadyRegistered; |
41
|
|
|
} |
42
|
|
|
|
43
|
9 |
|
$this->aggregateRoot = $aggregateRoot; |
|
|
|
|
44
|
9 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \SmoothPhp\Contracts\EventSourcing\Entity $entity |
48
|
|
|
*/ |
49
|
3 |
|
public function addChildEntity(\SmoothPhp\Contracts\EventSourcing\Entity $entity) |
50
|
|
|
{ |
51
|
3 |
|
$this->children[] = $entity; |
52
|
3 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Event $event |
56
|
|
|
* @throws Exception\NoAggregateRootRegistered |
57
|
|
|
*/ |
58
|
3 |
|
protected function apply(Event $event) |
59
|
|
|
{ |
60
|
3 |
|
if (!$this->aggregateRoot) { |
61
|
|
|
throw new Exception\NoAggregateRootRegistered; |
62
|
|
|
} |
63
|
3 |
|
$this->aggregateRoot->apply($event); |
64
|
3 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Handles event if capable. |
68
|
|
|
* |
69
|
|
|
* @param $event |
70
|
|
|
*/ |
71
|
6 |
View Code Duplication |
protected function handle(Event $event) |
|
|
|
|
72
|
|
|
{ |
73
|
6 |
|
$method = $this->getApplyMethod($event); |
74
|
6 |
|
if (!method_exists($this, $method)) { |
75
|
6 |
|
return; |
76
|
|
|
} |
77
|
|
|
$this->$method($event); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Event $event |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
6 |
|
private function getApplyMethod(Event $event) |
85
|
|
|
{ |
86
|
6 |
|
$classParts = explode('\\', get_class($event)); |
87
|
|
|
|
88
|
6 |
|
return 'apply' . end($classParts); |
89
|
|
|
} |
90
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.