Passed
Pull Request — master (#14)
by Camilo
01:32
created

PacketIdentifierStack::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Application;
6
7
use unreal4u\MQTT\Internals\WritableContentInterface;
8
9
/**
10
 * QoS Level 1 and 2 messages may come in out of order. Stack them so that we can find the appropriate response
11
 */
12
final class PacketIdentifierStack
13
{
14
    /**
15
     * @var array
16
     */
17
    private $stack = [];
18
19
    /**
20
     * Adds a packet identifiable object to the stack
21
     *
22
     * @param WritableContentInterface $writableContent
23
     * @return $this
24
     */
25
    public function add(WritableContentInterface $writableContent): self
26
    {
27
        // TODO Replace with some way of retrieving the packet identifier
28
        $this->stack[$writableContent->hasActivePacketIdentifier()] = $writableContent;
29
        return $this;
30
    }
31
32
    /**
33
     * Searches for a specific packetIdentifier in the stack
34
     *
35
     * @param int $packetIdentifier
36
     * @return bool
37
     */
38
    public function search(int $packetIdentifier): bool
39
    {
40
        // TODO Search for it or throw an Exception
41
42
        $this->delete($packetIdentifier);
43
        return true;
44
    }
45
46
    /**
47
     * TODO If this is a oneliner, maybe merge with above? Check this out later on
48
     *
49
     * @param int $packetIdentifier
50
     * @return $this
51
     */
52
    private function delete(int $packetIdentifier): self
53
    {
54
        unset($this->stack[$packetIdentifier]);
55
        return $this;
56
    }
57
}
58