Completed
Pull Request — master (#23)
by Hilari
16:18 queued 10:34
created

DomainEvent::getID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Cmp\Queues\Domain\Event;
3
4
use Cmp\Queues\Domain\Event\Exception\DomainEventException;
5
use Cmp\Queues\Domain\Queue\Message;
6
7
class DomainEvent implements Message
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $origin;
13
14
    /**
15
     * @var string
16
     */
17
    protected $name;
18
19
    /**
20
     * @var string
21
     */
22
    protected $version;
23
24
    /**
25
     * @var int
26
     */
27
    protected $occurredOn;
28
29
    /**
30
     * @var array
31
     */
32
    protected $body = array();
33
34
    /**
35
     * @var string
36
     */
37
    private $id;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $isDeprecated = false;
43
44
    /**
45
     * @var string|null
46
     */
47
    protected $correlationId;
48
49
    /**
50
     * @param string      $origin
51
     * @param string      $name
52
     * @param string      $version
53
     * @param int         $occurredOn
54
     * @param array       $body
55
     * @param string      $id
56
     * @param bool        $isDeprecated
57
     * @param string|null $correlationId
58
     */
59
    public function __construct(
60
        $origin,
61
        $name,
62
        $version,
63
        $occurredOn,
64
        array $body = [],
65
        $id = null,
66
        $isDeprecated = false,
67
        $correlationId = null
68
    ) {
69
        $this->setOrigin($origin)
70
            ->setName($name)
71
            ->setVersion($version)
72
            ->setOccurredOn($occurredOn);
73
74
        $this->body          = $body;
75
        $this->id            = $id;
76
        $this->isDeprecated  = $isDeprecated;
77
        $this->correlationId = $correlationId;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getOrigin()
84
    {
85
        return $this->origin;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getName()
92
    {
93
        return $this->name;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getVersion()
100
    {
101
        return $this->version;
102
    }
103
104
    /**
105
     * Timestamp
106
     *
107
     * @return int
108
     */
109
    public function getOccurredOn()
110
    {
111
        return $this->occurredOn;
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function getBody()
118
    {
119
        return $this->body;
120
    }
121
122
    /**
123
     * @return int
124
     */
125
    public function getDelay()
126
    {
127
        return 0;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getID()
134
    {
135
        return $this->id;
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function isDeprecated()
142
    {
143
        return $this->isDeprecated;
144
    }
145
146
    /**
147
     * @return string|null
148
     */
149
    public function getCorrelationID()
150
    {
151
        return $this->correlationId;
152
    }
153
154
    /**
155
     * @param string $key
156
     * @param mixed  $default
157
     *
158
     * @return mixed
159
     */
160
    public function getBodyValue($key, $default = null)
161
    {
162
        if (!array_key_exists($key, $this->body)) {
163
            return $default;
164
        }
165
166
        return $this->body[$key];
167
    }
168
169
    /**
170
     * @param string $key
171
     *
172
     * @return mixed
173
     *
174
     * @throws \RuntimeException
175
     */
176
    public function getBodyValueOrFail($key)
177
    {
178
        if (!array_key_exists($key, $this->body)) {
179
            throw new \RuntimeException("No value in the body found for Key: $key");
180
        }
181
182
        return $this->body[$key];
183
    }
184
185
    /**
186
     * @param string $origin
187
     * @return DomainEvent $this
188
     * @throws DomainEventException
189
     */
190
    protected function setOrigin($origin)
191
    {
192
        if(empty($origin)) {
193
            throw new DomainEventException('DomainEvent origin cannot be empty');
194
        }
195
        $this->origin = $origin;
196
        return $this;
197
    }
198
199
    /**
200
     * @param string $name
201
     * @return DomainEvent $this
202
     * @throws DomainEventException
203
     */
204
    protected function setName($name)
205
    {
206
        if(empty($name)) {
207
            throw new DomainEventException('DomainEvent name cannot be empty');
208
        }
209
        $this->name = $name;
210
        return $this;
211
    }
212
213
    /**
214
     * @param string $version
215
     * @return DomainEvent $this
216
     * @throws DomainEventException
217
     */
218
    protected function setVersion($version)
219
    {
220
        if(empty($version)) {
221
            throw new DomainEventException('DomainEvent version cannot be empty');
222
        }
223
        $this->version = $version;
224
        return $this;
225
    }
226
227
    /**
228
     * @param int $occurredOn
229
     * @return DomainEvent $this
230
     * @throws DomainEventException
231
     */
232
    protected function setOccurredOn($occurredOn)
233
    {
234
        if(!is_null($occurredOn) && !preg_match('/^\d+(\.\d{1,4})?$/', $occurredOn)) { // accepts also microseconds
235
            throw new DomainEventException("$occurredOn is not a valid unix timestamp.");
236
        }
237
238
        if ($occurredOn > time()) {
239
            throw new DomainEventException('OccuredOn cannot be located in the future');
240
        }
241
242
        $this->occurredOn = $occurredOn;
243
        return $this;
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function jsonSerialize()
250
    {
251
        return [
252
            'origin'        => $this->origin,
253
            'name'          => $this->name,
254
            'version'       => $this->version,
255
            'occurredOn'    => $this->occurredOn,
256
            'body'          => $this->body,
257
            'id'            => $this->id,
258
            'isDeprecated'  => $this->isDeprecated,
259
            'correlationId' => $this->correlationId,
260
        ];
261
    }
262
}