1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cubiche\Domain\EventSourcing\EventStore; |
12
|
|
|
|
13
|
|
|
use Cubiche\Domain\EventSourcing\DomainEventInterface; |
14
|
|
|
use Cubiche\Domain\Model\IdInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* EventStream class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class EventStream |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $streamName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var IdInterface |
30
|
|
|
*/ |
31
|
|
|
protected $aggregateId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DomainEventInterface[] |
35
|
|
|
*/ |
36
|
|
|
protected $events = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* EntityDomainEvent constructor. |
40
|
|
|
* |
41
|
|
|
* @param string $streamName |
42
|
|
|
* @param IdInterface $aggregateId |
43
|
|
|
* @param DomainEventInterface[] $events |
44
|
|
|
*/ |
45
|
|
|
public function __construct($streamName, IdInterface $aggregateId, array $events) |
46
|
|
|
{ |
47
|
|
|
$this->streamName = $streamName; |
48
|
|
|
$this->aggregateId = $aggregateId; |
49
|
|
|
|
50
|
|
|
foreach ($events as $event) { |
51
|
|
View Code Duplication |
if (!$event instanceof DomainEventInterface) { |
|
|
|
|
52
|
|
|
throw new \InvalidArgumentException( |
53
|
|
|
sprintf( |
54
|
|
|
'The event must be an instance of %s. Instance of %s given', |
55
|
|
|
DomainEventInterface::class, |
56
|
|
|
is_object($event) ? get_class($event) : gettype($event) |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->events[] = $event; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function streamName() |
69
|
|
|
{ |
70
|
|
|
return $this->streamName; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return IdInterface |
75
|
|
|
*/ |
76
|
|
|
public function aggregateId() |
77
|
|
|
{ |
78
|
|
|
return $this->aggregateId; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return DomainEventInterface[] |
83
|
|
|
*/ |
84
|
|
|
public function events() |
85
|
|
|
{ |
86
|
|
|
return $this->events; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.