Test Failed
Push — develop ( ddb817...815500 )
by nguereza
02:28
created

CreateAction::handle()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 70
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 41
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 70
rs 8.9528

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
namespace Platine\Framework\Demo\Action\User;
4
5
use Platine\Framework\Demo\Form\Param\UserParam;
6
use Platine\Framework\Demo\Form\Validator\UserValidator;
7
use Platine\Framework\Demo\Repository\UserRepository;
8
use Platine\Framework\Demo\Response\RedirectResponse;
9
use Platine\Framework\Demo\Response\TemplateResponse;
10
use Platine\Framework\Http\RequestData;
11
use Platine\Http\Handler\RequestHandlerInterface;
12
use Platine\Http\ResponseInterface;
13
use Platine\Http\ServerRequestInterface;
14
use Platine\Logger\LoggerInterface;
15
use Platine\Security\Hash\BcryptHash;
16
use Platine\Session\Session;
17
use Platine\Stdlib\Helper\Str;
18
use Platine\Template\Template;
19
20
/**
21
 * Description of CreateAction
22
 *
23
 * @author tony
24
 */
25
class CreateAction implements RequestHandlerInterface
26
{
27
28
    protected LoggerInterface $logger;
29
    protected Session $session;
30
    protected UserRepository $userRepository;
31
    protected Template $template;
32
33
34
    public function __construct(
35
        LoggerInterface $logger,
36
        Session $session,
37
        Template $template,
38
        UserRepository $userRepository
39
    ) {
40
        $this->logger = $logger;
41
        $this->session = $session;
42
        $this->userRepository = $userRepository;
43
        $this->template = $template;
44
    }
45
46
    public function handle(ServerRequestInterface $request): ResponseInterface
47
    {
48
        if ($request->getMethod() === 'GET') {
49
            return new TemplateResponse(
50
                $this->template,
51
                'user/create',
52
                [
53
                    'param' => new UserParam([])
54
                ]
55
            );
56
        }
57
58
        $param = new RequestData($request);
59
        $formParam = new UserParam($param->posts());
60
        $validator = new UserValidator($formParam);
61
62
        if (!$validator->validate()) {
63
            return new TemplateResponse(
64
                $this->template,
65
                'user/create',
66
                [
67
                    'errors' => $validator->getErrors(),
68
                    'param' => $formParam
69
                ]
70
            );
71
        }
72
73
        $username = $param->post('username');
74
        $userExist = $this->userRepository->findBy(['username' => $username]);
75
76
        if ($userExist) {
77
            $this->logger->error('User with username {username} already exists', ['username' => $username]);
78
            return new TemplateResponse(
79
                $this->template,
80
                'user/create',
81
                [
82
                   'param' => $formParam
83
                ]
84
            );
85
        }
86
87
        $password = $param->post('password');
88
89
        $hash = new BcryptHash();
90
        $passwordHash = $hash->hash($password);
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type null; however, parameter $plain of Platine\Security\Hash\BcryptHash::hash() 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

90
        $passwordHash = $hash->hash(/** @scrutinizer ignore-type */ $password);
Loading history...
91
92
        $user = $this->userRepository->create([
93
            'username' => $formParam->getUsername(),
94
            'fname' => Str::ucfirst($formParam->getFirstname()),
95
            'lname' => Str::upper($formParam->getLastname()),
96
            'password' => $passwordHash,
97
            'status' => 1,
98
            'age' => (int) $formParam->getAge(),
99
            'deleted' => 0,
100
        ]);
101
102
        $result = $this->userRepository->save($user);
103
104
        if (!$result) {
105
            $this->logger->error('Error when saved the user');
106
            return new TemplateResponse(
107
                $this->template,
108
                'user/create',
109
                [
110
                   'param' => $formParam
111
                ]
112
            );
113
        }
114
115
        return (new RedirectResponse('list'))->redirect();
116
    }
117
}
118