Completed
Pull Request — master (#17)
by Hilari
04:52
created

DomainEvent::setVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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
     * @param string $origin
46
     * @param string $name
47
     * @param string $version
48
     * @param int    $occurredOn
49
     * @param array  $body
50
     * @param string $id
51
     * @param bool   $isDeprecated
52
     */
53
    public function __construct($origin, $name, $version, $occurredOn, array $body = [], $id = null, $isDeprecated = false)
54
    {
55
        $this->setOrigin($origin)
56
             ->setName($name)
57
             ->setVersion($version)
58
             ->setOccurredOn($occurredOn)
59
        ;
60
61
        $this->body         = $body;
62
        $this->id           = $id;
63
        $this->isDeprecated = $isDeprecated;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getOrigin()
70
    {
71
        return $this->origin;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getName()
78
    {
79
        return $this->name;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getVersion()
86
    {
87
        return $this->version;
88
    }
89
90
    /**
91
     * Timestamp
92
     *
93
     * @return int
94
     */
95
    public function getOccurredOn()
96
    {
97
        return $this->occurredOn;
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getBody()
104
    {
105
        return $this->body;
106
    }
107
108
    /**
109
     * @return int
110
     */
111
    public function getDelay()
112
    {
113
        return 0;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getID()
120
    {
121
        return $this->uuid;
0 ignored issues
show
Bug introduced by
The property uuid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function isDeprecated()
128
    {
129
        return $this->isDeprecated;
130
    }
131
132
    /**
133
     * @param string $origin
134
     * @return DomainEvent $this
135
     * @throws DomainEventException
136
     */
137
    protected function setOrigin($origin)
138
    {
139
        if(empty($origin)) {
140
            throw new DomainEventException('DomainEvent origin cannot be empty');
141
        }
142
        $this->origin = $origin;
143
        return $this;
144
    }
145
146
    /**
147
     * @param string $name
148
     * @return DomainEvent $this
149
     * @throws DomainEventException
150
     */
151
    protected function setName($name)
152
    {
153
        if(empty($name)) {
154
            throw new DomainEventException('DomainEvent name cannot be empty');
155
        }
156
        $this->name = $name;
157
        return $this;
158
    }
159
160
    /**
161
     * @param string $version
162
     * @return DomainEvent $this
163
     * @throws DomainEventException
164
     */
165
    protected function setVersion($version)
166
    {
167
        if(empty($version)) {
168
            throw new DomainEventException('DomainEvent version cannot be empty');
169
        }
170
        $this->version = $version;
171
        return $this;
172
    }
173
174
    /**
175
     * @param int $occurredOn
176
     * @return DomainEvent $this
177
     * @throws DomainEventException
178
     */
179
    protected function setOccurredOn($occurredOn)
180
    {
181
        if(!is_null($occurredOn) && !preg_match('/^\d+(\.\d{1,4})?$/', $occurredOn)) { // accepts also microseconds
182
            throw new DomainEventException("$occurredOn is not a valid unix timestamp.");
183
        }
184
185
        if ($occurredOn > time()) {
186
            throw new DomainEventException('OccuredOn cannot be located in the future');
187
        }
188
189
        $this->occurredOn = $occurredOn;
190
        return $this;
191
    }
192
193
    /**
194
     * @return array
195
     */
196
    public function jsonSerialize()
197
    {
198
        return [
199
            'origin'       => $this->origin,
200
            'name'         => $this->name,
201
            'version'      => $this->version,
202
            'occurredOn'   => $this->occurredOn,
203
            'body'         => $this->body,
204
            'id'           => $this->id,
205
            'isDeprecated' => $this->isDeprecated,
206
        ];
207
    }
208
}