Completed
Push — master ( 42bfdd...25163a )
by Julián
02:19
created

AbstractEmptyAggregateEvent::__serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * event-sourcing (https://github.com/phpgears/event-sourcing).
5
 * Event Sourcing base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event-sourcing
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\EventSourcing\Event;
15
16
use Gears\Event\Time\SystemTimeProvider;
17
use Gears\Event\Time\TimeProvider;
18
use Gears\EventSourcing\Aggregate\AggregateVersion;
19
use Gears\EventSourcing\Event\Exception\AggregateEventException;
20
use Gears\Identity\Identity;
21
22
/**
23
 * Abstract empty immutable aggregate event.
24
 */
25
abstract class AbstractEmptyAggregateEvent implements AggregateEvent
26
{
27
    use AggregateEventBehaviour;
28
29
    /**
30
     * Prevent aggregate event direct instantiation.
31
     *
32
     * @param Identity           $aggregateId
33
     * @param \DateTimeImmutable $createdAt
34
     */
35
    final protected function __construct(Identity $aggregateId, \DateTimeImmutable $createdAt)
36
    {
37
        $this->assertImmutable();
38
39
        $this->identity = $aggregateId;
40
        $this->version = new AggregateVersion(0);
41
        $this->createdAt = $createdAt->setTimezone(new \DateTimeZone('UTC'));
0 ignored issues
show
Documentation Bug introduced by
It seems like $createdAt->setTimezone(new DateTimeZone('UTC')) can also be of type false. However, the property $createdAt is declared as type DateTimeImmutable. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getEventType(): string
48
    {
49
        return static::class;
50
    }
51
52
    /**
53
     * Instantiate new aggregate event.
54
     *
55
     * @param Identity          $aggregateId
56
     * @param TimeProvider|null $timeProvider
57
     *
58
     * @return mixed|self
59
     */
60
    final protected static function occurred(Identity $aggregateId, ?TimeProvider $timeProvider = null)
61
    {
62
        $timeProvider = $timeProvider ?? new SystemTimeProvider();
63
64
        return new static($aggregateId, $timeProvider->getCurrentTime());
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     *
70
     * @throws AggregateEventException
71
     *
72
     * @return mixed|self
73
     *
74
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
75
     */
76
    final public static function reconstitute(array $payload, \DateTimeImmutable $createdAt, array $attributes = [])
77
    {
78
        $event = new static($attributes['aggregateId'], $createdAt);
79
80
        if (!isset($attributes['aggregateVersion'])
81
            || !$attributes['aggregateVersion'] instanceof AggregateVersion
82
            || (new AggregateVersion(0))->isEqualTo($attributes['aggregateVersion'])
83
        ) {
84
            throw new AggregateEventException(\sprintf(
85
                'Invalid aggregate version, "%s" given',
86
                $attributes['aggregateVersion'] instanceof AggregateVersion
87
                    ? $attributes['aggregateVersion']->getValue()
88
                    : \gettype($attributes['aggregateVersion'])
89
            ));
90
        }
91
92
        $event->version = $attributes['aggregateVersion'];
93
94
        if (isset($attributes['metadata'])) {
95
            $event->addMetadata($attributes['metadata']);
96
        }
97
98
        return $event;
99
    }
100
101
    /**
102
     * @return array<string, mixed>
103
     */
104
    final public function __serialize(): array
105
    {
106
        throw new AggregateEventException(\sprintf('Aggregate event "%s" cannot be serialized', static::class));
107
    }
108
109
    /**
110
     * @param array<string, mixed> $data
111
     *
112
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
113
     */
114
    final public function __unserialize(array $data): void
115
    {
116
        throw new AggregateEventException(\sprintf('Aggregate event "%s" cannot be unserialized', static::class));
117
    }
118
119
    /**
120
     * @return string[]
121
     */
122
    final public function __sleep(): array
123
    {
124
        throw new AggregateEventException(\sprintf('Aggregate event "%s" cannot be serialized', static::class));
125
    }
126
127
    final public function __wakeup(): void
128
    {
129
        throw new AggregateEventException(\sprintf('Aggregate event "%s" cannot be unserialized', static::class));
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     *
135
     * @return string[]
136
     */
137
    final protected function getAllowedInterfaces(): array
138
    {
139
        return [AggregateEvent::class];
140
    }
141
}
142