DistributedCommandBus::setLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * The software is based on the Axon Framework project which is
16
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
17
 * see <http://www.axonframework.org/>.
18
 *
19
 * This software consists of voluntary contributions made by many individuals
20
 * and is licensed under the MIT license. For more information, see
21
 * <http://www.governor-framework.org/>.
22
 */
23
24
namespace Governor\Framework\CommandHandling\Distributed;
25
26
use Governor\Framework\CommandHandling\Callbacks\NoOpCallback;
27
use Governor\Framework\CommandHandling\CommandBusInterface;
28
use Governor\Framework\CommandHandling\CommandCallbackInterface;
29
use Governor\Framework\CommandHandling\CommandHandlerInterface;
30
use Governor\Framework\CommandHandling\CommandMessageInterface;
31
use Governor\Framework\CommandHandling\CommandDispatchInterceptorInterface;
32
use Psr\Log\LoggerAwareInterface;
33
use Governor\Framework\Common\Logging\NullLogger;
34
use Psr\Log\LoggerInterface;
35
36
class DistributedCommandBus implements CommandBusInterface, LoggerAwareInterface
37
{
38
39
    const DISPATCH_ERROR_MESSAGE = 'An error occurred while trying to dispatch a command on the DistributedCommandBus';
40
41
    /**
42
     * @var CommandBusConnectorInterface
43
     */
44
    private $connector;
45
46
    /**
47
     * @var RoutingStrategyInterface
48
     */
49
    private $routingStrategy;
50
51
    /**
52
     * @var CommandDispatchInterceptorInterface[]
53
     */
54
    private $dispatchInterceptors;
55
56
    /**
57
     * @var LoggerInterface
58
     */
59
    private $logger;
60
61
    /**
62
     * @param CommandBusConnectorInterface $connector
63
     * @param RoutingStrategyInterface $routingStrategy
64
     */
65
    function __construct(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
        CommandBusConnectorInterface $connector,
67
        RoutingStrategyInterface $routingStrategy
68
    ) {
69
        $this->connector = $connector;
70
        $this->routingStrategy = $routingStrategy;
71
        $this->logger = new NullLogger();
72
    }
73
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function dispatch(
79
        CommandMessageInterface $command,
80
        CommandCallbackInterface $callback = null
81
    ) {
82
        $command = $this->intercept($command);
83
        $routingKey = $this->routingStrategy->getRoutingKey($command);
84
85
        $this->logger->debug(
86
            'Dispatching command [{name}] with routing key [{key}] in the DistributedCommandBus',
87
            [
88
                'name' => $command->getCommandName(),
89
                'key' => $routingKey
90
            ]
91
        );
92
93
        try {
94
            $this->connector->send($routingKey, $command, $callback);
95
        } catch (\Exception $ex) {
96
            $this->logger->error(
97
                self::DISPATCH_ERROR_MESSAGE.' {err}',
98
                [
99
                    'err' => $ex->getMessage()
100
                ]
101
            );
102
        }
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function subscribe($commandName, CommandHandlerInterface $handler)
109
    {
110
        $this->connector->subscribe($commandName, $handler);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function unsubscribe($commandName, CommandHandlerInterface $handler)
117
    {
118
        $this->connector->unsubscribe($commandName, $handler);
119
    }
120
121
    /**
122
     * Invokes all the dispatch interceptors.
123
     *
124
     * @param CommandMessageInterface $command The original command being dispatched
125
     * @return CommandMessageInterface The command to actually dispatch
126
     */
127
    protected function intercept(CommandMessageInterface $command)
128
    {
129
        $commandToDispatch = $command;
130
131
        foreach ($this->dispatchInterceptors as $interceptor) {
132
            $commandToDispatch = $interceptor->dispatch($commandToDispatch);
133
        }
134
135
        return $commandToDispatch;
136
    }
137
138
    /**
139
     * @param CommandDispatchInterceptorInterface[] $dispatchInterceptors
140
     */
141
    public function setDispatchInterceptors(array $dispatchInterceptors)
142
    {
143
        $this->dispatchInterceptors = $dispatchInterceptors;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function setLogger(LoggerInterface $logger)
150
    {
151
        $this->logger = $logger;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function getSubscriptions()
158
    {
159
        return [];
160
    }
161
162
163
}