Passed
Pull Request — master (#396)
by Wilmer
03:46
created

AuthController::login()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8.0093

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
c 3
b 0
f 0
nc 5
nop 4
dl 0
loc 37
ccs 18
cts 19
cp 0.9474
crap 8.0093
rs 8.4444
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
        ServerRequestInterface $request,
43
        TranslatorInterface $translator,
44
        ValidatorInterface $validator
45
    ): ResponseInterface {
46 5
        if (!$this->currentUser->isGuest()) {
47
            return $this->redirectToMain();
48
        }
49
50
        /** @var array */
51 5
        $body = $request->getParsedBody();
52 5
        $error = null;
53
54 5
        $loginForm = new LoginForm($translator);
55
56
        if (
57 5
            $request->getMethod() === Method::POST
58 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

58
            && $loginForm->load(/** @scrutinizer ignore-type */ $body)
Loading history...
59 4
            && $validator->validate($loginForm)->isValid()
60
        ) {
61
            /** @var User $identity */
62 3
            $identity = $identityRepository->findByLogin($loginForm->getAttributeValue('login'));
63
64 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

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