AggregateChanged   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 96
ccs 0
cts 35
cp 0
rs 10
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A occurredOn() 0 3 1
A __construct() 0 13 2
A aggregateId() 0 3 1
A eventId() 0 3 1
A jsonSerialize() 0 8 1
A occur() 0 12 2
A payload() 0 3 1
A version() 0 3 1
A isPropagationStopped() 0 3 1
A assertValidSerialisedEvent() 0 7 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\EventSource\Domain\Event;
6
7
use DateTimeImmutable;
8
use InvalidArgumentException;
9
use JsonSerializable;
10
use Psr\EventDispatcher\StoppableEventInterface;
11
use Ramsey\Uuid\Uuid;
12
use RuntimeException;
13
14
abstract class AggregateChanged implements StoppableEventInterface, JsonSerializable
15
{
16
    protected const VERSION = 1;
17
    protected const DATE_FORMAT = 'Y-m-d H:i:s';
18
    protected const PROPERTIES = [
19
        'aggregate_id',
20
        'payload',
21
        'occurred_on',
22
    ];
23
    protected string $eventId;
24
    protected string $aggregateId;
25
    protected array $payload;
26
    protected int $version;
27
    protected bool $stopped = false;
28
    protected DateTimeImmutable $occurredOn;
29
30
    final public function __construct(
31
        string $eventId,
32
        string $aggregateId,
33
        array $payload,
34
        DateTimeImmutable $occurredOn
35
    ) {
36
        $this->eventId = $eventId;
37
        $this->aggregateId = $aggregateId;
38
        $this->payload = $payload;
39
        $this->occurredOn = $occurredOn;
40
        /** @var int $version */
41
        $version = is_int(static::VERSION) ? static::VERSION : 1;
0 ignored issues
show
introduced by
The condition is_int(static::VERSION) is always true.
Loading history...
42
        $this->version = $version;
43
    }
44
45
    public static function occur(string $aggregateId, array $payload): self
46
    {
47
        $now = DateTimeImmutable::createFromFormat(self::DATE_FORMAT, date(self::DATE_FORMAT));
48
        if (false === $now) {
49
            throw new RuntimeException('Error ocurred obtaining datetime from server.');
50
        }
51
52
        return new static(
53
            Uuid::uuid4()->toString(),
54
            $aggregateId,
55
            $payload,
56
            $now
57
        );
58
    }
59
60
    private static function assertValidSerialisedEvent(array $serializedEvent): void
0 ignored issues
show
Unused Code introduced by
The method assertValidSerialisedEvent() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
61
    {
62
        foreach (self::PROPERTIES as $property) {
63
            if (array_key_exists($property, $serializedEvent)) {
64
                throw new InvalidArgumentException(sprintf(
65
                    'Property %s is required to reconstitute an event.',
66
                    $property
67
                ));
68
            }
69
        }
70
    }
71
72
    public function eventId(): string
73
    {
74
        return $this->eventId;
75
    }
76
77
    public function aggregateId(): string
78
    {
79
        return $this->aggregateId;
80
    }
81
82
    public function payload(): array
83
    {
84
        return $this->payload;
85
    }
86
87
    public function occurredOn(): DateTimeImmutable
88
    {
89
        return $this->occurredOn;
90
    }
91
92
    public function isPropagationStopped(): bool
93
    {
94
        return $this->stopped;
95
    }
96
97
    public function version(): int
98
    {
99
        return $this->version;
100
    }
101
102
    public function jsonSerialize(): array
103
    {
104
        return [
105
            'name' => static::class,
106
            'aggregate_id' => $this->aggregateId,
107
            'payload' => $this->payload,
108
            'occurred_on' => $this->occurredOn,
109
            'version' => $this->version,
110
        ];
111
    }
112
}
113