|
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 EditAction |
|
22
|
|
|
* |
|
23
|
|
|
* @author tony |
|
24
|
|
|
*/ |
|
25
|
|
|
class EditAction 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
|
|
|
$id = (int) $request->getAttribute('id'); |
|
49
|
|
|
$user = $this->userRepository->find($id); |
|
50
|
|
|
if (!$user) { |
|
51
|
|
|
$this->logger->warning('Can not find user with id {id}', ['id' => $id]); |
|
52
|
|
|
|
|
53
|
|
|
return (new RedirectResponse('../list'))->redirect(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($request->getMethod() === 'GET') { |
|
57
|
|
|
return new TemplateResponse( |
|
58
|
|
|
$this->template, |
|
59
|
|
|
'user/edit', |
|
60
|
|
|
[ |
|
61
|
|
|
'param' => (new UserParam())->fromEntity($user) |
|
62
|
|
|
] |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$param = new RequestData($request); |
|
67
|
|
|
$formParam = new UserParam($param->posts()); |
|
68
|
|
|
$validator = new UserValidator($formParam); |
|
69
|
|
|
|
|
70
|
|
|
if (!$validator->validate()) { |
|
71
|
|
|
return new TemplateResponse( |
|
72
|
|
|
$this->template, |
|
73
|
|
|
'user/edit', |
|
74
|
|
|
[ |
|
75
|
|
|
'errors' => $validator->getErrors(), |
|
76
|
|
|
'param' => $formParam |
|
77
|
|
|
] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$username = $param->post('username'); |
|
82
|
|
|
$userExist = $this->userRepository->findBy(['username' => $username]); |
|
83
|
|
|
|
|
84
|
|
|
if ($userExist && $userExist->user_id != $id) { |
|
85
|
|
|
$this->logger->error('User with username {username} already exists', ['username' => $username]); |
|
86
|
|
|
return new TemplateResponse( |
|
87
|
|
|
$this->template, |
|
88
|
|
|
'user/edit', |
|
89
|
|
|
[ |
|
90
|
|
|
'param' => $formParam |
|
91
|
|
|
] |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$password = $param->post('password'); |
|
96
|
|
|
|
|
97
|
|
|
$user->username = $formParam->getUsername(); |
|
98
|
|
|
$user->fname = Str::ucfirst($formParam->getFirstname()); |
|
99
|
|
|
$user->lname = Str::upper($formParam->getLastname()); |
|
100
|
|
|
$user->age = (int) $formParam->getAge(); |
|
101
|
|
|
|
|
102
|
|
|
if (!empty($password)) { |
|
103
|
|
|
$hash = new BcryptHash(); |
|
104
|
|
|
$passwordHash = $hash->hash($password); |
|
105
|
|
|
$user->password = $passwordHash; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$result = $this->userRepository->save($user); |
|
109
|
|
|
|
|
110
|
|
|
if (!$result) { |
|
111
|
|
|
$this->logger->error('Error when saved the user'); |
|
112
|
|
|
return new TemplateResponse( |
|
113
|
|
|
$this->template, |
|
114
|
|
|
'user/edit', |
|
115
|
|
|
[ |
|
116
|
|
|
'param' => $formParam |
|
117
|
|
|
] |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return (new RedirectResponse('../list'))->redirect(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|