Completed
Push — master ( 0b4c0a...f06a3d )
by Adam
14:19
created

QueueMiddleware::getQueue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 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