Passed
Push — master ( d27c94...6659fb )
by Camilo
02:22
created

Connect::createPayload()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 15
cts 15
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 48
nop 0
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Protocol;
6
7
use unreal4u\MQTT\Exceptions\Connect\NoConnectionParametersDefined;
8
use unreal4u\MQTT\Exceptions\MustProvideUsername;
9
use unreal4u\MQTT\Internals\ProtocolBase;
10
use unreal4u\MQTT\Internals\WritableContent;
11
use unreal4u\MQTT\Internals\WritableContentInterface;
12
use unreal4u\MQTT\Protocol\Connect\Parameters;
13
use unreal4u\MQTT\Utilities;
14
15
/**
16
 * After a Network Connection is established by a Client to a Server, the first Packet sent from the Client to the
17
 * Server MUST be a CONNECT Packet
18
 */
19
final class Connect extends ProtocolBase implements WritableContentInterface
20
{
21
    use WritableContent;
22
23
    const CONTROL_PACKET_VALUE = 1;
24
25
    /**
26
     * @var Parameters
27
     */
28
    private $connectionParameters;
29
30
    /**
31
     * Saves the mandatory connection parameters onto this object
32
     * @param Parameters $connectionParameters
33
     *
34
     * @return Connect
35
     */
36 4
    public function setConnectionParameters(Parameters $connectionParameters): Connect
37
    {
38 4
        $this->connectionParameters = $connectionParameters;
39 4
        return $this;
40
    }
41
42
    /**
43
     * Get the connection parameters from the private object
44
     *
45
     * @return Parameters
46
     * @throws \unreal4u\MQTT\Exceptions\Connect\NoConnectionParametersDefined
47
     */
48
    public function getConnectionParameters(): Parameters
49
    {
50
        if ($this->connectionParameters === null) {
51
            throw new NoConnectionParametersDefined('You must pass on the connection parameters before connecting');
52
        }
53
54
        return $this->connectionParameters;
55
    }
56
57 1
    public function createVariableHeader(): string
58
    {
59 1
        $bitString = $this->createUTF8String('MQTT'); // Connect MUST begin with MQTT
60 1
        $bitString .= $this->getProtocolLevel(); // Protocol level
61 1
        $bitString .= \chr($this->connectionParameters->getFlags());
62 1
        $bitString .= Utilities::convertNumberToBinaryString($this->connectionParameters->keepAlivePeriod);
63 1
        return $bitString;
64
    }
65
66 3
    public function createPayload(): string
67
    {
68 3
        $output = '';
69
        // The order in a connect string is clientId first
70 3
        if ($this->connectionParameters->getClientId() !== '') {
71 3
            $output .= $this->createUTF8String($this->connectionParameters->getClientId());
72
        }
73
74
        // Then the willTopic if it is set
75 3
        if ($this->connectionParameters->getWillTopic() !== '') {
76 1
            $output .= $this->createUTF8String($this->connectionParameters->getWillTopic());
77
        }
78
79
        // The willMessage will come next
80 3
        if ($this->connectionParameters->getWillMessage() !== '') {
81 1
            $output .= $this->createUTF8String($this->connectionParameters->getWillMessage());
82
        }
83
84
        // If the username is set, it will come next
85 3
        if ($this->connectionParameters->getUsername() !== '') {
86 1
            $output .= $this->createUTF8String($this->connectionParameters->getUsername());
87
        }
88
89
        // And finally the password as last parameter
90 3
        if ($this->connectionParameters->getPassword() !== '') {
91 2
            if ($this->connectionParameters->getUsername() === '') {
92 1
                throw new MustProvideUsername('A password can not be set without a username! Please set username');
93
            }
94 1
            $output .= $this->createUTF8String($this->connectionParameters->getPassword());
95
        }
96
97 2
        return $output;
98
    }
99
100
    public function shouldExpectAnswer(): bool
101
    {
102
        return true;
103
    }
104
}
105