|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Admin\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
|
8
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
9
|
|
|
use Core\Service\AdminUserService; |
|
10
|
|
|
use Zend\Session\SessionManager; |
|
11
|
|
|
use Zend\Expressive\Router\RouterInterface as Router; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class UserController. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Admin\Controller |
|
17
|
|
|
*/ |
|
18
|
|
|
class UserController extends AbstractController |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Template |
|
22
|
|
|
*/ |
|
23
|
|
|
private $template; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Router |
|
27
|
|
|
*/ |
|
28
|
|
|
private $router; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var AdminUserService |
|
32
|
|
|
*/ |
|
33
|
|
|
private $adminUserService; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var SessionManager |
|
37
|
|
|
*/ |
|
38
|
|
|
private $session; |
|
39
|
|
|
|
|
40
|
|
|
CONST DEFAUTL_LIMIT = 15; |
|
41
|
|
|
CONST DEFAUTL_PAGE = 1; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* UserController constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Template $template |
|
47
|
|
|
* @param AdminUserService $adminUserService |
|
48
|
|
|
* @param SessionManager $session |
|
49
|
|
|
*/ |
|
50
|
|
View Code Duplication |
public function __construct(Template $template, Router $router, AdminUserService $adminUserService, SessionManager $session) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$this->template = $template; |
|
53
|
|
|
$this->router = $router; |
|
54
|
|
|
$this->adminUserService = $adminUserService; |
|
55
|
|
|
$this->session = $session; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Users list, exclude current logged in user from the list. |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
public function index() : \Psr\Http\Message\ResponseInterface |
|
64
|
|
|
{ |
|
65
|
|
|
$user = $this->session->getStorage()->user; |
|
|
|
|
|
|
66
|
|
|
$params = $this->request->getQueryParams(); |
|
67
|
|
|
$page = isset($params['page']) ? $params['page'] : self::DEFAUTL_PAGE; |
|
68
|
|
|
$limit = isset($params['limit']) ? $params['limit'] : self::DEFAUTL_LIMIT; |
|
69
|
|
|
|
|
70
|
|
|
//$filter = [ |
|
71
|
|
|
// 'first_name' => isset($params['first_name']) ? $params['first_name'] : '', |
|
|
|
|
|
|
72
|
|
|
// add filters ... |
|
73
|
|
|
//]; |
|
74
|
|
|
|
|
75
|
|
|
$adminUsers = $this->adminUserService->getPagination($page, $limit, $user->admin_user_uuid); |
|
76
|
|
|
|
|
77
|
|
|
return new HtmlResponse($this->template->render('admin::user/index', ['list' => $adminUsers])); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Edit one user by givven UUID from route. |
|
82
|
|
|
* |
|
83
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
public function edit(): \Psr\Http\Message\ResponseInterface |
|
86
|
|
|
{ |
|
87
|
|
|
$userId = $this->request->getAttribute('id'); |
|
88
|
|
|
$user = $this->adminUserService->getUser($userId); |
|
89
|
|
|
|
|
90
|
|
|
return new HtmlResponse($this->template->render('admin::user/edit', ['user' => $user])); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function doedit() |
|
94
|
|
|
{ |
|
95
|
|
|
try{ |
|
96
|
|
|
$userId = $this->request->getAttribute('id'); |
|
97
|
|
|
$data = $this->request->getParsedBody(); |
|
98
|
|
|
|
|
99
|
|
|
$this->adminUserService->save($data, $userId); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users')); |
|
102
|
|
|
} |
|
103
|
|
|
catch(\Exception $e){ |
|
104
|
|
|
return $this->response->withStatus(302)->withHeader( |
|
105
|
|
|
'Location', |
|
106
|
|
|
$this->router->generateUri('admin.users.action', ['action' => 'edit', 'id' => $userId]) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function delete() |
|
112
|
|
|
{ |
|
113
|
|
|
try{ |
|
114
|
|
|
$userId = $this->request->getAttribute('id'); |
|
115
|
|
|
$this->adminUserService->delete($userId); |
|
116
|
|
|
|
|
117
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users')); |
|
118
|
|
|
} |
|
119
|
|
|
catch(\Exception $e){ |
|
120
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users')); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
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.