Completed
Push — master ( e088fc...e52644 )
by Adam
02:49
created

WidgetsExtension::beforeCompile()   F

Complexity

Conditions 14
Paths 396

Size

Total Lines 76
Code Lines 29

Duplication

Lines 10
Ratio 13.16 %

Code Coverage

Tests 16
CRAP Score 31.6588

Importance

Changes 0
Metric Value
dl 10
loc 76
ccs 16
cts 29
cp 0.5517
rs 3.9629
c 0
b 0
f 0
cc 14
eloc 29
nc 396
nop 0
crap 31.6588

How to fix   Long Method    Complexity   

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
 * WidgetsManagerExtension.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:Widgets!
9
 * @subpackage     DI
10
 * @since          1.0.0
11
 *
12
 * @date           15.09.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\DI;
18
19
use Nette;
20
use Nette\DI;
21
use Nette\PhpGenerator as Code;
22
use Nette\Security as NS;
23
use Nette\Utils;
24
25
use IPub;
26
use IPub\Widgets;
27
use IPub\Widgets\Components;
28
use IPub\Widgets\Decorators;
29
use IPub\Widgets\Exceptions;
30
use IPub\Widgets\Filters;
31
32
/**
33
 * Widgets extension container
34
 *
35
 * @package        iPublikuj:Widgets!
36
 * @subpackage     DI
37
 *
38
 * @author         Adam Kadlec <[email protected]>
39
 */
40 1
final class WidgetsExtension extends DI\CompilerExtension
41
{
42
	// Define tag string for widgets
43
	const TAG_WIDGET_CONTROL = 'ipub.widgets.widget';
44
45
	// Define tag string for widgets decorators
46
	const TAG_WIDGET_DECORATOR = 'ipub.widgets.decorator';
47
48
	// Define tag string for widgets decorators
49
	const TAG_WIDGET_FILTER = 'ipub.widgets.filter';
50
51
	public function loadConfiguration()
52
	{
53
		// Get container builder
54 1
		$builder = $this->getContainerBuilder();
55
56
		/**
57
		 * Widgets services
58
		 */
59
60
		// Widgets manager
61 1
		$builder->addDefinition($this->prefix('widgets.manager'))
62 1
			->setClass(Widgets\Managers\WidgetsManager::class)
63 1
			->addTag('cms.widgets');
64
65 1
		$builder->addDefinition($this->prefix('widgets.component'))
66 1
			->setClass(Components\Control::class)
67 1
			->setImplement(Components\IControl::class)
68 1
			->setArguments([new Code\PhpLiteral('$position')])
69 1
			->setInject(TRUE)
70 1
			->addTag('cms.widgets');
71
72
		/**
73
		 * Widgets filters
74
		 */
75
76
		// Widgets filter manager
77 1
		$builder->addDefinition($this->prefix('filters.manager'))
78 1
			->setClass(Widgets\Managers\FiltersManager::class)
79 1
			->addTag('cms.widgets');
80
81
		// Widgets priority filter
82 1
		$builder->addDefinition('widgets.filters.priority')
83 1
			->setClass(Filters\Priority\Filter::class)
84 1
			->setImplement(Filters\Priority\IFilter::class)
85 1
			->setInject(TRUE)
86 1
			->addTag('cms.widgets')
87 1
			->addTag(self::TAG_WIDGET_FILTER);
88
89
		// Widgets status filter
90 1
		$builder->addDefinition('widgets.filters.status')
91 1
			->setClass(Filters\Status\Filter::class)
92 1
			->setImplement(Filters\Status\IFilter::class)
93 1
			->setInject(TRUE)
94 1
			->addTag('cms.widgets')
95 1
			->addTag(self::TAG_WIDGET_FILTER);
96
97
		/**
98
		 * Widgets decorators
99
		 */
100
101
		// Widgets decorators manager
102 1
		$builder->addDefinition($this->prefix('decorators.manager'))
103 1
			->setClass(Widgets\Managers\DecoratorsManager::class)
104 1
			->addTag('cms.widgets');
105
106
		// Widgets raw decorator
107 1
		$builder->addDefinition('widgets.decorator.raw')
108 1
			->setClass(Decorators\Raw\Control::class)
109 1
			->setImplement(Decorators\Raw\IControl::class)
110 1
			->setInject(TRUE)
111 1
			->addTag('cms.widgets')
112 1
			->addTag(self::TAG_WIDGET_DECORATOR);
113 1
	}
114
115
	/**
116
	 * {@inheritdoc}
117
	 */
118
	public function beforeCompile()
119
	{
120 1
		parent::beforeCompile();
121
122
		// Get container builder
123 1
		$builder = $this->getContainerBuilder();
124
125
		// Get widgets manager
126 1
		$widgetsManager = $builder->getDefinition($this->prefix('widgets.manager'));
127
128
		// Get all registered widgets components
129 1
		foreach ($builder->findByTag(self::TAG_WIDGET_CONTROL) as $serviceName => $groups) {
130
			// Check for valid group format
131
			$groups = is_array($groups) ? $groups : (is_string($groups) ? [$groups] : ['default']);
132
133
			// Register widget to manager and group
134
			foreach ($groups as $group) {
135
				$widgetsManager->addSetup('register', ['@' . $serviceName, $serviceName, $group]);
136
			}
137
		}
138
139
		// Get widgets decorators manager
140 1
		$decoratorsManager = $builder->getDefinition($this->prefix('decorators.manager'));
141
142
		// Get all registered widgets decorators
143 1 View Code Duplication
		foreach (array_keys($builder->findByTag(self::TAG_WIDGET_DECORATOR)) as $serviceName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
			// Register decorator to manager
145 1
			$decoratorsManager->addSetup('register', ['@' . $serviceName, $serviceName]);
146
		}
147
148
		// Get widgets filters manager
149 1
		$filtersManager = $builder->getDefinition($this->prefix('filters.manager'));
150
151
		// Get all registered widgets decorators
152 1 View Code Duplication
		foreach (array_keys($builder->findByTag(self::TAG_WIDGET_FILTER)) as $serviceName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153 1
			$priority = 999;
154
155
			// Register filter to manager
156 1
			$filtersManager->addSetup('register', ['@' . $serviceName, $serviceName, $priority]);
157
		}
158
159
		// Get widgets control provider
160 1
		$widgetsContainer = $builder->getDefinition($this->prefix('widgets.component'));
161
162
		// Search for widgets provider extensions
163
		/** @var IWidgetsProvider $extension */
164 1
		foreach ($this->compiler->getExtensions(IWidgetsProvider::class) as $extension) {
165
			// Get widget groups & widgets from extension
166
			foreach ($extension->getWidgets() as $group => $widgets) {
167
				foreach ($widgets as $id => $properties) {
168
					if (!isset($properties['type'])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
169
						
170
					}
171
172
					if (!isset($properties['position'])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
173
						
174
					}
175
176
					$type = $properties['type'];
177
					$position = $properties['position'];
178
179
					unset($properties['type']);
180
181
					if (!isset($properties['priority'])) {
182
						$properties['priority'] = 100;
183
					}
184
185
					$widgetsContainer->addSetup('addWidget', [$type, $properties, $group, $position]);
186
				}
187
			}
188
		}
189
190
		// Install extension latte macros
191 1
		$latteFactory = $builder->getDefinition($builder->getByType(Nette\Bridges\ApplicationLatte\ILatteFactory::class) ?: 'nette.latteFactory');
192 1
		$latteFactory->addSetup('IPub\Widgets\Latte\Macros::install(?->getCompiler())', ['@self']);
193 1
	}
194
195
	/**
196
	 * @param Nette\Configurator $config
197
	 * @param string $extensionName
198
	 */
199
	public static function register(Nette\Configurator $config, string $extensionName = 'widgets')
200
	{
201 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
202 1
			$compiler->addExtension($extensionName, new WidgetsExtension());
203 1
		};
204 1
	}
205
}
206