Passed
Push — master ( 3a63a2...33c461 )
by Damian
02:52
created

RedisQueue::peek()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Initx\Driver;
4
5
use Initx\Envelope;
6
use Initx\Exception\IllegalStateException;
7
use Initx\Exception\NoSuchElementException;
8
use Initx\Queue;
9
use JMS\Serializer\SerializerInterface;
10
use Predis\Client;
11
use Predis\ClientInterface;
12
use Throwable;
13
14
class RedisQueue implements Queue
15
{
16
    use HasFallbackSerializer;
17
18
    /**
19
     * @var Client
20
     */
21
    private $client;
22
23
    /**
24
     * @var SerializerInterface
25
     */
26
    private $serializer;
27
28
    /**
29
     * @var string
30
     */
31
    private $queue;
32
33 3
    public function __construct(ClientInterface $client, string $queue, ?SerializerInterface $serializer = null)
34
    {
35 3
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
$client is of type Predis\ClientInterface, but the property $client was declared to be of type Predis\Client. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
36 3
        $this->serializer = $serializer;
37 3
        $this->queue = $queue;
38 3
        $this->fallbackSerializer();
39 3
    }
40
41 2
    public function add(Envelope $envelope): void
42
    {
43 2
        if (!$this->offer($envelope)) {
44
            throw new IllegalStateException("Could not write to redis");
45
        }
46 2
    }
47
48 3
    public function offer(Envelope $envelope): bool
49
    {
50 3
        $serialized = $this->serializer->serialize($envelope, 'json');
51
52 3
        return (bool)$this->client->rpush(
53 3
            $this->queue,
54 3
            [$serialized]
55
        );
56
    }
57
58 1
    public function remove(): Envelope
59
    {
60 1
        $element = $this->poll();
61
62 1
        if (!$element) {
63
            throw new NoSuchElementException('Queue empty');
64
        }
65
66 1
        return $element;
67
    }
68
69 1
    public function poll(): ?Envelope
70
    {
71
        try {
72 1
            $serialized = $this->client->lpop($this->queue);
73
        } catch (Throwable $e) {
74
            throw new IllegalStateException("Predis connection error", 0, $e);
75
        }
76
77 1
        if (empty($serialized)) {
78
            return null;
79
        }
80
81 1
        return $this->serializer->deserialize($serialized, Envelope::class, 'json');
82
    }
83
84
    public function element(): Envelope
85
    {
86
        // TODO: Implement element() method.
87
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Initx\Envelope. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
88
89
    public function peek(): ?Envelope
90
    {
91
        // TODO: Implement peek() method.
92
    }
93
}
94