Passed
Push — master ( ce8c92...7c0481 )
by Alexander
01:29
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
final class Composite implements AuthenticationMethodInterface
16
{
17
    /**
18
     * @var AuthenticationMethodInterface[]
19
     */
20
    private array $authenticationMethods;
21
22
    /**
23
     * @param AuthenticationMethodInterface[] $methods
24
     */
25 5
    public function __construct(array $methods)
26
    {
27 5
        $this->authenticationMethods = $methods;
28 5
    }
29
30 4
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
31
    {
32 4
        foreach ($this->authenticationMethods as $authenticationMethod) {
33 3
            if (!$authenticationMethod instanceof AuthenticationMethodInterface) {
34 1
                throw new \RuntimeException('Authentication method must be an instance of ' . AuthenticationMethodInterface::class . '.');
35
            }
36
37 2
            $identity = $authenticationMethod->authenticate($request);
38 2
            if ($identity !== null) {
39 2
                return $identity;
40
            }
41
        }
42
43 2
        return null;
44
    }
45
46 1
    public function challenge(ResponseInterface $response): ResponseInterface
47
    {
48 1
        foreach ($this->authenticationMethods as $method) {
49 1
            $response = $method->challenge($response);
50
        }
51 1
        return $response;
52
    }
53
}
54