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
|
|
|
} |
|
|
|
|
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
|
|
|
} |
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function peek(): ?Envelope |
90
|
|
|
{ |
91
|
|
|
// TODO: Implement peek() method. |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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: