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

MQTTClientExtension::loadConfiguration()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 7.0006

Importance

Changes 0
Metric Value
dl 0
loc 68
ccs 42
cts 43
cp 0.9767
rs 7.7648
c 0
b 0
f 0
cc 7
nc 24
nop 0
crap 7.0006

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * MQTTClientExtension.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     DI
10
 * @since          1.0.0
11
 *
12
 * @date           12.03.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\MQTTClient\DI;
18
19
use Nette;
20
use Nette\DI;
21
use Nette\Schema;
22
23
use Symfony\Component\EventDispatcher;
24
25
use React;
26
27
use Psr\Log;
28
29
use IPub\MQTTClient;
30
use IPub\MQTTClient\Client;
31
use IPub\MQTTClient\Commands;
32
use IPub\MQTTClient\Configuration;
33
use IPub\MQTTClient\Events;
34
use IPub\MQTTClient\Logger;
35
36
/**
37
 * MQTT client extension container
38
 *
39
 * @package        iPublikuj:MQTTClient!
40
 * @subpackage     DI
41
 *
42
 * @author         Adam Kadlec <[email protected]>
43
 */
44 1
final class MQTTClientExtension extends DI\CompilerExtension
45
{
46
	/**
47
	 * {@inheritdoc}
48
	 */
49
	public function getConfigSchema() : Schema\Schema
50
	{
51 1
		return Schema\Expect::structure([
52 1
			'broker'     => Schema\Expect::structure([
53 1
				'httpHost' => Schema\Expect::string()->nullable(),
54 1
				'port'     => Schema\Expect::int(1883),
55 1
				'address'  => Schema\Expect::string('127.0.0.1'),
56 1
				'dns'      => Schema\Expect::structure([
57 1
					'enable'  => Schema\Expect::bool(TRUE),
58 1
					'address' => Schema\Expect::string('8.8.8.8'),
59
				]),
60 1
				'secured'  => Schema\Expect::structure([
61 1
					'enable'      => Schema\Expect::bool(TRUE),
62 1
					'sslSettings' => Schema\Expect::array([]),
63
				]),
64
			]),
65 1
			'connection' => Schema\Expect::structure([
66 1
				'username'  => Schema\Expect::string(''),
67 1
				'password'  => Schema\Expect::string(''),
68 1
				'clientID'  => Schema\Expect::string(''),
69 1
				'keepAlive' => Schema\Expect::int(60),
70 1
				'protocol'  => Schema\Expect::int(4),
71 1
				'clean'     => Schema\Expect::bool(TRUE),
72
			]),
73 1
			'loop'       => Schema\Expect::anyOf(Schema\Expect::string(), Schema\Expect::type(DI\Definitions\Statement::class))->nullable(),
74
		]);
75
	}
76
77
	/**
78
	 * {@inheritdoc}
79
	 */
80
	public function loadConfiguration()
81
	{
82 1
		$builder = $this->getContainerBuilder();
83 1
		$configuration = $this->getConfig();
84
85 1
		if ($configuration->loop === NULL) {
86 1
			if ($builder->getByType(React\EventLoop\LoopInterface::class) === NULL) {
87 1
				$loop = $builder->addDefinition($this->prefix('client.loop'))
88 1
					->setType(React\EventLoop\LoopInterface::class)
89 1
					->setFactory('React\EventLoop\Factory::create');
90
91
			} else {
92 1
				$loop = $builder->getDefinitionByType(React\EventLoop\LoopInterface::class);
93
			}
94
95
		} else {
96
			$loop = is_string($configuration->loop) ? new DI\Definitions\Statement($configuration->loop) : $configuration->loop;
97
		}
98
99 1
		$connection = $builder->addDefinition($this->prefix('client.configuration.connection'))
100 1
			->setType(Configuration\Connection::class)
101 1
			->setArguments([
102 1
				'username'  => $configuration->connection->username,
103 1
				'password'  => $configuration->connection->password,
104
				'will'      => NULL,
105 1
				'clientID'  => $configuration->connection->clientID,
106 1
				'keepAlive' => $configuration->connection->keepAlive,
107 1
				'protocol'  => $configuration->connection->protocol,
108 1
				'clean'     => $configuration->connection->clean,
109
			]);
110
111 1
		$brokerConfiguration = $builder->addDefinition($this->prefix('client.configuration'))
112 1
			->setType(Configuration\Broker::class)
113 1
			->setArguments([
114 1
				'httpHost'    => $configuration->broker->httpHost,
115 1
				'port'        => $configuration->broker->port,
116 1
				'address'     => $configuration->broker->address,
117 1
				'enableDNS'   => $configuration->broker->dns->enable,
118 1
				'dnsAddress'  => $configuration->broker->dns->address,
119 1
				'enableSSL'   => $configuration->broker->secured->enable,
120 1
				'sslSettings' => $configuration->broker->secured->sslSettings,
121 1
				$connection,
122
			]);
123
124 1
		if ($builder->findByType(Log\LoggerInterface::class) === []) {
125 1
			$builder->addDefinition($this->prefix('server.logger'))
126 1
				->setType(Logger\Console::class);
127
		}
128
129 1
		$builder->addDefinition($this->prefix('client.client'))
130 1
			->setType(Client\Client::class)
131 1
			->setArguments([
132 1
				'eventLoop'     => $loop,
133 1
				'configuration' => $brokerConfiguration,
134
			]);
135
136 1
		if (class_exists('Symfony\Component\Console\Command\Command')) {
137
			// Define all console commands
138
			$commands = [
139 1
				'client' => Commands\ClientCommand::class,
140
			];
141
142 1
			foreach ($commands as $name => $cmd) {
143 1
				$builder->addDefinition($this->prefix('commands.' . lcfirst($name)))
144 1
					->setType($cmd);
145
			}
146
		}
147 1
	}
148
149
	/**
150
	 * {@inheritdoc}
151
	 */
152
	public function beforeCompile()
153
	{
154 1
		parent::beforeCompile();
155
156 1
		$builder = $this->getContainerBuilder();
157
158 1
		if (interface_exists('Symfony\Component\EventDispatcher\EventDispatcherInterface')) {
159 1
			$dispatcher = $builder->getDefinition($builder->getByType(EventDispatcher\EventDispatcherInterface::class));
160
161 1
			$client = $builder->getDefinition($builder->getByType(Client\Client::class));
162 1
			assert($client instanceof DI\Definitions\ServiceDefinition);
163
164 1
			$client->addSetup('?->onStart[] = function() {?->dispatch(new ?(...func_get_args()));}', [
165 1
				'@self',
166 1
				$dispatcher,
167 1
				new Nette\PhpGenerator\PhpLiteral(Events\StartEvent::class),
168
			]);
169 1
			$client->addSetup('?->onOpen[] = function() {?->dispatch(new ?(...func_get_args()));}', [
170 1
				'@self',
171 1
				$dispatcher,
172 1
				new Nette\PhpGenerator\PhpLiteral(Events\OpenEvent::class),
173
			]);
174 1
			$client->addSetup('?->onConnect[] = function() {?->dispatch(new ?(...func_get_args()));}', [
175 1
				'@self',
176 1
				$dispatcher,
177 1
				new Nette\PhpGenerator\PhpLiteral(Events\ConnectEvent::class),
178
			]);
179 1
			$client->addSetup('?->onDisconnect[] = function() {?->dispatch(new ?(...func_get_args()));}', [
180 1
				'@self',
181 1
				$dispatcher,
182 1
				new Nette\PhpGenerator\PhpLiteral(Events\DisconnectEvent::class),
183
			]);
184 1
			$client->addSetup('?->onClose[] = function() {?->dispatch(new ?(...func_get_args()));}', [
185 1
				'@self',
186 1
				$dispatcher,
187 1
				new Nette\PhpGenerator\PhpLiteral(Events\CloseEvent::class),
188
			]);
189 1
			$client->addSetup('?->onPing[] = function() {?->dispatch(new ?(...func_get_args()));}', [
190 1
				'@self',
191 1
				$dispatcher,
192 1
				new Nette\PhpGenerator\PhpLiteral(Events\PingEvent::class),
193
			]);
194 1
			$client->addSetup('?->onPong[] = function() {?->dispatch(new ?(...func_get_args()));}', [
195 1
				'@self',
196 1
				$dispatcher,
197 1
				new Nette\PhpGenerator\PhpLiteral(Events\PongEvent::class),
198
			]);
199 1
			$client->addSetup('?->onPublish[] = function() {?->dispatch(new ?(...func_get_args()));}', [
200 1
				'@self',
201 1
				$dispatcher,
202 1
				new Nette\PhpGenerator\PhpLiteral(Events\PublishEvent::class),
203
			]);
204 1
			$client->addSetup('?->onSubscribe[] = function() {?->dispatch(new ?(...func_get_args()));}', [
205 1
				'@self',
206 1
				$dispatcher,
207 1
				new Nette\PhpGenerator\PhpLiteral(Events\SubscribeEvent::class),
208
			]);
209 1
			$client->addSetup('?->onUnsubscribe[] = function() {?->dispatch(new ?(...func_get_args()));}', [
210 1
				'@self',
211 1
				$dispatcher,
212 1
				new Nette\PhpGenerator\PhpLiteral(Events\UnsubscribeEvent::class),
213
			]);
214 1
			$client->addSetup('?->onMessage[] = function() {?->dispatch(new ?(...func_get_args()));}', [
215 1
				'@self',
216 1
				$dispatcher,
217 1
				new Nette\PhpGenerator\PhpLiteral(Events\MessageEvent::class),
218
			]);
219 1
			$client->addSetup('?->onWarning[] = function() {?->dispatch(new ?(...func_get_args()));}', [
220 1
				'@self',
221 1
				$dispatcher,
222 1
				new Nette\PhpGenerator\PhpLiteral(Events\WarningEvent::class),
223
			]);
224 1
			$client->addSetup('?->onError[] = function() {?->dispatch(new ?(...func_get_args()));}', [
225 1
				'@self',
226 1
				$dispatcher,
227 1
				new Nette\PhpGenerator\PhpLiteral(Events\ErrorEvent::class),
228
			]);
229
		}
230 1
	}
231
232
	/**
233
	 * @param Nette\Configurator $config
234
	 * @param string $extensionName
235
	 *
236
	 * @return void
237
	 */
238
	public static function register(
239
		Nette\Configurator $config,
240
		string $extensionName = 'mqttClient'
241
	) :void {
242 1
		$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) {
243 1
			$compiler->addExtension($extensionName, new MQTTClientExtension);
244 1
		};
245 1
	}
246
}
247