Completed
Push — master ( 5fca0d...2a987b )
by Quim
02:16
created

DomainEvent::__construct()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 5
nc 1
nop 4
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 int
21
     */
22
    protected $occurredOn;
23
24
    /**
25
     * @var array
26
     */
27
    protected $body = array();
28
29
    /**
30
     * DomainEvent constructor.
31
     * @param $origin
32
     * @param $name
33
     * @param $occurredOn
34
     * @param array $body
35
     */
36
    public function __construct($origin, $name, $occurredOn, array $body = [])
37
    {
38
        $this->setOrigin($origin)
39
             ->setName($name)
40
             ->setOccurredOn($occurredOn)
41
        ;
42
        $this->body = $body;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getOrigin()
49
    {
50
        return $this->origin;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getName()
57
    {
58
        return $this->name;
59
    }
60
61
    /**
62
     * Timestamp
63
     *
64
     * @return int
65
     */
66
    public function getOccurredOn()
67
    {
68
        return $this->occurredOn;
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getBody()
75
    {
76
        return $this->body;
77
    }
78
79
    /**
80
     * @return int
81
     */
82
    public function getDelay()
83
    {
84
        return 0;
85
    }
86
87
    /**
88
     * @param $origin
89
     * @return $this
90
     * @throws DomainEventException
91
     */
92
    protected function setOrigin($origin)
93
    {
94
        if(empty($origin)) {
95
            throw new DomainEventException('DomainEvent origin cannot be empty');
96
        }
97
        $this->origin = $origin;
98
        return $this;
99
    }
100
101
    /**
102
     * @param $name
103
     * @return $this
104
     * @throws DomainEventException
105
     */
106
    protected function setName($name)
107
    {
108
        if(empty($name)) {
109
            throw new DomainEventException('DomainEvent name cannot be empty');
110
        }
111
        $this->name = $name;
112
        return $this;
113
    }
114
115
    /**
116
     * @param $occurredOn
117
     * @return $this
118
     * @throws DomainEventException
119
     */
120
    protected function setOccurredOn($occurredOn)
121
    {
122
        if(!is_null($occurredOn) && !preg_match('/^\d+(\.\d{1,4})?$/', $occurredOn)) { // accepts also microseconds
123
            throw new DomainEventException("$occurredOn is not a valid unix timestamp.");
124
        }
125
        $this->occurredOn = $occurredOn;
126
        return $this;
127
    }
128
129
    /**
130
     * @return array
131
     */
132
    public function jsonSerialize()
133
    {
134
        return [
135
            'origin' => $this->origin,
136
            'name' => $this->name,
137
            'occurredOn' => $this->occurredOn,
138
            'body' => $this->body
139
        ];
140
    }
141
}