Completed
Push — master ( a3854a...587a55 )
by Adam
01:45
created

MQTTClientExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 115
ccs 40
cts 45
cp 0.8889
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B loadConfiguration() 0 68 5
A register() 0 6 1
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\PhpGenerator as Code;
22
23
use BinSoul\Net\Mqtt;
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\Logger;
33
34
/**
35
 * MQTT client extension container
36
 *
37
 * @package        iPublikuj:MQTTClient!
38
 * @subpackage     DI
39
 *
40
 * @author         Adam Kadlec <[email protected]>
41
 *
42
 * @method DI\ContainerBuilder getContainerBuilder()
43
 * @method array getConfig(array $default)
44
 * @method string prefix($id)
45
 */
46 1
final class MQTTClientExtension extends DI\CompilerExtension
47
{
48
	/**
49
	 * @var array
50
	 */
51
	private $defaults = [
52
		'broker'     => [
53
			'httpHost' => NULL,
54
			'port'     => 1883,
55
			'address'  => NULL,
56
			'dns'      => [
57
				'enable'  => TRUE,
58
				'address' => '8.8.8.8',
59
			],
60
			'secured'  => [
61
				'enable'      => FALSE,
62
				'sslSettings' => [],
63
			],
64
		],
65
		'connection' => [
66
			'username'  => '',
67
			'password'  => '',
68
			'clientID'  => '',
69
			'keepAlive' => 60,
70
			'protocol'  => 4,
71
			'clean'     => TRUE,
72
		],
73
		'loop'       => NULL,
74
	];
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79
	public function loadConfiguration()
80
	{
81
		// Get container builder
82 1
		$builder = $this->getContainerBuilder();
83
84
		// Merge extension default config
85 1
		$this->setConfig(DI\Config\Helpers::merge($this->config, DI\Helpers::expand($this->defaults, $builder->parameters)));
86
87
		// Get extension configuration
88 1
		$configuration = $this->getConfig();
89
90 1
		if ($configuration['loop'] === NULL) {
91 1
			if ($builder->getByType(React\EventLoop\LoopInterface::class) === NULL) {
92 1
				$loop = $builder->addDefinition($this->prefix('client.loop'))
93 1
					->setType(React\EventLoop\LoopInterface::class)
94 1
					->setFactory('React\EventLoop\Factory::create');
95
96
			} else {
97 1
				$loop = $builder->getDefinitionByType(React\EventLoop\LoopInterface::class);
98
			}
99
100
		} else {
101
			$loop = $builder->getDefinition(ltrim($configuration['loop'], '@'));
102
		}
103
104 1
		$connection = new Mqtt\DefaultConnection(
105 1
			$configuration['connection']['username'],
106 1
			$configuration['connection']['password'],
107 1
			NULL,
108 1
			$configuration['connection']['clientID'],
109 1
			$configuration['connection']['keepAlive'],
110 1
			$configuration['connection']['protocol'],
111 1
			$configuration['connection']['clean']
112
		);
113
114 1
		$configuration = new Client\Configuration(
115 1
			$configuration['broker']['httpHost'],
116 1
			$configuration['broker']['port'],
117 1
			$configuration['broker']['address'],
118 1
			$configuration['broker']['dns']['enable'],
119 1
			$configuration['broker']['dns']['address'],
120 1
			$configuration['broker']['secured']['enable'],
121 1
			$configuration['broker']['secured']['sslSettings'],
122 1
			$connection
123
		);
124
125 1
		if ($builder->findByType(Log\LoggerInterface::class) === []) {
126 1
			$builder->addDefinition($this->prefix('server.logger'))
127 1
				->setType(Logger\Console::class);
128
		}
129
130 1
		$builder->addDefinition($this->prefix('client.client'))
131 1
			->setType(Client\Client::class)
132 1
			->setArguments([
133 1
				'eventLoop'     => $loop,
134 1
				'configuration' => $configuration,
135
			]);
136
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 1
	}
147
148
	/**
149
	 * @param Nette\Configurator $config
150
	 * @param string $extensionName
151
	 *
152
	 * @return void
153
	 */
154
	public static function register(Nette\Configurator $config, string $extensionName = 'mqttClient')
155
	{
156
		$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) {
157
			$compiler->addExtension($extensionName, new MQTTClientExtension);
158
		};
159
	}
160
}
161