Completed
Push — master ( bb30b8...368962 )
by Camilo
02:11
created

EventManager::getObject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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

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