Completed
Push — master ( 33ac2f...378c34 )
by Dominik
02:14
created

AuthenticationStack   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

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