Completed
Pull Request — master (#29)
by Luna
01:26
created

DomainEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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