Test Failed
Push — master ( 98ddc8...4fb840 )
by Adam
01:45
created

PackagesExtension::loadConfiguration()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 28.1714

Importance

Changes 0
Metric Value
dl 0
loc 80
ccs 1
cts 40
cp 0.025
rs 8.1252
c 0
b 0
f 0
cc 5
nc 8
nop 0
crap 28.1714

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
 * PackagesExtension.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Packages!
9
 * @subpackage     DI
10
 * @since          1.0.0
11
 *
12
 * @date           27.05.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Packages\DI;
18
19
use Nette;
20
use Nette\DI;
21
22
use IPub\Packages;
23
use IPub\Packages\Commands;
24
use IPub\Packages\Helpers;
25
use IPub\Packages\Loaders;
26
use IPub\Packages\Repository;
27
28
/**
29
 * Packages extension container
30
 *
31
 * @package        iPublikuj:Packages!
32
 * @subpackage     DI
33
 *
34
 * @author         Adam Kadlec <[email protected]>
35
 */
36 1
final class PackagesExtension extends DI\CompilerExtension
37
{
38
	/**
39
	 * Extension default configuration
40
	 *
41
	 * @var array
42
	 */
43
	private $defaults = [
44
		'path'       => NULL,                        // Paths where to search for packages
45
		'dirs'       => [                            // Define path to folders
46
			'configDir' => '%appDir%/config',        // Path where is stored app configuration
47
			'vendorDir' => '%appDir%/../vendor',     // Path to composer vendor folder
48
			'tempDir'   => '%tempDir%',              // Path to temporary folder
49
		],
50
		'configFile' => 'config.neon',               // Filename with enabled packages extensions
51
		'loader'     => [
52
			'packageFiles' => [
53
				'package.php',
54
			],
55
		],
56
		'sources'    => [],
57
	];
58
59
	/**
60
	 * @return void
61
	 */
62
	public function loadConfiguration() : void
63
	{
64
		// Merge extension default config
65 1
		$this->setConfig(DI\Config\Helpers::merge($this->config, $this->getContainerBuilder()->expand($this->defaults)));
66
67
		// Get container builder
68
		$builder = $this->getContainerBuilder();
69
		// Get extension configuration
70
		$configuration = $this->getConfig();
71
72
		/**
73
		 * Load packages configuration
74
		 */
75
76
		$builder->parameters['packages'] = [];
77
78
		if (is_file($configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php')) {
79
			$packages = require $configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php';
80
81
			foreach ($packages as $name => $data) {
82
				$builder->parameters['packages'][$name] = $data;
83
			}
84
		}
85
86
		/**
87
		 * Register services
88
		 */
89
90
		$builder->addDefinition($this->prefix('loader'))
91
			->setType(Loaders\Loader::class)
92
			->setArguments([
93
				'packageFiles'    => $configuration['loader']['packageFiles'],
94
				'metadataSources' => $configuration['sources'],
95
				'vendorDir'       => $configuration['dirs']['vendorDir'],
96
			])
97
			->addTag('cms.packages');
98
99
		$repository = $builder->addDefinition($this->prefix('repository'))
100
			->setType(Repository\Repository::class)
101
			->addTag('cms.packages');
102
103
		if ($configuration['path']) {
104
			$repository->addSetup('addPath', [$configuration['path']]);
105
		}
106
107
		$builder->addDefinition($this->prefix('manager'))
108
			->setType(Packages\PackagesManager::class)
109
			->setArguments([
110
				'vendorDir' => $configuration['dirs']['vendorDir'],
111
				'configDir' => $configuration['dirs']['configDir'],
112
			])
113
			->addTag('cms.packages');
114
115
		$builder->addDefinition($this->prefix('pathResolver'))
116
			->setType(Helpers\PathResolver::class)
117
			->addTag('cms.packages');
118
119
		$builder->addDefinition($this->prefix('scripts.configuration'))
120
			->setType(Packages\Scripts\ConfigurationScript::class)
121
			->setArguments([
122
				'configDir'  => $configuration['dirs']['configDir'],
123
				'configFile' => $configuration['configFile'],
124
			])
125
			->addTag('cms.packages');
126
127
		// Define all console commands
128
		$commands = [
129
			'packagesSync'     => Commands\SyncCommand::class,
130
			'packagesList'     => Commands\ListCommand::class,
131
			'packageEnable'    => Commands\EnableCommand::class,
132
			'packageDisable'   => Commands\DisableCommand::class,
133
			'packageInstall'   => Commands\InstallCommand::class,
134
			'packageUninstall' => Commands\UninstallCommand::class,
135
		];
136
137
		foreach ($commands as $name => $cmd) {
138
			$builder->addDefinition($this->prefix('commands' . lcfirst($name)))
139
				->setType($cmd);
140
		}
141
	}
142
143
	/**
144
	 * {@inheritdoc}
145
	 */
146
	public function beforeCompile() : void
147
	{
148
		parent::beforeCompile();
149
150
		// Get container builder
151
		$builder = $this->getContainerBuilder();
152
153
		// Get packages manager
154
		$manager = $builder->getDefinition($this->prefix('manager'));
155
156
		foreach ($builder->findByType(Packages\Scripts\IScript::class) as $serviceDefinition) {
157
			$manager->addSetup('addScript', [$serviceDefinition->getType(), $serviceDefinition]);
158
		}
159
	}
160
161
	/**
162
	 * @param Nette\Configurator $config
163
	 * @param string $extensionName
164
	 *
165
	 * @return void
166
	 */
167
	public static function register(Nette\Configurator $config, string $extensionName = 'packages') : void
168
	{
169 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
170 1
			$compiler->addExtension($extensionName, new PackagesExtension());
171 1
		};
172 1
	}
173
}
174