Composite::challenge()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
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
final class Composite implements AuthenticationMethodInterface
16
{
17
    /**
18
     * @param AuthenticationMethodInterface[] $methods
19
     */
20 5
    public function __construct(private array $methods)
21
    {
22 5
    }
23
24 4
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
25
    {
26 4
        foreach ($this->methods as $method) {
27 3
            if (!$method instanceof AuthenticationMethodInterface) {
28 1
                throw new \RuntimeException('Authentication method must be an instance of ' . AuthenticationMethodInterface::class . '.');
29
            }
30
31 2
            $identity = $method->authenticate($request);
32 2
            if ($identity !== null) {
33 2
                return $identity;
34
            }
35
        }
36
37 2
        return null;
38
    }
39
40 1
    public function challenge(ResponseInterface $response): ResponseInterface
41
    {
42 1
        foreach ($this->methods as $method) {
43 1
            $response = $method->challenge($response);
44
        }
45 1
        return $response;
46
    }
47
}
48