Passed
Push — master ( c10d9f...27f23d )
by Alexander
06:32 queued 04:17
created

UserAuth::withAuthUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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\AuthenticationMethodInterface;
11
use Yiisoft\Auth\IdentityInterface;
12
use Yiisoft\Http\Status;
13
14
final class UserAuth implements AuthenticationMethodInterface
15
{
16
    private string $authUrl = '/login';
17
    private User $user;
18
    private ResponseFactoryInterface $responseFactory;
19
20 5
    public function __construct(User $user, ResponseFactoryInterface $responseFactory)
21
    {
22 5
        $this->user = $user;
23 5
        $this->responseFactory = $responseFactory;
24 5
    }
25
26 2
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
27
    {
28 2
        if ($this->user->isGuest()) {
29 1
            return null;
30
        }
31
32 1
        return $this->user->getIdentity();
33
    }
34
35 2
    public function challenge(ResponseInterface $response): ResponseInterface
36
    {
37 2
        return $this->responseFactory->createResponse(Status::FOUND)->withHeader('Location', $this->authUrl);
38
    }
39
40 2
    public function withAuthUrl(string $url): self
41
    {
42 2
        $new = clone $this;
43 2
        $new->authUrl = $url;
44 2
        return $new;
45
    }
46
}
47