Passed
Pull Request — master (#1)
by Adam
03:11
created

WebSocketsZMQExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 57
ccs 17
cts 22
cp 0.7727
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 28 2
A register() 0 6 1
1
<?php
2
/**
3
 * WebSocketsZMQExtension.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:WebSocketsZMQ!
9
 * @subpackage     DI
10
 * @since          1.0.0
11
 *
12
 * @date           01.03.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\WebSocketsZMQ\DI;
18
19
use Nette;
20
use Nette\DI;
21
22
use IPub;
23
use IPub\WebSocketsZMQ;
24
use IPub\WebSocketsZMQ\Consumer;
25
use IPub\WebSocketsZMQ\Pusher;
26
27
/**
28
 * WebSockets ZeroMQ extension container
29
 *
30
 * @package        iPublikuj:WebSocket!
31
 * @subpackage     DI
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 *
35
 * @method DI\ContainerBuilder getContainerBuilder()
36
 * @method array getConfig(array $defaults)
37
 * @method string prefix(string $name)
38
 */
39 1
final class WebSocketsZMQExtension extends DI\CompilerExtension
40
{
41
	/**
42
	 * @var array
43
	 */
44
	private $defaults = [
45
		'host'       => '127.0.0.1',
46
		'port'       => 5555,
47
		'persistent' => TRUE,
48
		'protocol'   => 'tcp',
49
	];
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54
	public function loadConfiguration()
55
	{
56 1
		parent::loadConfiguration();
57
58
		/** @var DI\ContainerBuilder $builder */
59 1
		$builder = $this->getContainerBuilder();
60
		/** @var array $configuration */
61 1
		if (method_exists($this, 'validateConfig')) {
62 1
			$configuration = $this->validateConfig($this->defaults);
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\CompilerExtension::validateConfig() has been deprecated with message: use getConfigSchema()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
63
		} else {
64
			$configuration = $this->getConfig($this->defaults);
65
		}
66
67 1
		$configuration = new WebSocketsZMQ\Configuration(
68 1
			$configuration['host'],
69 1
			$configuration['port'],
70 1
			$configuration['persistent'],
71 1
			$configuration['protocol']
72
		);
73
74 1
		$builder->addDefinition($this->prefix('consumer'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\Definitions\ServiceDefinition::setClass() has been deprecated with message: Use setType()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
75 1
			->setClass(Consumer\Consumer::class)
76 1
			->setArguments(['configuration' => $configuration]);
77
78 1
		$builder->addDefinition($this->prefix('pusher'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\Definitions\ServiceDefinition::setClass() has been deprecated with message: Use setType()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
79 1
			->setClass(Pusher\Pusher::class)
80 1
			->setArguments(['configuration' => $configuration]);
81 1
	}
82
83
	/**
84
	 * @param Nette\Configurator $config
85
	 * @param string $extensionName
86
	 *
87
	 * @return void
88
	 */
89
	public static function register(Nette\Configurator $config, string $extensionName = 'webSocketsZMQ')
90
	{
91
		$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) {
92
			$compiler->addExtension($extensionName, new WebSocketsZMQExtension());
93
		};
94
	}
95
}
96