Completed
Push — master ( 474237...d94a2b )
by Camilo
02:11
created

EventManager::analyzeHeaders()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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

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

99
                'rawMQTTHeaders' => /** @scrutinizer ignore-call */ str2bin($rawMQTTHeaders),
Loading history...
100
            ]);
101
            throw new \DomainException(sprintf('Invalid control packet found (%d)', $controlPacketType));
102
        }
103
104
        return $readableContent;
105
    }
106
}
107