AuthController::redirectToMain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
nc 1
nop 0
cc 1
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\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...
14
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...
15
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...
16
use Yiisoft\Yii\View\Renderer\ViewRenderer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\View\Renderer\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...
17
18
final class AuthController
19
{
20
    public function __construct(
21
        private readonly AuthService $authService,
22
        private readonly WebControllerService $webService,
23
        private ViewRenderer $viewRenderer,
24
    ) {
25
        $this->viewRenderer = $viewRenderer->withControllerName('auth');
26
    }
27
28
    public function login(
29
        ServerRequestInterface $request,
30
        TranslatorInterface $translator,
31
        FormHydrator $formHydrator,
32
        CookieLogin $cookieLogin
33
    ): ResponseInterface {
34
        if (!$this->authService->isGuest()) {
35
            return $this->redirectToMain();
36
        }
37
38
        $loginForm = new LoginForm($this->authService, $translator);
39
40
        if ($formHydrator->populateFromPostAndValidate($loginForm, $request)) {
41
            $identity = $this->authService->getIdentity();
42
43
            if ($identity instanceof CookieLoginIdentityInterface && $loginForm->getPropertyValue('rememberMe')) {
44
                return $cookieLogin->addCookie($identity, $this->redirectToMain());
45
            }
46
47
            return $this->redirectToMain();
48
        }
49
50
        return $this->viewRenderer->render('login', ['formModel' => $loginForm]);
51
    }
52
53
    public function logout(): ResponseInterface
54
    {
55
        $this->authService->logout();
56
57
        return $this->redirectToMain();
58
    }
59
60
    private function redirectToMain(): ResponseInterface
61
    {
62
        return $this->webService->getRedirectResponse('site/index');
63
    }
64
}
65