QueueMiddleware::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\TacticianQueue\Middleware;
6
7
use Lamoda\QueueBundle\Factory\PublisherFactory;
8
use Lamoda\TacticianQueue\Middleware\Command\ReceivedCommand;
9
use Lamoda\TacticianQueue\Middleware\QueueProducerStrategy\QueueProducerStrategyInterface;
10
use League\Tactician\Middleware;
11
12
final class QueueMiddleware implements Middleware
13
{
14
    /**
15
     * @var PublisherFactory
16
     */
17
    private $publisherFactory;
18
    /**
19
     * @var QueueProducerStrategyInterface
20
     */
21
    private $queueProducerStrategy;
22
23 3
    public function __construct(
24
        PublisherFactory $publisherFactory,
25
        QueueProducerStrategyInterface $queueProducerStrategy
26
    ) {
27 3
        $this->publisherFactory = $publisherFactory;
28 3
        $this->queueProducerStrategy = $queueProducerStrategy;
29 3
    }
30
31 3
    public function execute($command, callable $next)
32
    {
33 3
        if ($command instanceof ReceivedCommand) {
34 1
            $command = $command->getCommand();
35
36 1
            return $next($command);
37
        }
38
39 2
        if (!empty($queues = $this->queueProducerStrategy->produceQueues($command))) {
40 1
            foreach ($queues as $queue) {
41 1
                $this->publisherFactory->publish($queue);
42
            }
43
44 1
            return null;
45
        }
46
47 1
        return $next($command);
48
    }
49
}
50