Completed
Pull Request — master (#2)
by Tomáš
09:49
created

UserExtension   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 147
Duplicated Lines 22.45 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 33
loc 147
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A loadConfiguration() 0 6 2
A beforeCompile() 0 6 2
A afterCompile() 0 6 2
A buildExtensionAdapters() 0 12 2
A getExtensionAdapters() 0 12 2
A getEntityMappings() 11 11 2
A getTargetEntityMappings() 11 11 2
A getTranslationResources() 11 11 2

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
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\DI;
6
7
use Kdyby;
8
use Nette;
9
use SixtyEightPublishers;
10
11
final class UserExtension extends Nette\DI\CompilerExtension implements
12
	Kdyby\Doctrine\DI\IEntityProvider,
13
	Kdyby\Doctrine\DI\ITargetEntityProvider,
14
	Kdyby\Translation\DI\ITranslationProvider
15
{
16
	/** @var array  */
17
	private $defaults = [
18
		# 'common' is loaded in constructor
19
		# 'doctrine_identity' is loaded in constructor
20
		# 'forgot_password' is loaded in constructor
21
		# 'authentication' is loaded in constructor
22
	];
23
24
	/** @var string[]|\SixtyEightPublishers\User\DI\IExtensionAdapter[]  */
25
	private $extensionAdapters = [
26
		'common' => SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::class,
27
		'doctrine_identity' => SixtyEightPublishers\User\DoctrineIdentity\DI\DoctrineIdentityExtensionAdapter::class,
28
		'forgot_password' => SixtyEightPublishers\User\ForgotPassword\DI\ForgotPasswordExtensionAdapter::class,
29
		'authentication' => SixtyEightPublishers\User\Authentication\DI\AuthenticationExtensionAdapter::class,
30
	];
31
32
	/** @var bool  */
33
	private $extensionAdaptersBuilded = FALSE;
34
35
	public function __construct()
36
	{
37
		$this->defaults = array_merge($this->defaults, [
38
			'common' => SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::getDefaults(),
39
			'doctrine_identity' => SixtyEightPublishers\User\DoctrineIdentity\DI\DoctrineIdentityExtensionAdapter::getDefaults(),
40
			'forgot_password' => SixtyEightPublishers\User\ForgotPassword\DI\ForgotPasswordExtensionAdapter::getDefaults(),
41
			'authentication' => SixtyEightPublishers\User\Authentication\DI\AuthenticationExtensionAdapter::getDefaults(),
42
		]);
43
	}
44
45
	/**
46
	 * {@inheritdoc}
47
	 */
48
	public function loadConfiguration(): void
49
	{
50
		foreach ($this->getExtensionAdapters() as $extensionAdapter) {
51
			$extensionAdapter->loadConfiguration();
52
		}
53
	}
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function beforeCompile(): void
59
	{
60
		foreach ($this->getExtensionAdapters() as $extensionAdapter) {
61
			$extensionAdapter->beforeCompile();
62
		}
63
	}
64
65
	/**
66
	 * {@inheritdoc}
67
	 */
68
	public function afterCompile(Nette\PhpGenerator\ClassType $class): void
69
	{
70
		foreach ($this->getExtensionAdapters() as $extensionAdapter) {
71
			$extensionAdapter->afterCompile($class);
72
		}
73
	}
74
75
	/**
76
	 * @param array $config
77
	 */
78
	private function buildExtensionAdapters(array $config): void
79
	{
80
		$extensionAdapterFactory = new ExtensionAdapterFactory($this->getContainerBuilder(), new \ArrayObject());
81
82
		foreach ($this->extensionAdapters as $name => $className) {
83
			$this->extensionAdapters[$name] = $extensionAdapterFactory->create(
84
				$className,
85
				$this->prefix($name),
86
				$config[$name]
87
			);
88
		}
89
	}
90
91
	/**
92
	 * @return \SixtyEightPublishers\User\DI\IExtensionAdapter[]
93
	 */
94
	private function getExtensionAdapters(): array
95
	{
96
		if (FALSE === $this->extensionAdaptersBuilded) {
97
			/** @noinspection PhpInternalEntityUsedInspection */
98
			$config = $this->validateConfig(Nette\DI\Helpers::expand($this->defaults, $this->getContainerBuilder()->parameters));
99
100
			$this->buildExtensionAdapters($config);
101
			$this->extensionAdaptersBuilded = TRUE;
102
		}
103
104
		return $this->extensionAdapters;
105
	}
106
107
	/**************** interface \Kdyby\Doctrine\DI\IEntityProvider ****************/
108
109
	/**
110
	 * {@inheritdoc}
111
	 */
112 View Code Duplication
	public function getEntityMappings(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
113
	{
114
		$map =  array_values(array_map(
115
			function (Kdyby\Doctrine\DI\IEntityProvider $provider) {
116
				return $provider->getEntityMappings();
117
			},
118
			$this->getExtensionAdapters()
119
		));
120
121
		return 0 < count($map) ? array_merge(...$map) : [];
122
	}
123
124
	/**************** interface \Kdyby\Doctrine\DI\ITargetEntityProvider ****************/
125
126
	/**
127
	 * {@inheritdoc}
128
	 */
129 View Code Duplication
	public function getTargetEntityMappings(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
130
	{
131
		$map =  array_values(array_map(
132
			function (Kdyby\Doctrine\DI\ITargetEntityProvider $provider) {
133
				return $provider->getTargetEntityMappings();
134
			},
135
			$this->getExtensionAdapters()
136
		));
137
138
		return 0 < count($map) ? array_merge(...$map) : [];
139
	}
140
141
	/**************** interface \Kdyby\Translation\DI\ITranslationProvider ****************/
142
143
	/**
144
	 * {@inheritdoc}
145
	 */
146 View Code Duplication
	public function getTranslationResources(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
147
	{
148
		$map =  array_values(array_map(
149
			function (Kdyby\Translation\DI\ITranslationProvider $provider) {
150
				return $provider->getTranslationResources();
151
			},
152
			$this->getExtensionAdapters()
153
		));
154
155
		return 0 < count($map) ? array_merge(...$map) : [];
156
	}
157
}
158