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\SerializerBuilder; |
10
|
|
|
use JMS\Serializer\SerializerInterface; |
11
|
|
|
use Throwable; |
12
|
|
|
|
13
|
|
|
final class FilesystemQueue implements Queue |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $path; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var SerializerInterface |
22
|
|
|
*/ |
23
|
|
|
private $serializer; |
24
|
|
|
|
25
|
4 |
|
public function __construct(string $path, SerializerInterface $serializer = null) |
26
|
|
|
{ |
27
|
4 |
|
if (!$serializer) { |
28
|
4 |
|
$serializer = SerializerBuilder::create()->build(); |
29
|
|
|
} |
30
|
4 |
|
$this->serializer = $serializer; |
31
|
4 |
|
$this->path = $path; |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Inserts an element if possible, otherwise throwing exception. |
36
|
|
|
* |
37
|
|
|
* @param Envelope $envelope |
38
|
|
|
* @return void |
39
|
|
|
* @throws IllegalStateException |
40
|
|
|
*/ |
41
|
2 |
|
public function add(Envelope $envelope): void |
42
|
|
|
{ |
43
|
2 |
|
if (!$this->write($envelope)) { |
44
|
1 |
|
throw IllegalStateException::create("Could not write to {$this->path}"); |
45
|
|
|
} |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Inserts an element if possible. |
50
|
|
|
* |
51
|
|
|
* @param Envelope $envelope |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
2 |
|
public function offer(Envelope $envelope): void |
55
|
|
|
{ |
56
|
2 |
|
$this->write($envelope); |
57
|
2 |
|
} |
58
|
|
|
|
59
|
4 |
|
private function write(Envelope $envelope): bool |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
4 |
|
$result = (bool)file_put_contents( |
63
|
4 |
|
$this->path, |
64
|
4 |
|
$this->serializer->serialize($envelope, 'json'), |
65
|
4 |
|
FILE_APPEND |
66
|
|
|
); |
67
|
2 |
|
} catch (Throwable $exception) { |
68
|
2 |
|
$result = false; |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Remove and return head of queue, otherwise throwing exception. |
76
|
|
|
* |
77
|
|
|
* @return Envelope |
78
|
|
|
* @throws NoSuchElementException |
79
|
|
|
*/ |
80
|
|
|
public function remove(): Envelope |
81
|
|
|
{ |
82
|
|
|
// TODO: Implement remove() method. |
83
|
|
|
} |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Remove and return head of queue, otherwise returning null. |
87
|
|
|
* |
88
|
|
|
* @return Envelope | null |
89
|
|
|
*/ |
90
|
|
|
public function poll(): ?Envelope |
91
|
|
|
{ |
92
|
|
|
// TODO: Implement poll() method. |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return but do not remove head of queue, otherwise throwing exception. |
97
|
|
|
* |
98
|
|
|
* @return Envelope |
99
|
|
|
* @throws NoSuchElementException |
100
|
|
|
*/ |
101
|
|
|
public function element(): Envelope |
102
|
|
|
{ |
103
|
|
|
// TODO: Implement element() method. |
104
|
|
|
} |
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return but do not remove head of queue, otherwise returning null. |
108
|
|
|
* |
109
|
|
|
* @return Envelope | null |
110
|
|
|
*/ |
111
|
|
|
public function peek(): ?Envelope |
112
|
|
|
{ |
113
|
|
|
// TODO: Implement peek() method. |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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: