Test Failed
Push — master ( 25aa03...89c358 )
by Adam
03:53
created

PackagesExtension::loadConfiguration()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 8.1907
c 0
b 0
f 0
ccs 0
cts 39
cp 0
cc 5
nc 8
nop 0
crap 30

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
		// Get container builder
65
		$builder = $this->getContainerBuilder();
66
		// Get extension configuration
67
		$configuration = $this->getConfig($this->defaults);
0 ignored issues
show
Unused Code introduced by
The call to PackagesExtension::getConfig() has too many arguments starting with $this->defaults.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
68
69
		/**
70
		 * Load packages configuration
71
		 */
72
73
		$builder->parameters['packages'] = [];
74
75
		if (is_file($configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php')) {
76
			$packages = require $configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php';
77
78
			foreach ($packages as $name => $data) {
79
				$builder->parameters['packages'][$name] = $data;
80
			}
81
		}
82
83
		/**
84
		 * Register services
85
		 */
86
87
		$builder->addDefinition($this->prefix('loader'))
88
			->setType(Loaders\Loader::class)
89
			->setArguments([
90
				'packageFiles'    => $configuration['loader']['packageFiles'],
91
				'metadataSources' => $configuration['sources'],
92
				'vendorDir'       => $configuration['dirs']['vendorDir'],
93
			])
94
			->addTag('cms.packages');
95
96
		$repository = $builder->addDefinition($this->prefix('repository'))
97
			->setType(Repository\Repository::class)
98
			->addTag('cms.packages');
99
100
		if ($configuration['path']) {
101
			$repository->addSetup('addPath', [$configuration['path']]);
102
		}
103
104
		$builder->addDefinition($this->prefix('manager'))
105
			->setType(Packages\PackagesManager::class)
106
			->setArguments([
107
				'vendorDir' => $configuration['dirs']['vendorDir'],
108
				'configDir' => $configuration['dirs']['configDir'],
109
			])
110
			->addTag('cms.packages');
111
112
		$builder->addDefinition($this->prefix('pathResolver'))
113
			->setType(Helpers\PathResolver::class)
114
			->addTag('cms.packages');
115
116
		$builder->addDefinition($this->prefix('scripts.configuration'))
117
			->setType(Packages\Scripts\ConfigurationScript::class)
118
			->setArguments([
119
				'configDir'  => $configuration['dirs']['configDir'],
120
				'configFile' => $configuration['configFile'],
121
			])
122
			->addTag('cms.packages');
123
124
		// Define all console commands
125
		$commands = [
126
			'packagesSync'     => Commands\SyncCommand::class,
127
			'packagesList'     => Commands\ListCommand::class,
128
			'packageEnable'    => Commands\EnableCommand::class,
129
			'packageDisable'   => Commands\DisableCommand::class,
130
			'packageInstall'   => Commands\InstallCommand::class,
131
			'packageUninstall' => Commands\UninstallCommand::class,
132
		];
133
134
		foreach ($commands as $name => $cmd) {
135
			$builder->addDefinition($this->prefix('commands' . lcfirst($name)))
136
				->setType($cmd);
137
		}
138
	}
139
140
	/**
141
	 * {@inheritdoc}
142
	 */
143
	public function beforeCompile() : void
144
	{
145
		parent::beforeCompile();
146
147
		// Get container builder
148
		$builder = $this->getContainerBuilder();
149
150
		// Get packages manager
151
		$manager = $builder->getDefinition($this->prefix('manager'));
152
153
		foreach ($builder->findByType(Packages\Scripts\IScript::class) as $serviceDefinition) {
154
			$manager->addSetup('addScript', [$serviceDefinition->getType(), $serviceDefinition]);
155
		}
156
	}
157
158
	/**
159
	 * @param Nette\Configurator $config
160
	 * @param string $extensionName
161
	 *
162
	 * @return void
163
	 */
164
	public static function register(Nette\Configurator $config, string $extensionName = 'packages') : void
165
	{
166 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
167 1
			$compiler->addExtension($extensionName, new PackagesExtension());
168 1
		};
169 1
	}
170
}
171