Completed
Push — master ( f445a9...55498a )
by Camilo
02:27
created

Connect::setConnectionParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 5
    public function setConnectionParameters(Parameters $connectionParameters): self
37
    {
38 5
        $this->connectionParameters = $connectionParameters;
39 5
        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 2
    public function getConnectionParameters(): Parameters
49
    {
50 2
        if ($this->connectionParameters === null) {
51 1
            throw new NoConnectionParametersDefined('You must pass on the connection parameters before connecting');
52
        }
53
54 1
        return $this->connectionParameters;
55
    }
56
57
    /**
58
     * @return string
59
     * @throws \OutOfRangeException
60
     */
61 1
    public function createVariableHeader(): string
62
    {
63 1
        $bitString = $this->createUTF8String('MQTT'); // Connect MUST begin with MQTT
64 1
        $bitString .= $this->connectionParameters->getProtocolVersionBinaryRepresentation(); // Protocol level
65 1
        $bitString .= \chr($this->connectionParameters->getFlags());
66 1
        $bitString .= Utilities::convertNumberToBinaryString($this->connectionParameters->keepAlivePeriod);
67 1
        return $bitString;
68
    }
69
70 3
    public function createPayload(): string
71
    {
72 3
        $output = '';
73
        // The order in a connect string is clientId first
74 3
        if ($this->connectionParameters->getClientId() !== '') {
75 3
            $output .= $this->createUTF8String($this->connectionParameters->getClientId());
76
        }
77
78
        // Then the willTopic if it is set
79 3
        if ($this->connectionParameters->getWillTopic() !== '') {
80 1
            $output .= $this->createUTF8String($this->connectionParameters->getWillTopic());
81
        }
82
83
        // The willMessage will come next
84 3
        if ($this->connectionParameters->getWillMessage() !== '') {
85 1
            $output .= $this->createUTF8String($this->connectionParameters->getWillMessage());
86
        }
87
88
        // If the username is set, it will come next
89 3
        if ($this->connectionParameters->getUsername() !== '') {
90 1
            $output .= $this->createUTF8String($this->connectionParameters->getUsername());
91
        }
92
93
        // And finally the password as last parameter
94 3
        if ($this->connectionParameters->getPassword() !== '') {
95 2
            if ($this->connectionParameters->getUsername() === '') {
96 1
                throw new MustProvideUsername('A password can not be set without a username! Please set username');
97
            }
98 1
            $output .= $this->createUTF8String($this->connectionParameters->getPassword());
99
        }
100
101 2
        return $output;
102
    }
103
104 1
    public function shouldExpectAnswer(): bool
105
    {
106 1
        return true;
107
    }
108
}
109