Test Failed
Push — master ( 084381...c83f36 )
by Evgeniy
03:18
created

AuthController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use InvalidArgumentException;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Log\LoggerInterface;
12
use Yiisoft\Auth\IdentityRepositoryInterface;
13
use Yiisoft\Http\Method;
14
use Yiisoft\Http\Status;
15
use Yiisoft\Router\UrlGeneratorInterface;
16
use Yiisoft\User\CurrentUser;
17
use Yiisoft\Yii\View\ViewRenderer;
18
19
class AuthController
20
{
21
    private ResponseFactoryInterface $responseFactory;
22
    private LoggerInterface $logger;
23
    private UrlGeneratorInterface $urlGenerator;
24
    private ViewRenderer $viewRenderer;
25
    private CurrentUser $currentUser;
26
27
    public function __construct(
28
        ResponseFactoryInterface $responseFactory,
29
        ViewRenderer $viewRenderer,
30
        LoggerInterface $logger,
31
        UrlGeneratorInterface $urlGenerator,
32
        CurrentUser $currentUser
33
    ) {
34
        $this->responseFactory = $responseFactory;
35
        $this->logger = $logger;
36
        $this->urlGenerator = $urlGenerator;
37
        $this->viewRenderer = $viewRenderer->withControllerName('auth');
38
        $this->currentUser = $currentUser;
39
    }
40
41
    public function login(
42
        ServerRequestInterface $request,
43
        IdentityRepositoryInterface $identityRepository
44
    ): ResponseInterface {
45
        if (!$this->currentUser->isGuest()) {
46
            return $this->redirectToMain();
47
        }
48
49
        $body = $request->getParsedBody();
50
        $error = null;
51
52
        if ($request->getMethod() === Method::POST) {
53
            try {
54
                foreach (['login', 'password'] as $name) {
55
                    if (empty($body[$name])) {
56
                        throw new InvalidArgumentException(ucfirst($name) . ' is required');
57
                    }
58
                }
59
60
                /** @var \App\User\User $identity */
61
                $identity = $identityRepository->findByLogin($body['login']);
62
63
                if ($identity === null || !$identity->validatePassword($body['password'])) {
64
                    throw new InvalidArgumentException('Invalid login or password');
65
                }
66
67
                if ($this->currentUser->login($identity)) {
68
                    return $this->redirectToMain();
69
                }
70
71
                throw new InvalidArgumentException('Unable to login');
72
            } catch (\Throwable $e) {
73
                $this->logger->error($e);
74
                $error = $e->getMessage();
75
            }
76
        }
77
78
        return $this->viewRenderer->render(
79
            'login',
80
            [
81
                'body' => $body,
82
                'error' => $error,
83
            ]
84
        );
85
    }
86
87
    public function logout(): ResponseInterface
88
    {
89
        $this->currentUser->logout();
90
91
        return $this->redirectToMain();
92
    }
93
94
    private function redirectToMain(): ResponseInterface
95
    {
96
        return $this->responseFactory->createResponse(Status::FOUND)
97
            ->withHeader(
98
                'Location',
99
                $this->urlGenerator->generate('site/index')
100
            );
101
    }
102
}
103