Total Complexity | 6 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
16 | final class LockingMiddleware implements MiddlewareInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var bool |
||
20 | */ |
||
21 | private $executing = false; |
||
22 | |||
23 | /** |
||
24 | * @var callable[] |
||
25 | */ |
||
26 | private $queue = []; |
||
27 | |||
28 | 2 | public function deliver(MessageInterface $message, callable $next): void |
|
29 | { |
||
30 | 2 | $this->enqueue($message, $next); |
|
31 | |||
32 | 2 | if ($this->executing) { |
|
33 | 1 | return; |
|
34 | } |
||
35 | |||
36 | try { |
||
37 | 2 | $this->executing = true; |
|
38 | 2 | $this->processQueue(); |
|
39 | 1 | } catch (Exception $e) { |
|
40 | 1 | $this->queue = []; |
|
41 | 1 | throw $e; |
|
42 | 1 | } finally { |
|
43 | 2 | $this->executing = false; |
|
44 | } |
||
45 | 1 | } |
|
46 | |||
47 | /** |
||
48 | * Queues the execution of the next message instead of executing it |
||
49 | * when it's added to the message bus |
||
50 | */ |
||
51 | private function enqueue(MessageInterface $message, callable $next): void |
||
55 | 1 | }; |
|
56 | 2 | } |
|
57 | |||
58 | /** |
||
59 | * Process one message at time so we can isolate their execution |
||
60 | */ |
||
61 | 2 | private function processQueue(): void |
|
65 | } |
||
66 | 1 | } |
|
68 |