Completed
Pull Request — master (#3)
by Tomáš
10:21
created

AuthenticatorMount   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A authenticate() 0 21 3
A getAuthenticator() 0 11 2
A addAuthenticator() 0 4 1
A getPrefixAndUsername() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Authentication\Authenticator;
6
7
use Nette\SmartObject;
8
use Nette\Utils\Strings;
9
use Nette\Security\IIdentity;
10
use Nette\Security\IAuthenticator;
11
use Nette\Security\AuthenticationException;
12
use SixtyEightPublishers\User\Common\Exception\InvalidArgumentException;
13
14
final class AuthenticatorMount implements IAuthenticator
15
{
16
	use SmartObject;
17
18
	public const SEPARATOR = '://';
19
20
	/** @var \Nette\Security\IAuthenticator[] */
21
	private $authenticators;
22
23
	/**
24
	 * @param \Nette\Security\IAuthenticator[] $authenticators
25
	 */
26
	public function __construct(array $authenticators)
27
	{
28
		foreach ($authenticators as $name => $authenticator) {
29
			$this->addAuthenticator((string) $name, $authenticator);
30
		}
31
	}
32
33
	/**
34
	 * {@inheritdoc}
35
	 */
36
	public function authenticate(array $credentials): IIdentity
37
	{
38
		if (!isset($credentials[self::USERNAME])) {
39
			throw new AuthenticationException(sprintf(
40
				'Missing username field in credentials (key %s)',
41
				self::USERNAME
42
			), self::FAILURE);
43
		}
44
45
		[ $prefix, $username ] = $this->getPrefixAndUsername($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 $username 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...
46
47
		try {
48
			$authenticator = $this->getAuthenticator($prefix);
49
		} catch (InvalidArgumentException $e) {
50
			throw new AuthenticationException($e->getMessage(), self::FAILURE, $e);
51
		}
52
53
		$credentials[self::USERNAME] = $username;
54
55
		return $authenticator->authenticate($credentials);
56
	}
57
58
	/**
59
	 * @param string $name
60
	 *
61
	 * @return \Nette\Security\IAuthenticator
62
	 * @throws \SixtyEightPublishers\User\Common\Exception\InvalidArgumentException
63
	 */
64
	public function getAuthenticator(string $name): IAuthenticator
65
	{
66
		if (!isset($this->authenticators[$name])) {
67
			throw new InvalidArgumentException(sprintf(
68
				'Missing Authenticator with name "%s"',
69
				$name
70
			));
71
		}
72
73
		return $this->authenticators[$name];
74
	}
75
76
	/**
77
	 * @param string                         $name
78
	 * @param \Nette\Security\IAuthenticator $authenticator
79
	 *
80
	 * @return void
81
	 */
82
	private function addAuthenticator(string $name, IAuthenticator $authenticator): void
83
	{
84
		$this->authenticators[$name] = $authenticator;
85
	}
86
87
	/**
88
	 * @param string $username
89
	 *
90
	 * @return array
91
	 */
92
	private function getPrefixAndUsername(string $username): array
93
	{
94
		if (Strings::contains($username, self::SEPARATOR)) {
95
			return explode(self::SEPARATOR, $username, 2);
96
		}
97
98
		return [ '', $username ];
99
	}
100
}
101