1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* cqrs (https://github.com/phpgears/cqrs). |
5
|
|
|
* CQRS base. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/cqrs |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\CQRS; |
15
|
|
|
|
16
|
|
|
use Gears\CQRS\Exception\CommandException; |
17
|
|
|
use Gears\DTO\ScalarPayloadBehaviour; |
18
|
|
|
use Gears\Immutability\ImmutabilityBehaviour; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract immutable serializable command. |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractCommand implements Command |
24
|
|
|
{ |
25
|
|
|
use ImmutabilityBehaviour, ScalarPayloadBehaviour { |
26
|
|
|
ScalarPayloadBehaviour::__call insteadof ImmutabilityBehaviour; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AbstractCommand constructor. |
31
|
|
|
* |
32
|
|
|
* @param mixed[] $parameters |
33
|
|
|
*/ |
34
|
|
|
final protected function __construct(array $parameters) |
35
|
|
|
{ |
36
|
|
|
$this->assertImmutable(); |
37
|
|
|
|
38
|
|
|
$this->setPayload($parameters); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function getCommandType(): string |
45
|
|
|
{ |
46
|
|
|
return static::class; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
final public static function reconstitute(array $parameters) |
53
|
|
|
{ |
54
|
|
|
$commandClass = static::class; |
55
|
|
|
|
56
|
|
|
return new $commandClass($parameters); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string[] |
61
|
|
|
*/ |
62
|
|
|
final public function __sleep(): array |
63
|
|
|
{ |
64
|
|
|
throw new CommandException(\sprintf('Command "%s" cannot be serialized', static::class)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
final public function __wakeup(): void |
68
|
|
|
{ |
69
|
|
|
throw new CommandException(\sprintf('Command "%s" cannot be unserialized', static::class)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array<string, mixed> |
74
|
|
|
*/ |
75
|
|
|
final public function __serialize(): array |
76
|
|
|
{ |
77
|
|
|
throw new CommandException(\sprintf('Command "%s" cannot be serialized', static::class)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array<string, mixed> $data |
82
|
|
|
* |
83
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
84
|
|
|
*/ |
85
|
|
|
final public function __unserialize(array $data): void |
86
|
|
|
{ |
87
|
|
|
throw new CommandException(\sprintf('Command "%s" cannot be unserialized', static::class)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
* |
93
|
|
|
* @return string[] |
94
|
|
|
*/ |
95
|
|
|
final protected function getAllowedInterfaces(): array |
96
|
|
|
{ |
97
|
|
|
return [Command::class]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|