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; |
26
|
|
|
|
27
|
|
|
use Governor\Framework\UnitOfWork\UnitOfWorkInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Mechanism that takes care of interceptor and event handler execution. |
31
|
|
|
* |
32
|
|
|
* @author "David Kalosi" <[email protected]> |
33
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
34
|
|
|
*/ |
35
|
|
|
class DefaultInterceptorChain implements InterceptorChainInterface |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var CommandMessageInterface |
40
|
|
|
*/ |
41
|
|
|
private $command; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var CommandHandlerInterface |
45
|
|
|
*/ |
46
|
|
|
private $handler; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \ArrayIterator |
50
|
|
|
*/ |
51
|
|
|
private $chain; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var UnitOfWorkInterface |
55
|
|
|
*/ |
56
|
|
|
private $unitOfWork; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initialize the default interceptor chain to dispatch the given <code>command</code>, through the |
60
|
|
|
* <code>chain</code>, to the <code>handler</code>. |
61
|
|
|
* |
62
|
|
|
* @param CommandMessageInterface $command The command to dispatch through the interceptor chain |
63
|
|
|
* @param UnitOfWorkInterface $unitOfWork The UnitOfWork the command is executed in |
64
|
|
|
* @param CommandHandlerInterface $handler The handler for the command |
65
|
|
|
* @param array $chain The interceptor composing the chain |
66
|
|
|
*/ |
67
|
9 |
|
public function __construct(CommandMessageInterface $command, |
68
|
|
|
UnitOfWorkInterface $unitOfWork, CommandHandlerInterface $handler, |
69
|
|
|
array $chain) |
70
|
|
|
{ |
71
|
9 |
|
$this->command = $command; |
72
|
9 |
|
$this->handler = $handler; |
73
|
9 |
|
$this->chain = new \ArrayIterator($chain); |
74
|
9 |
|
$this->unitOfWork = $unitOfWork; |
75
|
9 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
9 |
|
public function proceed(CommandMessageInterface $commandProceedWith = null) |
81
|
|
|
{ |
82
|
9 |
|
if (null !== $commandProceedWith) { |
83
|
1 |
|
$this->command = $commandProceedWith; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
9 |
|
if ($this->chain->valid()) { |
87
|
5 |
|
$next = $this->chain->current(); |
88
|
5 |
|
$this->chain->next(); |
89
|
|
|
|
90
|
5 |
|
return $next->handle($this->command, $this->unitOfWork, $this); |
91
|
|
|
} else { |
92
|
8 |
|
return $this->handler->handle($this->command, $this->unitOfWork); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|