Completed
Push — master ( 129581...a310fa )
by Damian
03:27
created

FilesystemQueue::removeFirstLine()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.0029

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 6
nop 0
dl 0
loc 30
ccs 22
cts 23
cp 0.9565
crap 6.0029
rs 8.9457
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Initx\Querabilis\Driver;
4
5
use Initx\Querabilis\Envelope;
6
use Initx\Querabilis\Exception\IllegalStateException;
7
use Initx\Querabilis\Exception\NoSuchElementException;
8
use Initx\Querabilis\Queue;
9
use JMS\Serializer\SerializerInterface;
10
11
final class FilesystemQueue implements Queue
12
{
13
    use HasFallbackSerializer;
14
15
    /**
16
     * @var string
17
     */
18
    private $path;
19
20
    /**
21
     * @var SerializerInterface
22
     */
23
    private $serializer;
24
25 11
    public function __construct(string $path, ?SerializerInterface $serializer = null)
26
    {
27 11
        $this->serializer = $this->fallbackSerializer($serializer);
28 11
        $this->path = $path;
29 11
    }
30
31 5
    public function add(Envelope $envelope): bool
32
    {
33 5
        if (!$this->offer($envelope)) {
34
            $this->throwItIsNotWriteable();
35
        }
36
37 4
        return true;
38
    }
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
            $this->throwItIsNotWriteable();
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
            $this->throwItNotExists();
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
            $this->throwItNotExists();
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 2
    private function throwItIsNotWriteable(): void
145
    {
146 2
        throw new IllegalStateException("Could not write to file: {$this->path}");
147
    }
148
149 1
    private function throwItNotExists(): void
150
    {
151 1
        throw new IllegalStateException("File $this->path not exists");
152
    }
153
}
154