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

Composite::setAuthMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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 2
cts 2
cp 1
crap 1
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 4
    public function __construct(array $methods)
26
    {
27 4
        $this->authenticationMethods = $methods;
28 4
    }
29
30 3
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
31
    {
32 3
        foreach ($this->authenticationMethods as $i => $authenticationMethod) {
33 2
            $identity = $authenticationMethod->authenticate($request);
34 2
            if ($identity !== null) {
35 2
                return $identity;
36
            }
37
        }
38
39 2
        return null;
40
    }
41
42 1
    public function challenge(ResponseInterface $response): ResponseInterface
43
    {
44 1
        foreach ($this->authenticationMethods as $method) {
45 1
            $response = $method->challenge($response);
46
        }
47 1
        return $response;
48
    }
49
}
50