AsynchronousDeliveryMiddleware::deliver()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 13
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\MessageBus\Bus\DeliveryChain\Middleware;
6
7
use Werkspot\MessageBus\Bus\DeliveryChain\MiddlewareInterface;
8
use Werkspot\MessageBus\Message\MessageInterface;
9
use Werkspot\MessageBus\MessageQueue\AsynchronousMessageInterface;
10
use Werkspot\MessageBus\MessageQueue\MessageQueueServiceInterface;
11
12
final class AsynchronousDeliveryMiddleware implements MiddlewareInterface
13
{
14
    /**
15
     * @var MessageQueueServiceInterface
16
     */
17
    private $messageQueueService;
18
19 3
    public function __construct(MessageQueueServiceInterface $messageQueueService)
20
    {
21 3
        $this->messageQueueService = $messageQueueService;
22 3
    }
23
24 3
    public function deliver(MessageInterface $message, callable $next): void
25
    {
26 3
        if ($message instanceof AsynchronousMessageInterface) {
27
            // By default we 'queue' all QueuedMessage, even if the execute_at is in the past.
28
            // We do this  so that we support transactions and can rollback the execution if something goes wrong in
29
            // another part of the transaction.
30
            // If we were to send it to rabbit in the Async middleware, and further on an exception occurs, if would
31
            // already have been processed using rabbit, and there's no way to roll it back.
32 1
            $this->messageQueueService->enqueueMessage($message);
33 1
            return;
34
        }
35
36 2
        $next($message);
37 2
    }
38
}
39