Completed
Pull Request — master (#23)
by Hilari
13:04
created

DomainEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 8

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
     * @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
    public function getBodyValueOrFail($key)
175
    {
176
        if (!array_key_exists($key, $this->body)) {
177
            throw new \RuntimeException("No value in the body found for Key: $key");
178
        }
179
180
        return $this->body[$key];
181
    }
182
183
    /**
184
     * @param string $origin
185
     * @return DomainEvent $this
186
     * @throws DomainEventException
187
     */
188
    protected function setOrigin($origin)
189
    {
190
        if(empty($origin)) {
191
            throw new DomainEventException('DomainEvent origin cannot be empty');
192
        }
193
        $this->origin = $origin;
194
        return $this;
195
    }
196
197
    /**
198
     * @param string $name
199
     * @return DomainEvent $this
200
     * @throws DomainEventException
201
     */
202
    protected function setName($name)
203
    {
204
        if(empty($name)) {
205
            throw new DomainEventException('DomainEvent name cannot be empty');
206
        }
207
        $this->name = $name;
208
        return $this;
209
    }
210
211
    /**
212
     * @param string $version
213
     * @return DomainEvent $this
214
     * @throws DomainEventException
215
     */
216
    protected function setVersion($version)
217
    {
218
        if(empty($version)) {
219
            throw new DomainEventException('DomainEvent version cannot be empty');
220
        }
221
        $this->version = $version;
222
        return $this;
223
    }
224
225
    /**
226
     * @param int $occurredOn
227
     * @return DomainEvent $this
228
     * @throws DomainEventException
229
     */
230
    protected function setOccurredOn($occurredOn)
231
    {
232
        if(!is_null($occurredOn) && !preg_match('/^\d+(\.\d{1,4})?$/', $occurredOn)) { // accepts also microseconds
233
            throw new DomainEventException("$occurredOn is not a valid unix timestamp.");
234
        }
235
236
        if ($occurredOn > time()) {
237
            throw new DomainEventException('OccuredOn cannot be located in the future');
238
        }
239
240
        $this->occurredOn = $occurredOn;
241
        return $this;
242
    }
243
244
    /**
245
     * @return array
246
     */
247
    public function jsonSerialize()
248
    {
249
        return [
250
            'origin'        => $this->origin,
251
            'name'          => $this->name,
252
            'version'       => $this->version,
253
            'occurredOn'    => $this->occurredOn,
254
            'body'          => $this->body,
255
            'id'            => $this->id,
256
            'isDeprecated'  => $this->isDeprecated,
257
            'correlationId' => $this->correlationId,
258
        ];
259
    }
260
}