AuthenticationExtension::getTargetEntities()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
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\Authentication\DI;
6
7
use Nette\Schema\Expect;
8
use Nette\Schema\Schema;
9
use Nette\Security\IAuthenticator;
10
use Nette\DI\Definitions\Statement;
11
use SixtyEightPublishers\DoctrineBridge\DI\TargetEntity;
12
use SixtyEightPublishers\User\Common\DI\CommonExtension;
13
use SixtyEightPublishers\User\DI\AbstractCompilerExtensionPass;
14
use SixtyEightPublishers\User\Authentication\Entity\UserInterface;
15
use SixtyEightPublishers\User\Authentication\Csrf\CsrfTokenFactory;
16
use SixtyEightPublishers\User\Common\Exception\ConfigurationException;
17
use SixtyEightPublishers\DoctrineBridge\DI\TargetEntityProviderInterface;
18
use SixtyEightPublishers\User\Authentication\Authenticator\Authenticator;
19
use SixtyEightPublishers\User\Authentication\Control\SignIn\SignInControl;
20
use SixtyEightPublishers\TranslationBridge\DI\TranslationProviderInterface;
21
use SixtyEightPublishers\User\Authentication\Csrf\CsrfTokenFactoryInterface;
22
use SixtyEightPublishers\User\Authentication\Query\AuthenticatorQueryObject;
23
use SixtyEightPublishers\User\Authentication\Control\SignIn\SignInControlFactoryInterface;
24
use SixtyEightPublishers\User\Authentication\Query\AuthenticatorQueryObjectFactoryInterface;
25
26
final class AuthenticationExtension extends AbstractCompilerExtensionPass implements TargetEntityProviderInterface, TranslationProviderInterface
27
{
28
	/**
29
	 * {@inheritDoc}
30
	 */
31 View Code Duplication
	public function startup(): void
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...
32
	{
33
		parent::startup();
34
35
		if (!$this->config->enabled) {
36
			$this->stopPropagation();
37
		}
38
39
		if (!is_subclass_of($this->sharedData[CommonExtension::SHARED_DATA_USER_CLASS_NAME], UserInterface::class, TRUE)) {
40
			throw new ConfigurationException(sprintf(
41
				'Your User entity must implement interface %s',
42
				UserInterface::class
43
			));
44
		}
45
	}
46
47
	/**
48
	 * {@inheritDoc}
49
	 */
50
	public function getConfigSchema(): Schema
51
	{
52
		return Expect::structure([
53
			'enabled' => Expect::bool(FALSE),
54
			'authenticator' => Expect::anyOf(Expect::string(), Expect::type(Statement::class))
55
				->default(Authenticator::class)
56
				->before(static function ($def) {
57
					return $def instanceof Statement ? $def : new Statement($def);
0 ignored issues
show
Bug introduced by
The class Nette\DI\Definitions\Statement does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
				}),
59
			'csrf_token_factory' => Expect::anyOf(Expect::string(), Expect::type(Statement::class))
60
				->default(CsrfTokenFactory::class)
61
				->before(static function ($def) {
62
					return $def instanceof Statement ? $def : new Statement($def);
0 ignored issues
show
Bug introduced by
The class Nette\DI\Definitions\Statement does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
63
				}),
64
			'register_controls' => Expect::bool(FALSE),
65
		]);
66
	}
67
68
	/**
69
	 * {@inheritDoc}
70
	 */
71
	public function loadConfiguration(): void
72
	{
73
		$builder = $this->getContainerBuilder();
74
75
		$builder->addDefinition($this->prefix('authenticator'))
76
			->setType(IAuthenticator::class)
77
			->setFactory($this->config->authenticator);
78
79
		if (isset($builder->getAliases()['nette.authenticator'])) {
80
			$builder->removeAlias('nette.authenticator');
81
		}
82
83
		$builder->addAlias('nette.authenticator', $this->prefix('authenticator'));
84
85
		$builder->addDefinition($this->prefix('csrf_token_factory'))
86
			->setType(CsrfTokenFactoryInterface::class)
87
			->setFactory($this->config->csrf_token_factory);
88
89
		if ($this->config->register_controls) {
90
			$builder->addFactoryDefinition($this->prefix('control.sign_in'))
91
				->setImplement(SignInControlFactoryInterface::class)
92
				->getResultDefinition()
93
				->setFactory(SignInControl::class);
94
		}
95
96
		$builder->addFactoryDefinition($this->prefix('query_object_factory.authenticator'))
97
			->setImplement(AuthenticatorQueryObjectFactoryInterface::class)
98
			->getResultDefinition()
99
			->setFactory(AuthenticatorQueryObject::class);
100
	}
101
102
	/**
103
	 * {@inheritdoc}
104
	 */
105
	public function getTargetEntities(): array
106
	{
107
		return [
108
			new TargetEntity(UserInterface::class, $this->sharedData[CommonExtension::SHARED_DATA_USER_CLASS_NAME]),
109
		];
110
	}
111
112
	/**
113
	 * {@inheritdoc}
114
	 */
115
	public function getTranslationResources(): array
116
	{
117
		return $this->config->register_controls ? [ __DIR__ . '/../translations' ] : [];
118
	}
119
}
120