IronDriver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 87
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A enqueue() 0 10 1
A dequeue() 0 13 2
A retry() 0 4 1
A fail() 0 4 1
A ack() 0 13 2
1
<?php
2
namespace Spekkionu\PMG\Queue\Iron\Driver;
3
4
use Spekkionu\PMG\Queue\Iron\Envelope\IronEnvelope;
5
use IronMQ\IronMQ;
6
use PMG\Queue\Driver\AbstractPersistanceDriver;
7
use PMG\Queue\Exception;
8
use PMG\Queue\Serializer\Serializer;
9
use PMG\Queue\DefaultEnvelope;
10
use PMG\Queue\Envelope;
11
use PMG\Queue\Message;
12
use PMG\Queue\Exception\InvalidEnvelope;
13
14
class IronDriver extends AbstractPersistanceDriver
15
{
16
    /**
17
     * @var IronMQ
18
     */
19
    private $iron;
20
    /**
21
     * @var array
22
     */
23
    private $options;
24
25
    /**
26
     * @param IronMQ $iron
27
     * @param Serializer $serializer
28
     * @param array $options Properties to pass with the message
29
     */
30 8
    public function __construct(IronMQ $iron, Serializer $serializer = null, array $options = [])
31
    {
32 8
        parent::__construct($serializer);
0 ignored issues
show
Bug introduced by
It seems like $serializer defined by parameter $serializer on line 30 can be null; however, PMG\Queue\Driver\Abstrac...ceDriver::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
33 8
        $this->iron = $iron;
34 8
        $this->options = $options;
35 8
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40 1
    public function enqueue($queueName, Message $message)
41
    {
42 1
        $env = new DefaultEnvelope($message);
43 1
        $data = $this->serialize($env);
44
45 1
        $job = $this->iron->postMessage($queueName, $data, $this->options);
46
47 1
        return new IronEnvelope($job->id, $env);
48
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 2
    public function dequeue($queueName)
55
    {
56 2
        $job = $this->iron->reserveMessage($queueName);
57
58 2
        if ($job) {
59 1
            $wrapped = $this->unserialize($job->body);
60 1
            $message = new DefaultEnvelope($wrapped->unwrap(), $job->reserved_count);
61 1
            $env = new IronEnvelope($job->id, $message, $job->reservation_id);
62
63 1
            return $env;
64
        }
65 1
        return null;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71 3
    public function ack($queueName, Envelope $envelope)
72
    {
73 3
        if (!$envelope instanceof IronEnvelope) {
74 1
            throw new InvalidEnvelope(sprintf(
75 1
                '%s requires that envelopes be instances of "%s", got "%s"',
76 1
                __CLASS__,
77 1
                IronEnvelope::class,
78
                get_class($envelope)
79
            ));
80
        }
81
82 2
        $this->iron->deleteMessage($queueName, $envelope->getId(), $envelope->getReservationId());
83 2
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88 1
    public function retry($queueName, Envelope $envelope)
89
    {
90 1
        return $envelope->retry();
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 1
    public function fail($queueName, Envelope $envelope)
97
    {
98 1
        return $this->ack($queueName, $envelope);
99
    }
100
}
101