Completed
Pull Request — master (#10)
by Adam
02:42
created

QueueMiddleware::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Equip\Queue;
4
5
use League\Tactician\Middleware;
6
7
class QueueMiddleware implements Middleware
8
{
9
    /**
10
     * @var Queue
11
     */
12
    private $queue;
13
14
    /**
15
     * @var array
16
     */
17
    private $queue_map;
18
19
    /**
20
     * @param Queue $queue
21
     * @param array $queue_map
22
     */
23
    public function __construct(Queue $queue, array $queue_map = [])
24
    {
25
        $this->queue = $queue;
26
        $this->queue_map = $queue_map;
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function execute($command, callable $next)
33
    {
34
        if ($command instanceof QueueableCommand) {
35
            $queue = $this->getQueue(get_class($command));
36
37
            return $this->queue->add(
38
                $queue,
39
                // Wraps the command so it isn't requeued
40
                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...
41
            );
42
        }
43
44
        if ($command instanceof QueuedCommand) {
45
            $command = $command->command();
46
        }
47
48
        return $next($command);
49
    }
50
51
    /**
52
     * Gets the specific queue for the command
53
     *
54
     * @param string $command_name
55
     *
56
     * @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...
57
     *
58
     * @throws \Exception If the command isn't assigned to a pipe
59
     */
60
    private function getQueue($command_name)
61
    {
62
        // TODO: refactor this?
63
        foreach ($this->queue_map as $queue => $commands) {
64
            if (in_array($command_name, $commands)) {
65
                return $queue;
66
            }
67
        }
68
69
        // TODO: throw exception or just a generic queue?
70
        throw new \Exception('no queue belongs to that command');
71
    }
72
}
73