AuthenticationStack   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 57
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addAuthentication() 0 4 1
A isAuthenticated() 0 10 3
A getAuthenticatedUser() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Security\Authentication;
6
7
use Chubbyphp\Security\UserInterface;
8
use Psr\Http\Message\ServerRequestInterface as Request;
9
10
final class AuthenticationStack implements AuthenticationInterface
11
{
12
    /**
13
     * @var AuthenticationInterface[]
14
     */
15
    private $authentications = [];
16
17
    /**
18
     * @param AuthenticationInterface[] $authentications
19
     */
20 4
    public function __construct(array $authentications)
21
    {
22 4
        foreach ($authentications as $authentication) {
23 4
            $this->addAuthentication($authentication);
24
        }
25 4
    }
26
27
    /**
28
     * @param AuthenticationInterface $authentication
29
     */
30 4
    private function addAuthentication(AuthenticationInterface $authentication)
31
    {
32 4
        $this->authentications[] = $authentication;
33 4
    }
34
35
    /**
36
     * @param Request $request
37
     *
38
     * @return bool
39
     */
40 2
    public function isAuthenticated(Request $request): bool
41
    {
42 2
        foreach ($this->authentications as $authentication) {
43 2
            if ($authentication->isAuthenticated($request)) {
44 2
                return true;
45
            }
46
        }
47
48 1
        return false;
49
    }
50
51
    /**
52
     * @param Request $request
53
     *
54
     * @return UserInterface|null
55
     */
56 2
    public function getAuthenticatedUser(Request $request)
57
    {
58 2
        foreach ($this->authentications as $authentication) {
59 2
            if (null !== $user = $authentication->getAuthenticatedUser($request)) {
60 2
                return $user;
61
            }
62
        }
63
64 1
        return null;
65
    }
66
}
67