Passed
Push — master ( 4af9a6...57ed85 )
by Alexander
02:04
created

UserAuth   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 11
cp 0
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A authenticate() 0 7 2
A challenge() 0 3 1
A setAuthUrl() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Web\User;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Yiisoft\Auth\AuthInterface;
11
use Yiisoft\Auth\IdentityInterface;
12
use Yiisoft\Http\Status;
13
14
final class UserAuth implements AuthInterface
15
{
16
    private string $authUrl = '/login';
17
    private User $user;
18
    private ResponseFactoryInterface $responseFactory;
19
20
    public function __construct(User $user, ResponseFactoryInterface $responseFactory)
21
    {
22
        $this->user = $user;
23
        $this->responseFactory = $responseFactory;
24
    }
25
26
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
27
    {
28
        if ($this->user->isGuest()) {
29
            return null;
30
        }
31
32
        return $this->user->getIdentity();
33
    }
34
35
    public function challenge(ResponseInterface $response): ResponseInterface
36
    {
37
        return $this->responseFactory->createResponse(Status::FOUND)->withHeader('Location', $this->authUrl);
38
    }
39
40
    public function setAuthUrl(string $url): void
41
    {
42
        $this->authUrl = $url;
43
    }
44
}
45