|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
/*
|
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
15
|
|
|
*
|
|
16
|
|
|
* The software is based on the Axon Framework project which is
|
|
17
|
|
|
* licensed under the Apache 2.0 license. For more information on the Axon Framework
|
|
18
|
|
|
* see <http://www.axonframework.org/>.
|
|
19
|
|
|
*
|
|
20
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
21
|
|
|
* and is licensed under the MIT license. For more information, see
|
|
22
|
|
|
* <http://www.governor-framework.org/>.
|
|
23
|
|
|
*/
|
|
24
|
|
|
|
|
25
|
|
|
namespace Governor\Framework\CommandHandling\Handlers;
|
|
26
|
|
|
|
|
27
|
|
|
use Governor\Framework\Common\Annotation\AnnotationReaderFactoryInterface;
|
|
28
|
|
|
use Governor\Framework\Annotations\CommandHandler;
|
|
29
|
|
|
use Governor\Framework\Common\ParameterResolverFactoryInterface;
|
|
30
|
|
|
use Governor\Framework\Common\Annotation\MethodMessageHandlerInspector;
|
|
31
|
|
|
use Governor\Framework\CommandHandling\CommandBusInterface;
|
|
32
|
|
|
use Governor\Framework\CommandHandling\CommandMessageInterface;
|
|
33
|
|
|
use Governor\Framework\CommandHandling\CommandTargetResolverInterface;
|
|
34
|
|
|
use Governor\Framework\CommandHandling\AnnotationCommandTargetResolver;
|
|
35
|
|
|
use Governor\Framework\UnitOfWork\UnitOfWorkInterface;
|
|
36
|
|
|
use Governor\Framework\Repository\RepositoryInterface;
|
|
37
|
|
|
|
|
38
|
|
|
/**
|
|
39
|
|
|
* Description of AggregateCommandHandler
|
|
40
|
|
|
*
|
|
41
|
|
|
* @author "David Kalosi" <[email protected]>
|
|
42
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a>
|
|
43
|
|
|
*/
|
|
44
|
|
|
class AnnotatedAggregateCommandHandler extends AbstractAnnotatedCommandHandler
|
|
45
|
|
|
{
|
|
46
|
|
|
|
|
47
|
|
|
/**
|
|
48
|
|
|
* @var RepositoryInterface
|
|
49
|
|
|
*/
|
|
50
|
|
|
private $repository;
|
|
51
|
|
|
|
|
52
|
|
|
/**
|
|
53
|
|
|
* @var CommandTargetResolverInterface
|
|
54
|
|
|
*/
|
|
55
|
|
|
private $targetResolver;
|
|
56
|
|
|
|
|
57
|
|
|
/**
|
|
58
|
|
|
* @param string $className
|
|
59
|
|
|
* @param string $methodName
|
|
60
|
|
|
* @param ParameterResolverFactoryInterface $parameterResolver
|
|
61
|
|
|
* @param RepositoryInterface $repository
|
|
62
|
|
|
* @param CommandTargetResolverInterface $targetResolver
|
|
63
|
|
|
* @param AnnotationReaderFactoryInterface $annotationReaderFactory
|
|
64
|
|
|
*/
|
|
65
|
|
|
public function __construct(
|
|
66
|
|
|
$className,
|
|
67
|
|
|
$methodName,
|
|
68
|
|
|
ParameterResolverFactoryInterface $parameterResolver,
|
|
69
|
|
|
RepositoryInterface $repository,
|
|
70
|
|
|
CommandTargetResolverInterface $targetResolver = null,
|
|
71
|
|
|
AnnotationReaderFactoryInterface $annotationReaderFactory = null
|
|
72
|
|
|
) {
|
|
73
|
|
|
parent::__construct($className, $methodName, $parameterResolver, $annotationReaderFactory);
|
|
74
|
|
|
$this->repository = $repository;
|
|
75
|
|
|
|
|
76
|
|
|
$this->targetResolver = null === $targetResolver ? new AnnotationCommandTargetResolver($annotationReaderFactory)
|
|
|
|
|
|
|
77
|
|
|
: $targetResolver;
|
|
78
|
|
|
}
|
|
79
|
|
|
|
|
80
|
|
|
public function handle(
|
|
81
|
|
|
CommandMessageInterface $commandMessage,
|
|
82
|
|
|
UnitOfWorkInterface $unitOfWork
|
|
83
|
|
|
) {
|
|
84
|
|
|
if ($this->getMethod()->isConstructor()) {
|
|
85
|
|
|
$this->handleConstructor($commandMessage, $unitOfWork);
|
|
86
|
|
|
|
|
87
|
|
|
return null;
|
|
88
|
|
|
}
|
|
89
|
|
|
|
|
90
|
|
|
return $this->handleMethod($commandMessage, $unitOfWork);
|
|
91
|
|
|
}
|
|
92
|
|
|
|
|
93
|
|
|
private function handleConstructor(
|
|
94
|
|
|
CommandMessageInterface $commandMessage,
|
|
95
|
|
|
UnitOfWorkInterface $unitOfWork
|
|
|
|
|
|
|
96
|
|
|
) {
|
|
97
|
|
|
$reflectionClass = $this->getMethod()->getDeclaringClass();
|
|
98
|
|
|
$arguments = $this->resolveArguments($commandMessage);
|
|
99
|
|
|
|
|
100
|
|
|
$object = $reflectionClass->newInstanceArgs($arguments);
|
|
101
|
|
|
|
|
102
|
|
|
$this->repository->add($object);
|
|
103
|
|
|
}
|
|
104
|
|
|
|
|
105
|
|
|
private function handleMethod(
|
|
106
|
|
|
CommandMessageInterface $commandMessage,
|
|
107
|
|
|
UnitOfWorkInterface $unitOfWork
|
|
|
|
|
|
|
108
|
|
|
) {
|
|
109
|
|
|
$versionedId = $this->targetResolver->resolveTarget($commandMessage);
|
|
110
|
|
|
$aggregate = $this->repository->load(
|
|
111
|
|
|
$versionedId->getIdentifier(),
|
|
112
|
|
|
$versionedId->getVersion()
|
|
113
|
|
|
);
|
|
114
|
|
|
|
|
115
|
|
|
$arguments = $this->resolveArguments($commandMessage);
|
|
116
|
|
|
|
|
117
|
|
|
return $this->getMethod()->invokeArgs($aggregate, $arguments);
|
|
118
|
|
|
}
|
|
119
|
|
|
|
|
120
|
|
|
/**
|
|
121
|
|
|
* @param string $className
|
|
122
|
|
|
* @param RepositoryInterface $repository
|
|
123
|
|
|
* @param CommandBusInterface $commandBus
|
|
124
|
|
|
* @param ParameterResolverFactoryInterface $parameterResolver
|
|
125
|
|
|
* @param CommandTargetResolverInterface $targetResolver
|
|
126
|
|
|
* @param AnnotationReaderFactoryInterface $annotationReaderFactory
|
|
127
|
|
|
*/
|
|
128
|
4 |
|
public static function subscribe(
|
|
129
|
|
|
$className,
|
|
130
|
|
|
RepositoryInterface $repository,
|
|
131
|
|
|
CommandBusInterface $commandBus,
|
|
132
|
|
|
ParameterResolverFactoryInterface $parameterResolver,
|
|
133
|
|
|
CommandTargetResolverInterface $targetResolver = null,
|
|
134
|
|
|
AnnotationReaderFactoryInterface $annotationReaderFactory = null
|
|
135
|
|
|
) {
|
|
136
|
4 |
|
$inspector = new MethodMessageHandlerInspector(
|
|
137
|
4 |
|
$annotationReaderFactory,
|
|
|
|
|
|
|
138
|
4 |
|
new \ReflectionClass($className),
|
|
139
|
|
|
CommandHandler::class
|
|
140
|
4 |
|
);
|
|
141
|
|
|
|
|
142
|
4 |
|
foreach ($inspector->getHandlerDefinitions() as $handlerDefinition) {
|
|
143
|
|
|
$handler = new AnnotatedAggregateCommandHandler(
|
|
144
|
|
|
$className,
|
|
145
|
|
|
$handlerDefinition->getMethod()->name, $parameterResolver,
|
|
146
|
|
|
$repository, $targetResolver, $annotationReaderFactory
|
|
147
|
|
|
);
|
|
148
|
|
|
|
|
149
|
|
|
$commandBus->subscribe(
|
|
150
|
|
|
$handlerDefinition->getPayloadType(),
|
|
151
|
|
|
$handler
|
|
152
|
|
|
);
|
|
153
|
4 |
|
}
|
|
154
|
4 |
|
}
|
|
155
|
|
|
|
|
156
|
|
|
}
|
|
157
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):