Completed
Push — master ( eadab5...90d47b )
by Tomas
06:51
created

Dispatcher::handle()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 16
cp 0.9375
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 15
nc 3
nop 0
crap 5.0061
1
<?php
2
3
namespace Tomaj\Hermes;
4
5
use Exception;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LogLevel;
8
use Tomaj\Hermes\Handler\HandlerInterface;
9
use Tomaj\Hermes\Driver\DriverInterface;
10
use Tomaj\Hermes\Restart\RestartException;
11
use Tomaj\Hermes\Restart\RestartInterface;
12
use Tracy\Debugger;
13
use DateTime;
14
15
class Dispatcher implements DispatcherInterface
16
{
17
    /**
18
     * Dispatcher driver
19
     *
20
     * @var DriverInterface
21
     */
22
    private $driver;
23
24
    /**
25
     * Logger
26
     *
27
     * @var LoggerInterface
28
     */
29
    private $logger;
30
31
    /**
32
     * Restart
33
     *
34
     * @var RestartInterface
35
     */
36
    private $restart;
37
38
    /**
39
     * All registered handlers
40
     *
41
     * @var HandlerInterface[][]
42
     */
43
    private $handlers = [];
44
45
    /**
46
     * @var DateTime
47
     */
48
    private $startTime;
49
50
    /**
51
     * Create new Dispatcher
52
     *
53
     * @param DriverInterface $driver
54
     * @param LoggerInterface $logger
55
     * @param RestartInterface $restart
56
     */
57 18
    public function __construct(DriverInterface $driver, LoggerInterface $logger = null, RestartInterface $restart = null)
58
    {
59 18
        $this->driver = $driver;
60 18
        $this->logger = $logger;
61 18
        $this->restart = $restart;
62 18
        $this->startTime = new DateTime();
63 18
    }
64
65
    /**
66
     * @deprecated - use Emitter::emit method intead
67
     */
68 View Code Duplication
    public function emit(MessageInterface $message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $this->driver->send($message);
71
72
        $this->log(
73
            LogLevel::INFO,
74
            "Dispatcher send message #{$message->getId()} to driver " . get_class($this->driver),
75
            $this->messageLoggerContext($message)
76
        );
77
        return $this;
78
    }
79
80
    /**
81
     * Basic method for background job to star listening.
82
     *
83
     * This method hook to driver wait() method and start listening events.
84
     * Method is blocking, so when you call it all processing will stop.
85
     * WARNING! Don't use it on web server calls. Run it only with cli.
86
     *
87
     * @return void
88
     */
89
    public function handle()
90
    {
91
        try {
92 18
            $this->driver->wait(function (MessageInterface $message) {
93 18
                $this->log(
94 18
                    LogLevel::INFO,
95 18
                    "Start handle message #{$message->getId()} ({$message->getType()})",
96 18
                    $this->messageLoggerContext($message)
97 12
                );
98
99 18
                $result = $this->dispatch($message);
100
101 18
                if ($this->restart && $this->restart->shouldRestart($this->startTime)) {
102 3
                    throw new RestartException('Restart');
103
                }
104
105 15
                return $result;
106 18
            });
107 13
        } catch (RestartException $e) {
108 3
            $this->log(LogLevel::NOTICE, 'Existing hermes dispatcher - restart');
109 2
        } catch (Exception $exception) {
110
            Debugger::log($exception, Debugger::EXCEPTION);
111
        }
112 18
    }
113
114
    /**
115
     * Dispatch message
116
     *
117
     * @param MessageInterface $message
118
     *
119
     * @return bool
120
     */
121 18
    private function dispatch(MessageInterface $message)
122
    {
123 18
        $type = $message->getType();
124
125 18
        if (!$this->hasHandlers($type)) {
126 6
            return true;
127
        }
128
129 15
        $result = true;
130
131 15
        foreach ($this->handlers[$type] as $handler) {
132 15
            $handlerResult = $this->handleMessage($handler, $message);
133
134 15
            if ($result && !$handlerResult) {
135 9
                $result = false;
136 4
            }
137 10
        }
138
139 15
        return $result;
140
    }
141
142
    /**
143
     * Handle given message with given handler
144
     *
145
     * @param HandlerInterface $handler
146
     * @param MessageInterface $message
147
     *
148
     * @return bool
149
     */
150 15
    private function handleMessage(HandlerInterface $handler, MessageInterface $message)
151
    {
152
        // check if handler implements Psr\Log\LoggerAwareInterface (you can use \Psr\Log\LoggerAwareTrait)
153 15
        if ($this->logger && method_exists($handler, 'setLogger')) {
154
            $handler->setLogger($this->logger);
0 ignored issues
show
Bug introduced by
The method setLogger() does not seem to exist on object<Tomaj\Hermes\Handler\HandlerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
        }
156
157
        try {
158 15
            $result = $handler->handle($message);
159
160 12
            $this->log(
161 12
                LogLevel::INFO,
162 12
                "End handle message #{$message->getId()} ({$message->getType()})",
163 12
                $this->messageLoggerContext($message)
164 8
            );
165 11
        } catch (Exception $e) {
166 3
            $this->log(
167 3
                LogLevel::ERROR,
168 3
                "Handler " . get_class($handler) . " throws exception - {$e->getMessage()}",
169 3
                ['error' => $e, 'message' => $this->messageLoggerContext($message), 'exception' => $e]
170 2
            );
171 3
            Debugger::log($e, Debugger::EXCEPTION);
172 3
            $result = false;
173
        }
174 15
        return $result;
175
    }
176
177
    /**
178
     * Check if actual dispatcher has handler for given type
179
     *
180
     * @param string $type
181
     *
182
     * @return bool
183
     */
184 18
    private function hasHandlers($type)
185
    {
186 18
        return isset($this->handlers[$type]) && count($this->handlers[$type]) > 0;
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192 18
    public function registerHandler($type, HandlerInterface $handler)
193
    {
194 18
        if (!isset($this->handlers[$type])) {
195 18
            $this->handlers[$type] = [];
196 12
        }
197
198 18
        $this->handlers[$type][] = $handler;
199 18
    }
200
201
    /**
202
     * Serialize message to logger context
203
     *
204
     * @param MessageInterface $message
205
     *
206
     * @return array
207
     */
208 18 View Code Duplication
    private function messageLoggerContext(MessageInterface $message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
    {
210
        return [
211 18
            'id' => $message->getId(),
212 18
            'created' => $message->getCreated(),
213 18
            'type' => $message->getType(),
214 18
            'payload' => $message->getPayload(),
215 12
        ];
216
    }
217
218
    /**
219
     * Interal log method wrapper
220
     *
221
     * @param string $level
222
     * @param string $message
223
     * @param array $context
224
     *
225
     * @return void
226
     */
227 18
    private function log($level, $message, array $context = array())
228
    {
229 18
        if ($this->logger) {
230 3
            $this->logger->log($level, $message, $context);
231 2
        }
232 18
    }
233
}
234