1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\DataTypes; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\Exceptions\InvalidBrokerPort; |
8
|
|
|
use unreal4u\MQTT\Exceptions\InvalidBrokerProtocol; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This Value Object will always contain a valid broker port. |
12
|
|
|
*/ |
13
|
|
|
final class BrokerPort |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* This field indicates the level of assurance for delivery of an Application Message. Can be 0, 1 or 2 |
17
|
|
|
* |
18
|
|
|
* 0: At most once delivery (default) |
19
|
|
|
* 1: At least once delivery |
20
|
|
|
* 2: Exactly once delivery |
21
|
|
|
* |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $brokerPort; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* If we should connect through SSL or TLS, this parameter should be set to true |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $transmissionProtocol; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* List of valid protocols that this package implements (or the broker can implement) |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
private static $validProtocols = [ |
37
|
|
|
'tcp', |
38
|
|
|
'ssl', |
39
|
|
|
'tlsv1.0', |
40
|
|
|
'tlsv1.1', |
41
|
|
|
'tlsv1.2', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* BrokerPort constructor. |
46
|
|
|
* |
47
|
|
|
* @param int $brokerPort |
48
|
|
|
* @param string $transmissionProtocol |
49
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidBrokerProtocol |
50
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidBrokerPort |
51
|
|
|
*/ |
52
|
31 |
|
public function __construct(int $brokerPort = 1883, string $transmissionProtocol = 'tcp') |
53
|
|
|
{ |
54
|
31 |
|
if ($brokerPort > 65535 || $brokerPort < 1) { |
55
|
2 |
|
throw new InvalidBrokerPort(sprintf( |
56
|
2 |
|
'The provided broker port is invalid. Valid values are between 1 and 65535 (Provided: %d)', |
57
|
|
|
$brokerPort |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
29 |
|
if (\in_array($transmissionProtocol, self::$validProtocols, true) === false) { |
62
|
1 |
|
throw new InvalidBrokerProtocol(sprintf( |
63
|
1 |
|
'You must provide a valid protocol (Provided: %s)', |
64
|
|
|
$transmissionProtocol |
65
|
|
|
)); |
66
|
|
|
} |
67
|
|
|
|
68
|
28 |
|
$this->brokerPort = $brokerPort; |
69
|
28 |
|
$this->transmissionProtocol = $transmissionProtocol; |
70
|
28 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets the current broker port |
74
|
|
|
* |
75
|
|
|
* @return int |
76
|
|
|
*/ |
77
|
6 |
|
public function getBrokerPort(): int |
78
|
|
|
{ |
79
|
6 |
|
return $this->brokerPort; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
8 |
|
public function getTransmissionProtocol(): string |
86
|
|
|
{ |
87
|
8 |
|
return $this->transmissionProtocol; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|