1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* The software is based on the Axon Framework project which is |
17
|
|
|
* licensed under the Apache 2.0 license. For more information on the Axon Framework |
18
|
|
|
* see <http://www.axonframework.org/>. |
19
|
|
|
* |
20
|
|
|
* This software consists of voluntary contributions made by many individuals |
21
|
|
|
* and is licensed under the MIT license. For more information, see |
22
|
|
|
* <http://www.governor-framework.org/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Governor\Framework\Domain; |
26
|
|
|
|
27
|
|
|
use Ramsey\Uuid\Uuid; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Basic implementation of the @see MessageInterface. |
31
|
|
|
* |
32
|
|
|
* @author "David Kalosi" <[email protected]> |
33
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
34
|
|
|
*/ |
35
|
|
|
class GenericMessage implements MessageInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $id; |
41
|
|
|
/** |
42
|
|
|
* @var MetaData |
43
|
|
|
*/ |
44
|
|
|
private $metadata; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var mixed |
48
|
|
|
*/ |
49
|
|
|
private $payload; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $payload |
53
|
|
|
* @param MetaData $metadata |
54
|
|
|
* @param string $id |
55
|
|
|
*/ |
56
|
109 |
|
public function __construct($payload, MetaData $metadata = null, $id = null) |
57
|
|
|
{ |
58
|
109 |
|
if (!is_object($payload)) { |
59
|
|
|
throw new \InvalidArgumentException("Payload needs to be an object."); |
60
|
|
|
} |
61
|
|
|
|
62
|
109 |
|
$this->id = isset($id) ? $id : Uuid::uuid1()->toString(); |
63
|
109 |
|
$this->metadata = isset($metadata) ? $metadata : MetaData::emptyInstance(); |
64
|
109 |
|
$this->payload = $payload; |
65
|
109 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
43 |
|
public function getIdentifier() |
71
|
|
|
{ |
72
|
43 |
|
return $this->id; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* |
77
|
|
|
* @return \Governor\Framework\Domain\MetaData |
78
|
|
|
*/ |
79
|
40 |
|
public function getMetaData() |
80
|
|
|
{ |
81
|
40 |
|
return $this->metadata; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
61 |
|
public function getPayload() |
88
|
|
|
{ |
89
|
61 |
|
return $this->payload; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
64 |
|
public function getPayloadType() |
96
|
|
|
{ |
97
|
64 |
|
return get_class($this->payload); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $metadata |
102
|
|
|
* @return GenericMessage |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function andMetaData(array $metadata = []) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if (empty($metadata)) { |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return new GenericMessage( |
111
|
|
|
$this->getPayload(), |
112
|
|
|
$this->getMetaData()->mergeWith($metadata), |
113
|
|
|
$this->getIdentifier() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $metadata |
119
|
|
|
* @return GenericMessage |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function withMetaData(array $metadata = []) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
if ($this->getMetaData()->isEqualTo($metadata)) { |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return new GenericMessage($this->getPayload(), new MetaData($metadata), $this->getIdentifier()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
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.