Completed
Push — master ( e9000a...3a63a2 )
by Damian
03:43
created

RedisQueue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
12
class RedisQueue implements Queue
13
{
14
    use HasFallbackSerializer;
15
16
    /**
17
     * @var Client
18
     */
19
    private $client;
20
21
    /**
22
     * @var SerializerInterface
23
     */
24
    private $serializer;
25
26
    /**
27
     * @param mixed $parameters Connection parameters. Docs: https://github.com/nrk/predis/wiki/Connection-Parameters
28
     * @param mixed $options Options to configure. Docs: https://github.com/nrk/predis/wiki/Client-Options
29
     * @param SerializerInterface|null $serializer
30
     */
31
    public function __construct($parameters = null, $options = null,  SerializerInterface $serializer = null)
32
    {
33
        $this->client = new Client($parameters, $options);
34
        $this->client->connect();
35
        $this->serializer = $serializer;
36
        $this->fallbackSerializer();
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function add(Envelope $envelope): void
43
    {
44
        if (!$this->offer($envelope)) {
45
            throw new IllegalStateException("Could not write to redis");
46
        }
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function offer(Envelope $envelope): bool
53
    {
54
        $serialized = $this->serializer->serialize($envelope, 'json');
55
56
        return (bool)$this->client->rpush(
57
            $envelope->getTitle(),
58
            [$serialized]
59
        );
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function remove(): Envelope
66
    {
67
        // TODO: Implement remove() method.
68
    }
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...
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function poll(): ?Envelope
74
    {
75
        // TODO: Implement poll() method.
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function element(): Envelope
82
    {
83
        // TODO: Implement element() method.
84
    }
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...
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function peek(): ?Envelope
90
    {
91
        // TODO: Implement peek() method.
92
    }
93
}
94