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

AuthenticationStack::isAuthenticated()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
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