Passed
Pull Request — master (#409)
by Wilmer
03:49
created

AuthController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Auth\Controller;
6
7
use App\Auth\AuthService;
8
use App\Auth\Form\LoginForm;
9
use App\Service\WebControllerService;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Translator\TranslatorInterface;
14
use Yiisoft\Validator\ValidatorInterface;
15
use Yiisoft\Yii\View\ViewRenderer;
16
17
final class AuthController
18
{
19
    private WebControllerService $webService;
20
    private ViewRenderer $viewRenderer;
21
    private AuthService $authService;
22
23 5
    public function __construct(ViewRenderer $viewRenderer, AuthService $authService, WebControllerService $webService)
24
    {
25 5
        $this->viewRenderer = $viewRenderer->withControllerName('auth');
26 5
        $this->authService = $authService;
27 5
        $this->webService = $webService;
28 5
    }
29
30 5
    public function login(
31
        ServerRequestInterface $request,
32
        TranslatorInterface $translator,
33
        ValidatorInterface $validator
34
    ): ResponseInterface {
35 5
        if (!$this->authService->isGuest()) {
36
            return $this->redirectToMain();
37
        }
38
39 5
        $body = $request->getParsedBody();
40 5
        $loginForm = new LoginForm($this->authService, $translator);
41
42
        if (
43 5
            $request->getMethod() === Method::POST
44 4
            && $loginForm->load(is_array($body) ? $body : [])
45 4
            && $validator->validate($loginForm)->isValid()
46
        ) {
47 1
            return $this->redirectToMain();
48
        }
49
50 5
        return $this->viewRenderer->render('login', ['formModel' => $loginForm]);
51
    }
52
53 1
    public function logout(): ResponseInterface
54
    {
55 1
        $this->authService->logout();
56
57 1
        return $this->redirectToMain();
58
    }
59
60 1
    private function redirectToMain(): ResponseInterface
61
    {
62 1
        return $this->webService->getRedirectResponse('site/index');
63
    }
64
}
65