Composite   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 11
c 2
b 0
f 0
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 14 4
A challenge() 0 6 2
A __construct() 0 2 1
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