Passed
Push — master ( 6791f9...01e57e )
by Adam
02:08
created

PackagesExtensions::loadConfiguration()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 78
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 5.025

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 36
cts 40
cp 0.9
rs 8.4316
c 0
b 0
f 0
cc 5
eloc 46
nc 8
nop 0
crap 5.025

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