QueueMiddleware   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 66
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
ccs 23
cts 23
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 18 3
A getQueue() 0 12 3
1
<?php
2
3
namespace Equip\Queue;
4
5
use Exception;
6
use League\Tactician\Middleware;
7
8
class QueueMiddleware implements Middleware
9
{
10
    /**
11
     * @var Queue
12
     */
13
    private $queue;
14
15
    /**
16
     * @var array
17
     */
18
    private $queue_map;
19
20
    /**
21
     * @param Queue $queue
22
     * @param array $queue_map
23
     */
24 5
    public function __construct(Queue $queue, array $queue_map = [])
25
    {
26 5
        $this->queue = $queue;
27 5
        $this->queue_map = $queue_map;
28 5
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33 3
    public function execute($command, callable $next)
34
    {
35 3
        if ($command instanceof QueueableCommand) {
36 1
            $queue = $this->getQueue(get_class($command));
37
38 1
            return $this->queue->add(
39 1
                $queue,
40
                // Wraps the command so it isn't requeued
41 1
                new QueuedCommand($command)
0 ignored issues
show
Documentation introduced by
new \Equip\Queue\QueuedCommand($command) is of type object<Equip\Queue\QueuedCommand>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42 1
            );
43
        }
44
45 2
        if ($command instanceof QueuedCommand) {
46 1
            $command = $command->command();
47 1
        }
48
49 2
        return $next($command);
50
    }
51
52
    /**
53
     * Gets the specific queue for the command
54
     *
55
     * @param string $command_name
56
     *
57
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
58
     *
59
     * @throws \Exception If the command isn't assigned to a pipe
60
     */
61 3
    private function getQueue($command_name)
62
    {
63 3
        foreach ($this->queue_map as $queue => $commands) {
64 3
            if (in_array($command_name, $commands)) {
65 2
                return $queue;
66
            }
67 2
        }
68
69 1
        throw new Exception(
70 1
            sprintf('No queue has been set for the `%s` command', $command_name)
71 1
        );
72
    }
73
}
74