Completed
Pull Request — master (#26)
by
unknown
01:55
created

DomainEvent::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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