WidgetsExtension   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 171
Duplicated Lines 5.85 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 81.16%

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 0
dl 10
loc 171
ccs 56
cts 69
cp 0.8116
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 63 1
F beforeCompile() 10 76 14
A register() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * WidgetsManagerExtension.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
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
23
use IPub\Widgets;
24
use IPub\Widgets\Components;
25
use IPub\Widgets\Decorators;
26
use IPub\Widgets\Filters;
27
28
/**
29
 * Widgets extension container
30
 *
31
 * @package        iPublikuj:Widgets!
32
 * @subpackage     DI
33
 *
34
 * @author         Adam Kadlec <[email protected]>
35
 */
36 1
final class WidgetsExtension extends DI\CompilerExtension
37
{
38
	// Define tag string for widgets
39
	const TAG_WIDGET_CONTROL = 'ipub.widgets.widget';
40
41
	// Define tag string for widgets decorators
42
	const TAG_WIDGET_DECORATOR = 'ipub.widgets.decorator';
43
44
	// Define tag string for widgets decorators
45
	const TAG_WIDGET_FILTER = 'ipub.widgets.filter';
46
47
	/**
48
	 * @return void
49
	 */
50
	public function loadConfiguration() : void
51
	{
52
		// Get container builder
53 1
		$builder = $this->getContainerBuilder();
54
55
		/**
56
		 * Widgets services
57
		 */
58
59
		// Widgets manager
60 1
		$builder->addDefinition($this->prefix('widgets.manager'))
61 1
			->setType(Widgets\Managers\WidgetsManager::class)
62 1
			->addTag('cms.widgets');
63
64 1
		$builder->addDefinition($this->prefix('widgets.component'))
65 1
			->setType(Components\Control::class)
66 1
			->setImplement(Components\IControl::class)
67 1
			->setArguments([new Code\PhpLiteral('$position')])
68 1
			->setInject(TRUE)
69 1
			->addTag('cms.widgets');
70
71
		/**
72
		 * Widgets filters
73
		 */
74
75
		// Widgets filter manager
76 1
		$builder->addDefinition($this->prefix('filters.manager'))
77 1
			->setType(Widgets\Managers\FiltersManager::class)
78 1
			->addTag('cms.widgets');
79
80
		// Widgets priority filter
81 1
		$builder->addDefinition('widgets.filters.priority')
82 1
			->setType(Filters\Priority\Filter::class)
83 1
			->setImplement(Filters\Priority\IFilter::class)
84 1
			->setInject(TRUE)
85 1
			->addTag('cms.widgets')
86 1
			->addTag(self::TAG_WIDGET_FILTER);
87
88
		// Widgets status filter
89 1
		$builder->addDefinition('widgets.filters.status')
90 1
			->setType(Filters\Status\Filter::class)
91 1
			->setImplement(Filters\Status\IFilter::class)
92 1
			->setInject(TRUE)
93 1
			->addTag('cms.widgets')
94 1
			->addTag(self::TAG_WIDGET_FILTER);
95
96
		/**
97
		 * Widgets decorators
98
		 */
99
100
		// Widgets decorators manager
101 1
		$builder->addDefinition($this->prefix('decorators.manager'))
102 1
			->setType(Widgets\Managers\DecoratorsManager::class)
103 1
			->addTag('cms.widgets');
104
105
		// Widgets raw decorator
106 1
		$builder->addDefinition('widgets.decorator.raw')
107 1
			->setType(Decorators\Raw\Control::class)
108 1
			->setImplement(Decorators\Raw\IControl::class)
109 1
			->setInject(TRUE)
110 1
			->addTag('cms.widgets')
111 1
			->addTag(self::TAG_WIDGET_DECORATOR);
112 1
	}
113
114
	/**
115
	 * {@inheritdoc}
116
	 */
117
	public function beforeCompile() : void
118
	{
119 1
		parent::beforeCompile();
120
121
		// Get container builder
122 1
		$builder = $this->getContainerBuilder();
123
124
		// Get widgets manager
125 1
		$widgetsManager = $builder->getDefinition($this->prefix('widgets.manager'));
126
127
		// Get all registered widgets components
128 1
		foreach ($builder->findByTag(self::TAG_WIDGET_CONTROL) as $serviceName => $groups) {
129
			// Check for valid group format
130
			$groups = is_array($groups) ? $groups : (is_string($groups) ? [$groups] : ['default']);
131
132
			// Register widget to manager and group
133
			foreach ($groups as $group) {
134
				$widgetsManager->addSetup('register', ['@' . $serviceName, $serviceName, $group]);
135
			}
136
		}
137
138
		// Get widgets decorators manager
139 1
		$decoratorsManager = $builder->getDefinition($this->prefix('decorators.manager'));
140
141
		// Get all registered widgets decorators
142 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...
143
			// Register decorator to manager
144 1
			$decoratorsManager->addSetup('register', ['@' . $serviceName, $serviceName]);
145
		}
146
147
		// Get widgets filters manager
148 1
		$filtersManager = $builder->getDefinition($this->prefix('filters.manager'));
149
150
		// Get all registered widgets decorators
151 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...
152 1
			$priority = 999;
153
154
			// Register filter to manager
155 1
			$filtersManager->addSetup('register', ['@' . $serviceName, $serviceName, $priority]);
156
		}
157
158
		// Get widgets control provider
159 1
		$widgetsContainer = $builder->getDefinition($this->prefix('widgets.component'));
160
161
		// Search for widgets provider extensions
162
		/** @var IWidgetsProvider $extension */
163 1
		foreach ($this->compiler->getExtensions(IWidgetsProvider::class) as $extension) {
164
			// Get widget groups & widgets from extension
165
			foreach ($extension->getWidgets() as $group => $widgets) {
166
				foreach ($widgets as $id => $properties) {
167
					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...
168
169
					}
170
171
					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...
172
173
					}
174
175
					$type = $properties['type'];
176
					$position = $properties['position'];
177
178
					unset($properties['type']);
179
180
					if (!isset($properties['priority'])) {
181
						$properties['priority'] = 100;
182
					}
183
184
					$widgetsContainer->addSetup('addWidget', [$type, $properties, $group, $position]);
185
				}
186
			}
187
		}
188
189
		// Install extension latte macros
190 1
		$latteFactory = $builder->getDefinition($builder->getByType(Nette\Bridges\ApplicationLatte\ILatteFactory::class) ?: 'nette.latteFactory');
191 1
		$latteFactory->addSetup('IPub\Widgets\Latte\Macros::install(?->getCompiler())', ['@self']);
192 1
	}
193
194
	/**
195
	 * @param Nette\Configurator $config
196
	 * @param string $extensionName
197
	 *
198
	 * @return void
199
	 */
200
	public static function register(Nette\Configurator $config, string $extensionName = 'widgets') : void
201
	{
202 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
203 1
			$compiler->addExtension($extensionName, new WidgetsExtension());
204 1
		};
205 1
	}
206
}
207