Event   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
c 4
b 0
f 0
lcom 2
cbo 0
dl 0
loc 104
rs 10
ccs 35
cts 35
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getPayload() 0 4 1
A getSequence() 0 4 1
A getTimestamp() 0 4 1
A __construct() 0 9 2
A serialize() 0 12 2
A unserialize() 0 16 2
1
<?php namespace C4tech\RayEmitter\Domain;
2
3
use stdClass;
4
use C4tech\RayEmitter\Contracts\Domain\Event as EventInterface;
5
6
abstract class Event implements EventInterface
7
{
8
    /**
9
     * Global identifier for the entity related to this event.
10
     * @var string
11
     */
12
    protected $identifier;
13
14
    /**
15
     * Version sequence number.
16
     * @var integer
17
     */
18
    protected $sequence = 0;
19
20
    /**
21
     * Date object of when the event occurred.
22
     * @var DateTime
23
     */
24
    protected $timestamp;
25
26
    /**
27
     * Payload of event alterations.
28
     * @var object
29
     */
30
    protected $payload;
31
32 7
    public function __construct($identifier, array $payload)
33
    {
34 7
        $this->identifier = $identifier;
35
36 7
        $this->payload = new stdClass;
37 7
        foreach ($payload as $key => $value) {
38 7
            $this->payload->$key = $value;
39 7
        }
40 7
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 1
    public function getId()
46
    {
47 1
        return $this->identifier;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 2
    public function getPayload()
54
    {
55 2
        return $this->payload;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 2
    public function getSequence()
62
    {
63 2
        return $this->sequence;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 2
    public function getTimestamp()
70
    {
71 2
        return $this->timestamp;
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77 1
    public function serialize()
78
    {
79 1
        $payload = [];
80 1
        foreach (get_object_vars($this->payload) as $key => $value_object) {
81 1
            $payload[$key] = [
82 1
                'class' => get_class($value_object),
83 1
                'value' => json_encode($value_object)
84 1
            ];
85 1
        }
86
87 1
        return json_encode($payload);
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93 1
    public static function unserialize($record)
94
    {
95 1
        $payload = json_decode($record->payload, true);
96 1
        $data = [];
97
98 1
        foreach ($payload as $key => $config) {
99 1
            $class = $config['class'];
100 1
            $data[$key] = $class::jsonUnserialize($config['value']);
101 1
        }
102
103 1
        $event = new static($record->identifier, $data);
104 1
        $event->sequence = $record->sequence;
105 1
        $event->timestamp = $record->created_at;
106
107 1
        return $event;
108
    }
109
}
110