Completed
Pull Request — master (#6)
by Adam
07:08
created

Broker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Broker.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:MQTTClient!
9
 * @subpackage     Client
10
 * @since          1.0.0
11
 *
12
 * @date           14.03.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\MQTTClient\Configuration;
18
19
use Nette;
20
21
use IPub\MQTTClient\Exceptions;
22
23
/**
24
 * MQTT client broker configuration
25
 *
26
 * @package        iPublikuj:MQTTClient!
27
 * @subpackage     Client
28
 *
29
 * @author         Adam Kadlec <[email protected]>
30
 */
31 1
final class Broker
32
{
33
	/**
34
	 * Implement nette smart magic
35
	 */
36 1
	use Nette\SmartObject;
37
38
	/**
39
	 * @var string
40
	 */
41
	private $httpHost;
42
43
	/**
44
	 * @var int
45
	 */
46
	private $port;
47
48
	/**
49
	 * @var string
50
	 */
51
	private $address;
52
53
	/**
54
	 * @var bool
55
	 */
56
	private $enableDNS = TRUE;
57
58
	/**
59
	 * @var string
60
	 */
61
	private $dnsAddress;
62
63
	/**
64
	 * @var bool
65
	 */
66
	private $enableSSL = FALSE;
67
68
	/**
69
	 * @var array
70
	 */
71
	private $sslSettings = [];
72
73
	/**
74
	 * @var Connection
75
	 */
76
	private $connection;
77
78
	/**
79
	 * @param string|NULL $httpHost
80
	 * @param int $port
81
	 * @param string|NULL $address
82
	 * @param bool $enableDNS
83
	 * @param string $dnsAddress
84
	 * @param bool $enableSSL
85
	 * @param array $sslSettings
86
	 * @param Connection $connection
87
	 */
88
	public function __construct(
89
		string $httpHost = NULL,
90
		int $port = 1883,
91
		string $address = NULL,
92
		bool $enableDNS = TRUE,
93
		string $dnsAddress = '8.8.8.8',
94
		bool $enableSSL = FALSE,
95
		array $sslSettings,
96
		Connection $connection
97
	) {
98 1
		$this->httpHost = $httpHost;
99 1
		$this->port = $port;
100 1
		$this->address = $address;
101 1
		$this->enableDNS = $enableDNS;
102 1
		$this->dnsAddress = $dnsAddress;
103 1
		$this->enableSSL = $enableSSL;
104 1
		$this->sslSettings = $sslSettings;
105 1
		$this->connection = $connection;
106 1
	}
107
108
	/**
109
	 * @return string
110
	 *
111
	 * @throws Exceptions\InvalidStateException
112
	 */
113
	public function getUri() : string
114
	{
115
		if ($this->httpHost !== NULL) {
116
			return $this->httpHost . ':' . $this->port;
117
118
		} elseif ($this->address !== NULL) {
119
			return $this->address . ':' . $this->port;
120
		}
121
122
		throw new Exceptions\InvalidStateException('Broker http host or address is not specified');
123
	}
124
125
	/**
126
	 * @return string|NULL
127
	 */
128
	public function getHttpHost() : ?string
129
	{
130
		return $this->httpHost;
131
	}
132
133
	/**
134
	 * @return int
135
	 */
136
	public function getPort() : int
137
	{
138
		return $this->port;
139
	}
140
141
	/**
142
	 * @return string|NULL
143
	 */
144
	public function getAddress() : ?string
145
	{
146
		return $this->address;
147
	}
148
149
	/**
150
	 * @return bool
151
	 */
152
	public function isDNSEnabled() : bool
153
	{
154 1
		return $this->enableDNS;
155
	}
156
157
	/**
158
	 * @return string
159
	 */
160
	public function getDNSAddress() : string
161
	{
162 1
		return $this->dnsAddress;
163
	}
164
165
	/**
166
	 * @return bool
167
	 */
168
	public function isSSLEnabled() : bool
169
	{
170 1
		return $this->enableSSL;
171
	}
172
173
	/**
174
	 * @return array
175
	 */
176
	public function getSSLConfiguration() : array
177
	{
178 1
		return $this->sslSettings;
179
	}
180
181
	/**
182
	 * @return Connection
183
	 */
184
	public function getConnection() : Connection
185
	{
186
		return $this->connection;
187
	}
188
}
189