Test Failed
Push — develop ( 64b6b0...ddb817 )
by nguereza
02:24
created

CreateAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
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\Template\Template;
18
19
/**
20
 * Description of CreateAction
21
 *
22
 * @author tony
23
 */
24
class CreateAction implements RequestHandlerInterface
25
{
26
27
    protected LoggerInterface $logger;
28
    protected Session $session;
29
    protected UserRepository $userRepository;
30
    protected Template $template;
31
32
33
    public function __construct(
34
        LoggerInterface $logger,
35
        Session $session,
36
        Template $template,
37
        UserRepository $userRepository
38
    ) {
39
        $this->logger = $logger;
40
        $this->session = $session;
41
        $this->userRepository = $userRepository;
42
        $this->template = $template;
43
    }
44
45
    public function handle(ServerRequestInterface $request): ResponseInterface
46
    {
47
        if ($request->getMethod() === 'GET') {
48
            return new TemplateResponse(
49
                $this->template,
50
                'user/create',
51
                [
52
                    'param' => new UserParam([])
53
                ]
54
            );
55
        }
56
57
        $param = new RequestData($request);
58
        $formParam = new UserParam($param->posts());
59
        $validator = new UserValidator($formParam);
60
61
        if (!$validator->validate()) {
62
            return new TemplateResponse(
63
                $this->template,
64
                'user/create',
65
                [
66
                    'errors' => $validator->getErrors(),
67
                    'param' => $formParam
68
                ]
69
            );
70
        }
71
72
        $username = $param->post('username');
73
        $userExist = $this->userRepository->findBy(['username' => $username]);
74
75
        if ($userExist) {
76
            $this->logger->error('User with username {username} already exists', ['username' => $username]);
77
            return new TemplateResponse(
78
                $this->template,
79
                'user/create',
80
                [
81
                   'param' => $formParam
82
                ]
83
            );
84
        }
85
86
        $password = $param->post('password');
87
88
        $hash = new BcryptHash();
89
        $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

89
        $passwordHash = $hash->hash(/** @scrutinizer ignore-type */ $password);
Loading history...
90
91
        $user = $this->userRepository->create([
92
            'username' => $formParam->getUsername(),
93
            'fname' => $formParam->getFirstname(),
94
            'lname' => $formParam->getLastname(),
95
            'password' => $passwordHash,
96
            'status' => 1,
97
            'age' => (int) $formParam->getAge(),
98
            'deleted' => 0,
99
        ]);
100
101
        $this->userRepository->save($user);
102
103
        return (new RedirectResponse('list'))->redirect();
104
    }
105
}
106