AbstractEvent   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 79
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A eventId() 0 4 1
A occurredOn() 0 4 1
A author() 0 4 1
jsonSerialize() 0 1 ?
unserializeEvent() 0 1 ?
1
<?php
2
3
/**
4
 * This file is part of slick/cqrs-tools
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\CQRSTools\Event;
11
12
use DateTimeImmutable;
13
use Exception;
14
use League\Event\AbstractEvent as LeagueEvent;
15
use Slick\CQRSTools\Domain\AggregateRootIdentifier;
16
use Slick\CQRSTools\Domain\Event\EventId;
17
use Slick\CQRSTools\Event;
18
use Slick\CQRSTools\Exception\FailToCreateDateException;
19
20
/**
21
 * Abstract Event
22
 *
23
 * @package Slick\CQRSTools\Event
24
 */
25
abstract class AbstractEvent extends LeagueEvent implements Event
26
{
27
28
    /**
29
     * @var EventId
30
     */
31
    protected $eventId;
32
33
    /**
34
     * @var DateTimeImmutable
35
     */
36
    protected $occurredOn;
37
38
    /**
39
     * @var null|AggregateRootIdentifier
40
     */
41
    protected $author;
42
43
    /**
44
     * Creates an Abstract Event
45
     *
46
     * @param AggregateRootIdentifier|null $author
47
     */
48
    public function __construct(AggregateRootIdentifier $author = null)
49
    {
50
        $this->eventId = new EventId();
51
        $this->author = $author;
52
        try {
53
            $this->occurredOn = new DateTimeImmutable();
54
        } catch (Exception $caught) {
55
            throw new FailToCreateDateException($caught->getMessage(), 1, $caught);
56
        }
57
    }
58
59
    /**
60
     * Event identifier
61
     *
62
     * @return EventId
63
     */
64
    public function eventId(): EventId
65
    {
66
        return $this->eventId;
67
    }
68
69
    /**
70
     * Date and time event has occurred
71
     *
72
     * @return DateTimeImmutable
73
     */
74
    public function occurredOn(): DateTimeImmutable
75
    {
76
        return $this->occurredOn;
77
    }
78
79
    /**
80
     * The aggregate identifier of the entity responsible for this event
81
     *
82
     * @return null|AggregateRootIdentifier
83
     */
84
    public function author(): ?AggregateRootIdentifier
85
    {
86
        return $this->author;
87
    }
88
89
    /**
90
     * Specify data which should be serialized to JSON
91
     *
92
     * @return mixed data which can be serialized by json_encode(),
93
     *               which is a value of any type other than a resource.
94
     */
95
    abstract public function jsonSerialize();
96
97
    /**
98
     * Used to unserialize from a stored event
99
     *
100
     * @param mixed $data
101
     */
102
    abstract public function unserializeEvent($data): void;
103
}
104