Completed
Pull Request — master (#14)
by Bartek
06:48
created

DomainEvent::setOrigin()   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
     * DomainEvent constructor.
36
     * @param $origin
37
     * @param $name
38
     * @param $version
39
     * @param $occurredOn
40
     * @param array $body
41
     */
42
    public function __construct($origin, $name, $version, $occurredOn, array $body = [])
43
    {
44
        $this->setOrigin($origin)
45
             ->setName($name)
46
             ->setVersion($version)
47
             ->setOccurredOn($occurredOn)
48
        ;
49
        $this->body = $body;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getOrigin()
56
    {
57
        return $this->origin;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getName()
64
    {
65
        return $this->name;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getVersion()
72
    {
73
        return $this->version;
74
    }
75
76
    /**
77
     * Timestamp
78
     *
79
     * @return int
80
     */
81
    public function getOccurredOn()
82
    {
83
        return $this->occurredOn;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function getBody()
90
    {
91
        return $this->body;
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getDelay()
98
    {
99
        return 0;
100
    }
101
102
    /**
103
     * @param $origin
104
     * @return $this
105
     * @throws DomainEventException
106
     */
107
    protected function setOrigin($origin)
108
    {
109
        if(empty($origin)) {
110
            throw new DomainEventException('DomainEvent origin cannot be empty');
111
        }
112
        $this->origin = $origin;
113
        return $this;
114
    }
115
116
    /**
117
     * @param $name
118
     * @return $this
119
     * @throws DomainEventException
120
     */
121
    protected function setName($name)
122
    {
123
        if(empty($name)) {
124
            throw new DomainEventException('DomainEvent name cannot be empty');
125
        }
126
        $this->name = $name;
127
        return $this;
128
    }
129
130
    /**
131
     * @param $version
132
     * @return $this
133
     * @throws DomainEventException
134
     */
135
    protected function setVersion($version)
136
    {
137
        if(empty($version)) {
138
            throw new DomainEventException('DomainEvent version cannot be empty');
139
        }
140
        $this->version = $version;
141
        return $this;
142
    }
143
144
    /**
145
     * @param $occurredOn
146
     * @return $this
147
     * @throws DomainEventException
148
     */
149
    protected function setOccurredOn($occurredOn)
150
    {
151
        if(!is_null($occurredOn) && !preg_match('/^\d+(\.\d{1,4})?$/', $occurredOn)) { // accepts also microseconds
152
            throw new DomainEventException("$occurredOn is not a valid unix timestamp.");
153
        }
154
155
        if ($occurredOn > time()) {
156
            throw new DomainEventException('OccuredOn cannot be located in the future');
157
        }
158
159
        $this->occurredOn = $occurredOn;
160
        return $this;
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    public function jsonSerialize()
167
    {
168
        return [
169
            'origin'     => $this->origin,
170
            'name'       => $this->name,
171
            'version'    => $this->version,
172
            'occurredOn' => $this->occurredOn,
173
            'body'       => $this->body
174
        ];
175
    }
176
}