1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2013 Janos Szurovecz |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
7
|
|
|
* the Software without restriction, including without limitation the rights to |
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
10
|
|
|
* so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
13
|
|
|
* copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
* SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace predaddy\domain; |
25
|
|
|
|
26
|
|
|
use predaddy\messagehandling\AbstractMessage; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Base class for all Domain Events. |
30
|
|
|
* This class contains the basic behavior expected from any event |
31
|
|
|
* to be processed by event sourcing engines and aggregates. |
32
|
|
|
* |
33
|
|
|
* @author Janos Szurovecz <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractDomainEvent extends AbstractMessage implements DomainEvent |
36
|
|
|
{ |
37
|
|
|
use StateHashTrait; |
38
|
|
|
|
39
|
|
|
protected $aggregateClass; |
40
|
|
|
protected $aggregateValue; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param AbstractDomainEvent $event |
44
|
|
|
* @param AggregateId $aggregateId |
45
|
|
|
* @param $stateHash |
46
|
|
|
* @return AbstractDomainEvent |
47
|
|
|
*/ |
48
|
|
|
public static function initEvent(AbstractDomainEvent $event, AggregateId $aggregateId, $stateHash) |
49
|
|
|
{ |
50
|
|
|
$event->setAggregateId($aggregateId); |
51
|
|
|
$event->stateHash = $stateHash; |
52
|
|
|
return $event; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param AggregateId|null $aggregateId |
57
|
|
|
*/ |
58
|
|
|
public function __construct(AggregateId $aggregateId = null) |
59
|
|
|
{ |
60
|
|
|
parent::__construct(); |
61
|
|
|
if ($aggregateId === null) { |
62
|
|
|
$aggregateId = NullAggregateId::instance(); |
63
|
|
|
} |
64
|
|
|
$this->setAggregateId($aggregateId); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return GenericAggregateId |
69
|
|
|
*/ |
70
|
|
|
public function aggregateId() |
71
|
|
|
{ |
72
|
|
|
return new GenericAggregateId($this->aggregateValue, $this->aggregateClass); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function toStringHelper() |
76
|
|
|
{ |
77
|
|
|
return parent::toStringHelper() |
78
|
|
|
->add($this->aggregateId()) |
79
|
|
|
->add('stateHash', $this->stateHash()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param AggregateId $aggregateId |
84
|
|
|
*/ |
85
|
|
|
private function setAggregateId(AggregateId $aggregateId) |
86
|
|
|
{ |
87
|
|
|
$this->aggregateClass = $aggregateId->aggregateClass(); |
88
|
|
|
$this->aggregateValue = $aggregateId->value(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|