Passed
Push — master ( e04fa8...4578ad )
by Damian
03:42
created

FilesystemQueue   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 22
eloc 59
dl 0
loc 130
ccs 63
cts 66
cp 0.9545
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 4 2
A poll() 0 9 2
A peek() 0 9 2
A offer() 0 15 2
B removeFirstLine() 0 30 6
A element() 0 9 2
A readFirstLine() 0 9 3
A remove() 0 9 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 Throwable;
11
12
final class FilesystemQueue implements Queue
13
{
14
    use HasFallbackSerializer;
15
16
    /**
17
     * @var string
18
     */
19
    private $path;
20
21
    /**
22
     * @var SerializerInterface
23
     */
24
    private $serializer;
25
26 11
    public function __construct(string $path, SerializerInterface $serializer = null)
27
    {
28 11
        $this->serializer = $serializer;
29 11
        $this->path = $path;
30 11
        $this->fallbackSerializer();
31 11
    }
32
33 5
    public function add(Envelope $envelope): void
34
    {
35 5
        if (!$this->offer($envelope)) {
36
            throw new IllegalStateException("Could not write to file: {$this->path}");
37
        }
38 4
    }
39
40 7
    public function offer(Envelope $envelope): bool
41
    {
42 7
        $content = $this->serializer->serialize($envelope, 'json').PHP_EOL;
43
44 7
        $result = (bool)@file_put_contents(
45 7
            $this->path,
46 7
            $content,
47 7
            FILE_APPEND
48
        );
49
50 7
        if (!$result) {
51 2
            throw new IllegalStateException("Could not write to file: {$this->path}");
52
        }
53
54 5
        return true;
55
    }
56
57 3
    public function remove(): Envelope
58
    {
59 3
        $envelope = $this->poll();
60
61 2
        if (!$envelope) {
62 1
            throw new NoSuchElementException();
63
        }
64
65 1
        return $envelope;
66
    }
67
68 3
    public function poll(): ?Envelope
69
    {
70 3
        $firstLine = $this->removeFirstLine();
71
72 2
        if (!$firstLine) {
73 1
            return null;
74
        }
75
76 1
        return $this->serializer->deserialize($firstLine, Envelope::class, 'json');
77
    }
78
79 2
    public function element(): Envelope
80
    {
81 2
        $envelope = $this->peek();
82
83 2
        if (!$envelope) {
84 1
            throw new NoSuchElementException();
85
        }
86
87 1
        return $envelope;
88
    }
89
90 4
    public function peek(): ?Envelope
91
    {
92 4
        $firstLine = $this->readFirstLine();
93
94 4
        if (!$firstLine) {
95 2
            return null;
96
        }
97
98 2
        return $this->serializer->deserialize($firstLine, Envelope::class, 'json');
99
    }
100
101 3
    private function removeFirstLine(): ?string
102
    {
103 3
        if (!file_exists($this->path)) {
104 1
            throw new IllegalStateException("File $this->path not exists");
105
        }
106 2
        $firstLine = null;
107 2
        if ($handle = fopen($this->path, 'cb+')) {
108 2
            if (!flock($handle, LOCK_EX)) {
109
                fclose($handle);
110
            }
111 2
            $offset = 0;
112 2
            $len = filesize($this->path);
113 2
            while (($line = fgets($handle, 4096)) !== false) {
114 1
                if (!$firstLine) {
115 1
                    $firstLine = $line;
116 1
                    $offset = strlen($firstLine);
117 1
                    continue;
118
                }
119 1
                $pos = ftell($handle);
120 1
                fseek($handle, $pos - strlen($line) - $offset);
121 1
                fwrite($handle, $line);
122 1
                fseek($handle, $pos);
123
            }
124 2
            fflush($handle);
125 2
            ftruncate($handle, $len - $offset);
126 2
            flock($handle, LOCK_UN);
127 2
            fclose($handle);
128
        }
129
130 2
        return $firstLine;
131
    }
132
133 4
    private function readFirstLine(): ?string
134
    {
135 4
        if (!file_exists($this->path)) {
136
            throw new IllegalStateException("File $this->path not exists");
137
        }
138
139 4
        $firstLine = fgets(fopen($this->path, 'rb'));
0 ignored issues
show
Bug introduced by
It seems like fopen($this->path, 'rb') can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
        $firstLine = fgets(/** @scrutinizer ignore-type */ fopen($this->path, 'rb'));
Loading history...
140
141 4
        return $firstLine ?: null;
142
    }
143
}
144