Passed
Push — master ( f12820...2e6535 )
by Damian
02:05
created

FilesystemQueue::offer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
    }
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...
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
    }
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...
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