1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* event (https://github.com/phpgears/event). |
5
|
|
|
* Event handling. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/event |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\Event; |
15
|
|
|
|
16
|
|
|
use Gears\Event\Exception\EventException; |
17
|
|
|
use Gears\Event\Time\SystemTimeProvider; |
18
|
|
|
use Gears\Event\Time\TimeProvider; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract immutable event. |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractEvent implements Event |
24
|
|
|
{ |
25
|
|
|
use EventBehaviour; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* AbstractEvent constructor. |
29
|
|
|
* |
30
|
|
|
* @param array<string, mixed> $payload |
31
|
|
|
* @param \DateTimeImmutable $createdAt |
32
|
|
|
*/ |
33
|
|
|
private function __construct(array $payload, \DateTimeImmutable $createdAt) |
34
|
|
|
{ |
35
|
|
|
$this->assertImmutable(); |
36
|
|
|
|
37
|
|
|
$this->setPayload($payload); |
38
|
|
|
$this->createdAt = $createdAt->setTimezone(new \DateTimeZone('UTC')); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function getEventType(): string |
45
|
|
|
{ |
46
|
|
|
return static::class; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Instantiate new event. |
51
|
|
|
* |
52
|
|
|
* @param array<string, mixed> $payload |
53
|
|
|
* @param TimeProvider $timeProvider |
54
|
|
|
* |
55
|
|
|
* @return mixed|self |
56
|
|
|
*/ |
57
|
|
|
final protected static function occurred(array $payload, ?TimeProvider $timeProvider = null) |
58
|
|
|
{ |
59
|
|
|
$timeProvider = $timeProvider ?? new SystemTimeProvider(); |
60
|
|
|
|
61
|
|
|
return new static($payload, $timeProvider->getCurrentTime()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
* |
67
|
|
|
* @return mixed|self |
68
|
|
|
*/ |
69
|
|
|
public static function reconstitute(array $payload, \DateTimeImmutable $createdAt, array $attributes) |
70
|
|
|
{ |
71
|
|
|
$event = new static($payload, $createdAt); |
72
|
|
|
|
73
|
|
|
if (isset($attributes['metadata'])) { |
74
|
|
|
$event->addMetadata($attributes['metadata']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $event; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string[] |
82
|
|
|
*/ |
83
|
|
|
final public function __sleep(): array |
84
|
|
|
{ |
85
|
|
|
throw new EventException(\sprintf('Event "%s" cannot be serialized.', static::class)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
final public function __wakeup(): void |
89
|
|
|
{ |
90
|
|
|
throw new EventException(\sprintf('Event "%s" cannot be unserialized.', static::class)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array<string, mixed> |
95
|
|
|
*/ |
96
|
|
|
final public function __serialize(): array |
97
|
|
|
{ |
98
|
|
|
throw new EventException(\sprintf('Event "%s" cannot be serialized.', static::class)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array<string, mixed> $data |
103
|
|
|
* |
104
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
105
|
|
|
*/ |
106
|
|
|
final public function __unserialize(array $data): void |
107
|
|
|
{ |
108
|
|
|
throw new EventException(\sprintf('Event "%s" cannot be unserialized.', static::class)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
* |
114
|
|
|
* @return string[] |
115
|
|
|
*/ |
116
|
|
|
final protected function getAllowedInterfaces(): array |
117
|
|
|
{ |
118
|
|
|
return [Event::class]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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 theid
property of an instance of theAccount
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.