AuthenticationStack::isAuthenticated()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 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