Passed
Pull Request — master (#18)
by Alexander
01:35
created

Composite::setAuthMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Auth\Method;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Yiisoft\Auth\AuthenticationMethodInterface;
10
use Yiisoft\Auth\IdentityInterface;
11
12
/**
13
 * Composite allows multiple authentication methods at the same time.
14
 *
15
 * The authentication methods contained by Composite are configured via {@see setAuthMethods()},
16
 * which is a list of supported authentication class configurations.
17
 */
18
final class Composite implements AuthenticationMethodInterface
19
{
20
    /**
21
     * @var AuthenticationMethodInterface[]
22
     */
23
    private array $authenticationMethods;
24
25
    public function __construct(array $methods)
26
    {
27
        $this->authenticationMethods = $methods;
28
    }
29
30
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
31
    {
32
        foreach ($this->authenticationMethods as $i => $authenticationMethod) {
33
            $identity = $authenticationMethod->authenticate($request);
34
            if ($identity !== null) {
35
                return $identity;
36
            }
37
        }
38
39
        return null;
40
    }
41
42
    public function challenge(ResponseInterface $response): ResponseInterface
43
    {
44
        foreach ($this->authenticationMethods as $method) {
45
            $response = $method->challenge($response);
46
        }
47
        return $response;
48
    }
49
}
50