Passed
Push — master ( 11848a...152feb )
by nguereza
13:16 queued 12s
created

AuthAction::handle()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 35
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 57
rs 9.0488

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Platine\App\Http\Action\User;
6
7
use Platine\App\Param\AuthParam;
8
use Platine\App\Validator\AuthValidator;
9
use Platine\Framework\Auth\AuthenticationInterface;
10
use Platine\Framework\Auth\Exception\AuthenticationException;
11
use Platine\Framework\Helper\ActionHelper;
12
use Platine\Framework\Http\Action\BaseAction;
13
use Platine\Framework\Http\Response\RedirectResponse;
14
use Platine\Http\ResponseInterface;
15
16
/**
17
* @class AuthAction
18
* @package Platine\App\Http\Action\User
19
* @template T
20
* @extends BaseAction<T>
21
*/
22
class AuthAction extends BaseAction
23
{
24
    /**
25
    * Create new instance
26
    * @param AuthenticationInterface $authentication
27
    * @param ActionHelper<T> $actionHelper
28
    */
29
    public function __construct(
30
        protected AuthenticationInterface $authentication,
31
        ActionHelper $actionHelper,
32
    ) {
33
        parent::__construct($actionHelper);
34
    }
35
36
    /**
37
    * {@inheritdoc}
38
    */
39
    public function respond(): ResponseInterface
40
    {
41
        $this->setView('user/login');
42
        $request = $this->request;
43
        $param = $this->param;
44
45
        $formParam = new AuthParam($param->posts());
46
        $this->addContext('param', $formParam);
47
48
        if ($request->getMethod() === 'GET') {
49
            return $this->viewResponse();
50
        }
51
52
        $validator = new AuthValidator($formParam, $this->lang);
53
        if ($validator->validate() === false) {
54
            $this->addContext('errors', $validator->getErrors());
55
56
            return $this->viewResponse();
57
        }
58
59
        $username = $formParam->getUsername();
60
        $password = $formParam->getPassword();
61
62
        $credentials = [
63
            'username' => $username,
64
            'password' => $password,
65
        ];
66
67
        try {
68
            $this->authentication->login($credentials);
69
        } catch (AuthenticationException $ex) {
70
            $this->logger->error('Authentication error: {error}', [
71
                'error' => $ex->getMessage()
72
            ]);
73
74
            $this->flash->setError('Authentication error. Please check your login and/or password.');
75
76
            return $this->viewResponse();
77
        }
78
79
        $returnUrl = $this->routeHelper->generateUrl('home');
80
        if ($param->get('next')) {
81
            $returnUrl = $param->get('next');
82
        }
83
84
        return new RedirectResponse($returnUrl);
85
    }
86
}
87