1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Admin\Controller; |
6
|
|
|
|
7
|
|
|
use Admin\Service\AdminUserService; |
8
|
|
|
use Std\AbstractController; |
9
|
|
|
use Std\FilterException; |
10
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
11
|
|
|
use Zend\Expressive\Router\RouterInterface as Router; |
12
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
13
|
|
|
use Zend\Http\PhpEnvironment\Request; |
14
|
|
|
use Zend\Session\SessionManager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class UserController. |
18
|
|
|
*/ |
19
|
|
|
class UserController extends AbstractController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Template |
23
|
|
|
*/ |
24
|
|
|
private $template; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Router |
28
|
|
|
*/ |
29
|
|
|
private $router; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AdminUserService |
33
|
|
|
*/ |
34
|
|
|
private $adminUserService; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var SessionManager |
38
|
|
|
*/ |
39
|
|
|
private $session; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* UserController constructor. |
43
|
|
|
* |
44
|
|
|
* @param Template $template |
45
|
|
|
* @param Router $router |
46
|
|
|
* @param AdminUserService $adminUserService |
47
|
|
|
* @param SessionManager $session |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
50
|
|
|
Template $template, |
51
|
|
|
Router $router, |
52
|
|
|
AdminUserService $adminUserService, |
53
|
|
|
SessionManager $session |
54
|
|
|
) { |
55
|
|
|
$this->template = $template; |
56
|
|
|
$this->router = $router; |
57
|
|
|
$this->adminUserService = $adminUserService; |
58
|
|
|
$this->session = $session; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Users list, exclude current logged in user from the list. |
63
|
|
|
* |
64
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
65
|
|
|
*/ |
66
|
|
|
public function index(): \Psr\Http\Message\ResponseInterface |
67
|
|
|
{ |
68
|
|
|
$user = $this->session->getStorage()->user; |
|
|
|
|
69
|
|
|
$params = $this->request->getQueryParams(); |
70
|
|
|
$page = isset($params['page']) ? $params['page'] : 1; |
71
|
|
|
$limit = isset($params['limit']) ? $params['limit'] : 15; |
72
|
|
|
|
73
|
|
|
$adminUsers = $this->adminUserService->getPagination($page, $limit, $user->admin_user_id); |
74
|
|
|
|
75
|
|
|
return new HtmlResponse($this->template->render( |
76
|
|
|
'admin::user/index', |
77
|
|
|
['list' => $adminUsers, 'layout' => 'layout/admin']) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Edit one user by givven UUID from route. |
83
|
|
|
* |
84
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function edit($errors = []): \Psr\Http\Message\ResponseInterface |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$id = $this->request->getAttribute('id'); |
89
|
|
|
$user = $this->adminUserService->getUser($id); |
90
|
|
|
|
91
|
|
|
if ($this->request->getParsedBody()) { |
92
|
|
|
$user = (object) ($this->request->getParsedBody() + (array) $user); |
93
|
|
|
$user->admin_user_id = $id; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new HtmlResponse( |
97
|
|
|
$this->template->render( |
98
|
|
|
'admin::user/edit', [ |
99
|
|
|
'user' => $user, |
100
|
|
|
'errors' => $errors, |
101
|
|
|
'layout' => 'layout/admin', |
102
|
|
|
] |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function save() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
$userId = $this->request->getAttribute('id'); |
111
|
|
|
$data = $this->request->getParsedBody(); |
112
|
|
|
$data += (new Request())->getFiles()->toArray(); |
113
|
|
|
|
114
|
|
|
if ($userId) { |
115
|
|
|
$this->adminUserService->updateUser($data, $userId); |
116
|
|
|
} else { |
117
|
|
|
$this->adminUserService->registerNewUser($data); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users')); |
121
|
|
|
} catch (FilterException $fe) { |
122
|
|
|
return $this->edit($fe->getArrayMessages()); |
123
|
|
|
} catch (\Exception $e) { |
124
|
|
|
throw $e; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
public function delete() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
try { |
131
|
|
|
$userId = $this->request->getAttribute('id'); |
132
|
|
|
$this->adminUserService->delete($userId); |
133
|
|
|
|
134
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users')); |
135
|
|
|
} catch (\Exception $e) { |
136
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users')); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.