Test Failed
Pull Request — master (#2)
by Adam
02:23
created

MQTTClientExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 118
ccs 36
cts 46
cp 0.7826
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B loadConfiguration() 0 70 6
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
		'console'    => FALSE,
75
	];
76
77
	/**
78
	 * {@inheritdoc}
79
	 */
80
	public function loadConfiguration()
81
	{
82
		// Get container builder
83 1
		$builder = $this->getContainerBuilder();
84
85
		// Merge extension default config
86 1
		$this->setConfig(DI\Config\Helpers::merge($this->config, DI\Helpers::expand($this->defaults, $builder->parameters)));
0 ignored issues
show
Bug introduced by
It seems like \Nette\DI\Config\Helpers... $builder->parameters)) targeting Nette\DI\Config\Helpers::merge() can also be of type string; however, Nette\DI\CompilerExtension::setConfig() does only seem to accept array|object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
87
88
		// Get extension configuration
89 1
		$configuration = $this->getConfig();
0 ignored issues
show
Bug introduced by
The call to getConfig() misses a required argument $default.

This check looks for function calls that miss required arguments.

Loading history...
90
91 1
		if ($configuration['loop'] === NULL) {
92 1
			if ($builder->getByType(React\EventLoop\LoopInterface::class) === NULL) {
93 1
				$loop = $builder->addDefinition($this->prefix('client.loop'))
94 1
					->setType(React\EventLoop\LoopInterface::class)
95 1
					->setFactory('React\EventLoop\Factory::create');
96
97
			} else {
98 1
				$loop = $builder->getDefinitionByType(React\EventLoop\LoopInterface::class);
99
			}
100
101
		} else {
102
			$loop = $builder->getDefinition(ltrim($configuration['loop'], '@'));
103
		}
104
105 1
		$connection = new Mqtt\DefaultConnection(
106 1
			$configuration['connection']['username'],
107 1
			$configuration['connection']['password'],
108 1
			NULL,
109 1
			$configuration['connection']['clientID'],
110 1
			$configuration['connection']['keepAlive'],
111 1
			$configuration['connection']['protocol'],
112 1
			$configuration['connection']['clean']
113
		);
114
115 1
		$configuration = new Client\Configuration(
116 1
			$configuration['broker']['httpHost'],
117 1
			$configuration['broker']['port'],
118 1
			$configuration['broker']['address'],
119 1
			$configuration['broker']['dns']['enable'],
120 1
			$configuration['broker']['dns']['address'],
121 1
			$configuration['broker']['secured']['enable'],
122 1
			$configuration['broker']['secured']['sslSettings'],
123 1
			$connection
124
		);
125
126 1
		if ($builder->findByType(Log\LoggerInterface::class) === []) {
127 1
			$builder->addDefinition($this->prefix('server.logger'))
128 1
				->setType(Logger\Console::class);
129
		}
130
131 1
		$builder->addDefinition($this->prefix('client.client'))
132 1
			->setType(Client\Client::class)
133 1
			->setArguments([
134 1
				'eventLoop'     => $loop,
135 1
				'configuration' => $configuration,
136
			]);
137
138 1
		if ($configuration['console'] === NULL) {
139
			// Define all console commands
140
			$commands = [
141
				'client' => Commands\ClientCommand::class,
142
			];
143
144
			foreach ($commands as $name => $cmd) {
145
				$builder->addDefinition($this->prefix('commands' . lcfirst($name)))
146
					->setType($cmd);
147
			}
148
		}
149
	}
150
151
	/**
152
	 * @param Nette\Configurator $config
153
	 * @param string $extensionName
154
	 *
155
	 * @return void
156
	 */
157
	public static function register(Nette\Configurator $config, string $extensionName = 'mqttClient')
158
	{
159
		$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) {
160
			$compiler->addExtension($extensionName, new MQTTClientExtension);
161
		};
162
	}
163
}
164