Passed
Push — master ( 4c95d5...ad70ef )
by Alexander
01:34
created

AuthController::login()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 4
nop 4
dl 0
loc 23
rs 9.6111
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;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Yiisoft\FormModel\FormHydrator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\FormModel\FormHydrator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Http\Method;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Http\Method was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Translator\TranslatorInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Translator\TranslatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Yiisoft\User\Login\Cookie\CookieLogin;
0 ignored issues
show
Bug introduced by
The type Yiisoft\User\Login\Cookie\CookieLogin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Yiisoft\User\Login\Cookie\CookieLoginIdentityInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\User\Login\Cooki...eLoginIdentityInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Yiisoft\Yii\View\ViewRenderer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\View\ViewRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
final class AuthController
20
{
21
    public function __construct(
22
        private readonly AuthService          $authService,
23
        private readonly WebControllerService $webService,
24
        private ViewRenderer                  $viewRenderer,
25
    ) {
26
        $this->viewRenderer = $viewRenderer->withControllerName('auth');
27
    }
28
29
    public function login(
30
        ServerRequestInterface $request,
31
        TranslatorInterface $translator,
32
        FormHydrator $formHydrator,
33
        CookieLogin $cookieLogin
34
    ): ResponseInterface {
35
        if (!$this->authService->isGuest()) {
36
            return $this->redirectToMain();
37
        }
38
39
        $loginForm = new LoginForm($this->authService, $translator);
40
41
        if ($formHydrator->populateFromPostAndValidate($loginForm, $request)) {
42
            $identity = $this->authService->getIdentity();
43
44
            if ($identity instanceof CookieLoginIdentityInterface && $loginForm->getPropertyValue('rememberMe')) {
45
                return $cookieLogin->addCookie($identity, $this->redirectToMain());
46
            }
47
48
            return $this->redirectToMain();
49
        }
50
51
        return $this->viewRenderer->render('login', ['formModel' => $loginForm]);
52
    }
53
54
55
    public function logout(): ResponseInterface
56
    {
57
        $this->authService->logout();
58
59
        return $this->redirectToMain();
60
    }
61
62
    private function redirectToMain(): ResponseInterface
63
    {
64
        return $this->webService->getRedirectResponse('site/index');
65
    }
66
}
67