1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2012-2014 Janos Szurovecz |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
7
|
|
|
* the Software without restriction, including without limitation the rights to |
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
10
|
|
|
* so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
13
|
|
|
* copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
* SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace predaddy\commandhandling; |
25
|
|
|
|
26
|
|
|
use ArrayObject; |
27
|
|
|
use Exception; |
28
|
|
|
use precore\lang\ObjectClass; |
29
|
|
|
use precore\util\Preconditions; |
30
|
|
|
use predaddy\domain\GenericAggregateId; |
31
|
|
|
use predaddy\domain\Repository; |
32
|
|
|
use predaddy\domain\StateHashAware; |
33
|
|
|
use predaddy\messagehandling\ClosureWrapper; |
34
|
|
|
use predaddy\messagehandling\MessageHandlerDescriptorFactory; |
35
|
|
|
use predaddy\messagehandling\util\SimpleMessageCallback; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* This class acts as a {@link CommandBus} expect one case. If the posted command is a {@link DirectCommand} |
39
|
|
|
* and there is no registered handler which could process that, it will load the appropriate AR from the given |
40
|
|
|
* repository and passes the command to that. This bus should be used if business method parameters |
41
|
|
|
* in the aggregates are {@link Command} objects. |
42
|
|
|
* |
43
|
|
|
* If you have specialized repositories for your aggregates, it is recommended to use {@link RepositoryDelegate}. |
44
|
|
|
* |
45
|
|
|
* @author Janos Szurovecz <[email protected]> |
46
|
|
|
*/ |
47
|
|
|
class DirectCommandBus extends CommandBus |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var Repository |
51
|
|
|
*/ |
52
|
|
|
private $repository; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var MessageHandlerDescriptorFactory |
56
|
|
|
*/ |
57
|
|
|
private $handlerDescriptorFactory; |
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param DirectCommandBusBuilder $builder |
61
|
|
|
*/ |
62
|
|
|
public function __construct(DirectCommandBusBuilder $builder) |
63
|
|
|
{ |
64
|
|
|
parent::__construct($builder); |
65
|
|
|
$this->repository = $builder->getRepository(); |
66
|
|
|
$this->handlerDescriptorFactory = $builder->getHandlerDescriptorFactory(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The given repository cannot be null, the default value is due to PHP restrictions. |
71
|
|
|
* |
72
|
|
|
* @param Repository $repository Is being passed to the registered DirectCommandForwarder |
73
|
|
|
* @return DirectCommandBusBuilder |
74
|
|
|
*/ |
75
|
|
|
public static function builder(Repository $repository = null) |
76
|
|
|
{ |
77
|
|
|
return new DirectCommandBusBuilder(Preconditions::checkNotNull($repository)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function callableWrappersFor($message) |
81
|
|
|
{ |
82
|
|
|
$wrappers = parent::callableWrappersFor($message); |
83
|
|
|
if (($message instanceof DirectCommand) && $wrappers->count() === 0) { |
84
|
|
|
$wrappers = new ArrayObject([new ClosureWrapper( |
85
|
|
|
function (DirectCommand $command) { |
86
|
|
|
return $this->forwardCommand($command); |
87
|
|
|
} |
88
|
|
|
)]); |
89
|
|
|
} |
90
|
|
|
return $wrappers; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param DirectCommand $command |
95
|
|
|
* @throws \Exception If the handler throws any |
96
|
|
|
* @return mixed The return value of the last handler (should be one handler per aggregate) |
97
|
|
|
*/ |
98
|
|
|
private function forwardCommand(DirectCommand $command) |
99
|
|
|
{ |
100
|
|
|
$aggregateClass = $command->aggregateClass(); |
101
|
|
|
$aggregateId = $command->aggregateId(); |
102
|
|
|
if ($aggregateId === null) { |
103
|
|
|
$aggregate = ObjectClass::forName($aggregateClass)->newInstanceWithoutConstructor(); |
104
|
|
|
self::getLogger()->debug('New aggregate [{}] has been created', [$aggregateClass]); |
105
|
|
|
} else { |
106
|
|
|
$aggregate = $this->repository->load(new GenericAggregateId($aggregateId, $aggregateClass)); |
107
|
|
|
self::getLogger()->debug( |
108
|
|
|
'Aggregate [{}] with ID [{}] has been successfully loaded', |
109
|
|
|
[$aggregateClass, $aggregateId] |
110
|
|
|
); |
111
|
|
|
if ($command instanceof StateHashAware) { |
112
|
|
|
$aggregate->failWhenStateHashViolation($command->stateHash()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
$forwarderBus = CommandBus::builder() |
116
|
|
|
->withIdentifier($aggregateClass) |
117
|
|
|
->withHandlerDescriptorFactory($this->handlerDescriptorFactory) |
118
|
|
|
->build(); |
119
|
|
|
$forwarderBus->register($aggregate); |
120
|
|
|
$callback = new SimpleMessageCallback(); |
121
|
|
|
$forwarderBus->post($command, $callback); |
122
|
|
|
$thrownException = $callback->getException(); |
123
|
|
|
if ($thrownException instanceof Exception) { |
124
|
|
|
self::getLogger()->debug('Error occurred when command has been applied [{}]', [$command], $thrownException); |
125
|
|
|
throw $thrownException; |
126
|
|
|
} |
127
|
|
|
$this->repository->save($aggregate); |
128
|
|
|
self::getLogger()->info("Command [{}] has been applied", [$command]); |
129
|
|
|
return $callback->getResult(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|