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

AuthenticatorMount::getAuthenticator()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Authentication\Authenticator;
6
7
use Nette;
8
use SixtyEightPublishers;
9
10
final class AuthenticatorMount implements Nette\Security\IAuthenticator
11
{
12
	use Nette\SmartObject;
13
14
	const 	SEPARATOR = '://';
15
16
	/** @var \Nette\Security\IAuthenticator[] */
17
	private $authenticators;
18
19
	/**
20
	 * @param \Nette\Security\IAuthenticator $authenticators
21
	 */
22
	public function __construct(array $authenticators)
23
	{
24
		foreach ($authenticators as $name => $authenticator) {
25
			$this->addAuthenticator((string) $name, $authenticator);
26
		}
27
	}
28
29
	/**
30
	 * @param string                         $name
31
	 * @param \Nette\Security\IAuthenticator $authenticator
32
	 *
33
	 * @return void
34
	 */
35
	private function addAuthenticator(string $name, Nette\Security\IAuthenticator $authenticator): void
36
	{
37
		$this->authenticators[$name] = $authenticator;
38
	}
39
40
	/**
41
	 * @param string $name
42
	 *
43
	 * @return \Nette\Security\IAuthenticator
44
	 * @throws \SixtyEightPublishers\User\Common\Exception\InvalidArgumentException
45
	 */
46
	public function getAuthenticator(string $name): Nette\Security\IAuthenticator
47
	{
48
		if (!isset($this->authenticators[$name])) {
49
			throw new SixtyEightPublishers\User\Common\Exception\InvalidArgumentException(sprintf(
50
				'Missing Authenticator with name "%s"',
51
				$name
52
			));
53
		}
54
	}
55
56
	/**
57
	 * @param string $login
58
	 *
59
	 * @return array
60
	 */
61
	private function getPrefixAndLogin(string $login): array
62
	{
63
		if (Nette\Utils\Strings::contains($login, self::SEPARATOR)) {
64
			return explode(self::SEPARATOR, $login, 2);
65
		}
66
67
		return [ '', $login ];
68
	}
69
70
	/************* interface \Nette\Security\IAuthenticator *************/
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75
	public function authenticate(array $credentials): Nette\Security\IIdentity
76
	{
77
		if (!isset($credentials[self::USERNAME])) {
78
			throw new Nette\Security\AuthenticationException(sprintf(
79
				'Missing login field in credentials (key %s)',
80
				self::USERNAME
81
			), self::FAILURE);
82
		}
83
84
		[ $prefix, $login ] = $credentials[self::USERNAME];
0 ignored issues
show
Bug introduced by
The variable $prefix does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $login does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
85
86
		try {
87
			$authenticator = $this->getAuthenticator($prefix);
88
		} catch (SixtyEightPublishers\User\Common\Exception\InvalidArgumentException $e) {
89
			throw new Nette\Security\AuthenticationException($e->getMessage(), self::FAILURE, $e);
90
		}
91
92
		$credentials[self::USERNAME] = $login;
93
94
		return $authenticator->authenticate($credentials);
95
	}
96
}
97