EventManager::analyzeHeaders()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 9.7666
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Internals;
6
7
use unreal4u\MQTT\Application\EmptyReadableResponse;
8
use unreal4u\MQTT\Protocol\ConnAck;
9
use unreal4u\MQTT\Protocol\PingResp;
10
use unreal4u\MQTT\Protocol\PubAck;
11
use unreal4u\MQTT\Protocol\PubComp;
12
use unreal4u\MQTT\Protocol\Publish;
13
use unreal4u\MQTT\Protocol\PubRec;
14
use unreal4u\MQTT\Protocol\PubRel;
15
use unreal4u\MQTT\Protocol\SubAck;
16
use unreal4u\MQTT\Protocol\UnsubAck;
17
18
/**
19
 * Is able to load in an incoming event and handle it with properly, providing the ability to actively validate
20
 */
21
final class EventManager extends ProtocolBase
22
{
23
    /**
24
     * Current object in string format
25
     * @var string
26
     */
27
    private $currentObjectType = '';
28
29
    /**
30
     * A list of all Readable objects that this class may instantiate at some point
31
     *
32
     * @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349209
33
     * @var string[]
34
     */
35
    private static $readableObjects = [
36
        2 => ConnAck::class,
37
        3 => Publish::class,
38
        4 => PubAck::class,
39
        5 => PubRec::class,
40
        6 => PubRel::class,
41
        7 => PubComp::class,
42
        9 => SubAck::class,
43
        11 => UnsubAck::class,
44
        13 => PingResp::class,
45
    ];
46
47
    /**
48
     * Not used in this class but handy to have, will maybe be used in the future?
49
     *
50
     * @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349209
51
     * @var string[]
52
     */
53
    /*
54
    private static $writableObjects = [
55
        1 => Connect::class,
56
        3 => Publish::class,
57
        4 => PubAck::class,
58
        5 => PubRec::class,
59
        6 => PubRel::class,
60
        7 => PubComp::class,
61
        8 => Subscribe::class,
62
        10 => Unsubscribe::class,
63
        12 => PingReq::class,
64
        14 => Disconnect::class,
65
    ];
66
    */
67
68
    /**
69
     * Will check within all the Readable objects whether one of those is the correct packet we are looking for
70
     *
71
     * @param string $rawMQTTHeaders Arbitrary size of minimum 1 incoming byte(s)
72
     * @param ClientInterface $client Used if the object itself needs to process some more stuff
73
     * @return ReadableContentInterface
74
     * @throws \DomainException
75
     */
76 6
    public function analyzeHeaders(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface
77
    {
78 6
        if ($rawMQTTHeaders === '') {
79 1
            $this->logger->debug('Empty headers, returning an empty object');
80 1
            return new EmptyReadableResponse($this->logger);
81
        }
82 5
        $controlPacketType = \ord($rawMQTTHeaders[0]) >> 4;
83
84 5
        if (isset(self::$readableObjects[$controlPacketType])) {
85 4
            $this->currentObjectType = self::$readableObjects[$controlPacketType];
86 4
            $this->logger->info('Found a matching object, instantiating', [
87 4
                'type' => $this->currentObjectType,
88 4
                'controlPacketNumber' => $controlPacketType,
89
            ]);
90
            /** @var ReadableContentInterface $readableContent */
91 4
            $readableContent = new $this->currentObjectType($this->logger);
92 4
            $readableContent->instantiateObject($rawMQTTHeaders, $client);
93
        } else {
94 1
            $this->logger->error('Invalid control packet type found', ['controlPacketType' => $controlPacketType]);
95 1
            throw (new \DomainException(sprintf('Invalid control packet found (%d)', $controlPacketType)));
96
        }
97
98 4
        return $readableContent;
99
    }
100
}
101