Completed
Push — master ( 90ae48...625ed6 )
by Damian
03:12
created

RedisQueue::element()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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 $queueName;
32
33 6
    public function __construct(ClientInterface $client, string $queueName, ?SerializerInterface $serializer = null)
34
    {
35 6
        $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 6
        $this->serializer = $serializer;
37 6
        $this->queueName = $queueName;
38 6
        $this->fallbackSerializer();
39 6
    }
40
41 5
    public function add(Envelope $envelope): void
42
    {
43 5
        if (!$this->offer($envelope)) {
44
            throw new IllegalStateException("Could not write to redis");
45
        }
46 5
    }
47
48 6
    public function offer(Envelope $envelope): bool
49
    {
50 6
        $serialized = $this->serializer->serialize($envelope, 'json');
51
52 6
        return (bool)$this->client->rpush(
53 6
            $this->queueName,
54 6
            [$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 2
    public function poll(): ?Envelope
70
    {
71
        try {
72 2
            $serialized = $this->client->lpop($this->queueName);
73
        } catch (Throwable $e) {
74
            throw new IllegalStateException("Predis connection error", 0, $e);
75
        }
76
77 2
        if (empty($serialized)) {
78 1
            return null;
79
        }
80
81 2
        return $this->serializer->deserialize($serialized, Envelope::class, 'json');
82
    }
83
84 1
    public function element(): Envelope
85
    {
86 1
        $element = $this->peek();
87
88 1
        if (!$element) {
89
            throw new NoSuchElementException('Queue empty');
90
        }
91
92 1
        return $element;
93
    }
94
95 2
    public function peek(): ?Envelope
96
    {
97 2
        $serialized = $this->client->lrange($this->queueName, 0, 0)[0];
98
99 2
        if (empty($serialized)) {
100
            return null;
101
        }
102
103 2
        return $this->serializer->deserialize($serialized, Envelope::class, 'json');
104
    }
105
}
106