Passed
Push — master ( ce8c92...7c0481 )
by Alexander
01:29
created

QueryParameter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A withParameterName() 0 5 1
A __construct() 0 3 1
A authenticate() 0 8 2
A challenge() 0 3 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
use Yiisoft\Auth\IdentityRepositoryInterface;
12
13
/**
14
 * QueryParameter supports the authentication based on the access token passed through a query parameter.
15
 */
16
final class QueryParameter implements AuthenticationMethodInterface
17
{
18
    private string $parameterName = 'access-token';
19
20
    private IdentityRepositoryInterface $identityRepository;
21
22 9
    public function __construct(IdentityRepositoryInterface $identityRepository)
23
    {
24 9
        $this->identityRepository = $identityRepository;
25 9
    }
26
27 6
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
28
    {
29 6
        $accessToken = $request->getQueryParams()[$this->parameterName] ?? null;
30 6
        if (is_string($accessToken)) {
31 5
            return $this->identityRepository->findIdentityByToken($accessToken, self::class);
32
        }
33
34 3
        return null;
35
    }
36
37 2
    public function challenge(ResponseInterface $response): ResponseInterface
38
    {
39 2
        return $response;
40
    }
41
42
    /**
43
     * @param string $name The parameter name for passing the access token.
44
     * @return self
45
     */
46 2
    public function withParameterName(string $name): self
47
    {
48 2
        $new = clone $this;
49 2
        $new->parameterName = $name;
50 2
        return $new;
51
    }
52
}
53