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

UserExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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,
0 ignored issues
show
Bug introduced by
It seems like $className defined by $className on line 82 can also be of type object<SixtyEightPublish...r\DI\IExtensionAdapter>; however, SixtyEightPublishers\Use...dapterFactory::create() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->validateConfig(\N...Builder()->parameters)) on line 98 can also be of type string; however, SixtyEightPublishers\Use...uildExtensionAdapters() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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