Completed
Pull Request — master (#2)
by Vojtěch
10:05
created

AuthenticationExtensionAdapter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 29.91 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 32
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processConfig() 24 24 3
A loadConfiguration() 0 35 5
A getTargetEntityMappings() 0 6 1
A getTranslationResources() 8 8 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\Authentication\DI;
6
7
use Kdyby;
8
use Nette;
9
use SixtyEightPublishers;
10
11
final class AuthenticationExtensionAdapter extends SixtyEightPublishers\User\DI\AbstractExtensionAdapter implements
12
	Kdyby\Doctrine\DI\ITargetEntityProvider,
13
	Kdyby\Translation\DI\ITranslationProvider
14
{
15
	/** @var array  */
16
	protected static $defaults = [
17
		'enabled' => FALSE,
18
		'authenticator' => SixtyEightPublishers\User\Authentication\Authenticator\Authenticator::class,
19
		'register_controls' => FALSE,
20
	];
21
22
	/** @var NULL|string */
23
	private $authenticatorName;
24
25
	/**
26
	 * {@inheritdoc}
27
	 */
28 View Code Duplication
	protected function processConfig(array $config, \ArrayObject $sharedData): 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...
29
	{
30
		Nette\Utils\Validators::assertField($config, 'enabled', 'bool');
31
		Nette\Utils\Validators::assertField($config, 'authenticator', 'string|' . Nette\DI\Statement::class);
32
		Nette\Utils\Validators::assertField($config, 'register_controls', 'bool');
33
34
		if (FALSE === $config['enabled']) {
35
			$this->stopPropagation();
36
		}
37
38
		if (!is_subclass_of(
39
			$sharedData[SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::SHARED_DATA_USER_CLASS_NAME],
40
			SixtyEightPublishers\User\Authentication\DoctrineEntity\IUser::class,
41
			TRUE
42
		)
43
		) {
44
			throw new SixtyEightPublishers\User\Common\Exception\ConfigurationException(sprintf(
45
				'Your User entity must implement interface %s',
46
				SixtyEightPublishers\User\Authentication\DoctrineEntity\IUser::class
47
			));
48
		}
49
50
		return $config;
51
	}
52
53
	/**
54
	 * {@inheritdoc}
55
	 */
56
	public function loadConfiguration(): void
57
	{
58
		$config = $this->getConfig();
59
		$builder = $this->getContainerBuilder();
60
		$authenticator = $config['authenticator'];
61
62
		# authenticator
63
		if (!is_string($authenticator) || !Nette\Utils\Strings::startsWith($authenticator, '@')) {
64
			$builder->addDefinition($this->prefix('authenticator'))
65
				->setType(Nette\Security\IAuthenticator::class)
66
				->setFactory($authenticator);
67
68
			$this->authenticatorName = $this->prefix('authenticator');
69
		} else {
70
			$this->authenticatorName = Nette\Utils\Strings::substring($authenticator, 1);
71
		}
72
73
		if (isset($builder->getAliases()['nette.authenticator'])) {
74
			$builder->removeAlias('nette.authenticator');
75
		}
76
77
		$builder->addAlias('nette.authenticator', $this->authenticatorName);
78
79
		# controls
80
		if (TRUE === $config['register_controls']) {
81
			$builder->addDefinition($this->prefix('control.sign_in'))
82
				->setImplement(SixtyEightPublishers\User\Authentication\Control\SignIn\ISignInControlFactory::class)
83
				->setFactory(SixtyEightPublishers\User\Authentication\Control\SignIn\SignInControl::class);
84
		}
85
86
		# queries
87
		$builder->addDefinition($this->prefix('query_factory.authenticator'))
88
			->setType(SixtyEightPublishers\User\Authentication\Query\IAuthenticatorQueryFactory::class)
89
			->setFactory(SixtyEightPublishers\User\Authentication\Query\AuthenticatorQueryFactory::class);
90
	}
91
92
	/**************** interface \Kdyby\Doctrine\DI\ITargetEntityProvider ****************/
93
94
	/**
95
	 * {@inheritdoc}
96
	 */
97
	public function getTargetEntityMappings(): array
98
	{
99
		return [
100
			SixtyEightPublishers\User\Authentication\DoctrineEntity\IUser::class => $this->getSharedData(SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::SHARED_DATA_USER_CLASS_NAME),
101
		];
102
	}
103
104
	/**************** interface \Kdyby\Translation\DI\ITranslationProvider ****************/
105
106
	/**
107
	 * {@inheritdoc}
108
	 */
109 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...
110
	{
111
		$config = $this->getConfig();
112
113
		return TRUE === $config['register_controls']
114
			? [ __DIR__ . '/../locale' ]
115
			: [];
116
	}
117
}
118