Passed
Pull Request — master (#396)
by Wilmer
06:08
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 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 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Form\LoginForm;
8
use App\User\User;
9
use Psr\Http\Message\ResponseFactoryInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Yiisoft\Auth\IdentityRepositoryInterface;
13
use Yiisoft\Http\Method;
14
use Yiisoft\Http\Status;
15
use Yiisoft\Router\UrlGeneratorInterface;
16
use Yiisoft\Translator\TranslatorInterface;
17
use Yiisoft\User\CurrentUser;
18
use Yiisoft\Validator\ValidatorInterface;
19
use Yiisoft\Yii\View\ViewRenderer;
20
21
class AuthController
22
{
23
    private ResponseFactoryInterface $responseFactory;
24
    private UrlGeneratorInterface $urlGenerator;
25
    private ViewRenderer $viewRenderer;
26
    private CurrentUser $currentUser;
27
28 5
    public function __construct(
29
        ResponseFactoryInterface $responseFactory,
30
        ViewRenderer $viewRenderer,
31
        UrlGeneratorInterface $urlGenerator,
32
        CurrentUser $currentUser
33
    ) {
34 5
        $this->responseFactory = $responseFactory;
35 5
        $this->urlGenerator = $urlGenerator;
36 5
        $this->viewRenderer = $viewRenderer->withControllerName('auth');
37 5
        $this->currentUser = $currentUser;
38 5
    }
39
40 5
    public function login(
41
        IdentityRepositoryInterface $identityRepository,
42
        LoginForm $loginForm,
43
        ServerRequestInterface $request,
44
        TranslatorInterface $translator,
45
        ValidatorInterface $validator
46
    ): ResponseInterface {
47 5
        if (!$this->currentUser->isGuest()) {
48
            return $this->redirectToMain();
49
        }
50
51
        /** @var array */
52 5
        $body = $request->getParsedBody();
53 5
        $error = null;
54
55
        if (
56 5
            $request->getMethod() === Method::POST
57 4
            && $loginForm->load($body)
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type null and object; however, parameter $data of Yiisoft\Form\FormModel::load() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            && $loginForm->load(/** @scrutinizer ignore-type */ $body)
Loading history...
58 4
            && $validator->validate($loginForm)->isValid()
59
        ) {
60
            /** @var User $identity */
61 3
            $identity = $identityRepository->findByLogin($loginForm->getAttributeValue('login'));
62
63 3
            if ($identity === null || !$identity->validatePassword($loginForm->getAttributeValue('password'))) {
0 ignored issues
show
Bug introduced by
It seems like $loginForm->getAttributeValue('password') can also be of type boolean and iterable and null; however, parameter $password of App\User\User::validatePassword() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            if ($identity === null || !$identity->validatePassword(/** @scrutinizer ignore-type */ $loginForm->getAttributeValue('password'))) {
Loading history...
64 2
                $loginForm->getFormErrors()->addError('password', $translator->translate('Invalid login or password'));
65 1
            } elseif ($this->currentUser->login($identity)) {
66 1
                return $this->redirectToMain();
67
            }
68
        }
69
70 5
        return $this->viewRenderer->render(
71 5
            'login',
72
            [
73 5
                'body' => $body,
74 5
                'formModel' => $loginForm,
75 5
                'error' => $error,
76
            ]
77
        );
78
    }
79
80 1
    public function logout(): ResponseInterface
81
    {
82 1
        $this->currentUser->logout();
83
84 1
        return $this->redirectToMain();
85
    }
86
87 1
    private function redirectToMain(): ResponseInterface
88
    {
89 1
        return $this->responseFactory->createResponse(Status::FOUND)
90 1
            ->withHeader(
91 1
                'Location',
92 1
                $this->urlGenerator->generate('site/index')
93
            );
94
    }
95
}
96