1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Category\Controller; |
6
|
|
|
|
7
|
|
|
use Category\Service\CategoryService; |
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
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class IndexController. |
17
|
|
|
*/ |
18
|
|
|
class IndexController extends AbstractController |
19
|
|
|
{ |
20
|
|
|
/** @var Template */ |
21
|
|
|
private $template; |
22
|
|
|
|
23
|
|
|
/** @var Router */ |
24
|
|
|
private $router; |
25
|
|
|
|
26
|
|
|
/** @var CategoryService */ |
27
|
|
|
private $categoryService; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* IndexController constructor. |
31
|
|
|
* |
32
|
|
|
* @param Template $template template engine |
33
|
|
|
* @param Router $router |
34
|
|
|
* @param CategoryService $categoryService |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
Template $template, |
38
|
|
|
Router $router, |
39
|
|
|
CategoryService $categoryService |
40
|
|
|
) { |
41
|
|
|
$this->template = $template; |
42
|
|
|
$this->router = $router; |
43
|
|
|
$this->categoryService = $categoryService; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Category list. |
48
|
|
|
* |
49
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
50
|
|
|
*/ |
51
|
|
|
public function index(): \Psr\Http\Message\ResponseInterface |
52
|
|
|
{ |
53
|
|
|
$params = $this->request->getQueryParams(); |
54
|
|
|
$page = isset($params['page']) ? $params['page'] : 1; |
55
|
|
|
$limit = isset($params['limit']) ? $params['limit'] : 15; |
56
|
|
|
|
57
|
|
|
$categories = $this->categoryService->getPagination($page, $limit); |
58
|
|
|
|
59
|
|
|
return new HtmlResponse($this->template->render('category::index/index', [ |
60
|
|
|
'list' => $categories, |
61
|
|
|
'layout' => 'layout/admin', |
62
|
|
|
])); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Edit one user by givven UUID from route. |
67
|
|
|
* |
68
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function edit($errors = []): \Psr\Http\Message\ResponseInterface |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$id = $this->request->getAttribute('id'); |
73
|
|
|
$category = $this->categoryService->getCategory($id); |
74
|
|
|
|
75
|
|
|
if ($this->request->getParsedBody()) { |
76
|
|
|
$category = (object) ($this->request->getParsedBody() + (array) $category); |
77
|
|
|
$category->category_id = $id; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new HtmlResponse( |
81
|
|
|
$this->template->render('category::index/edit', [ |
82
|
|
|
'category' => $category, |
83
|
|
|
'errors' => $errors, |
84
|
|
|
'layout' => 'layout/admin', |
85
|
|
|
] |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function save() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$id = $this->request->getAttribute('id'); |
94
|
|
|
$data = $this->request->getParsedBody(); |
95
|
|
|
$data += (new Request())->getFiles()->toArray(); |
96
|
|
|
|
97
|
|
|
if ($id) { |
98
|
|
|
$this->categoryService->updateCategory($data, $id); |
99
|
|
|
} else { |
100
|
|
|
$this->categoryService->createCategory($data); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->response->withStatus(302) |
104
|
|
|
->withHeader('Location', $this->router->generateUri('admin.categories')); |
105
|
|
|
} catch (FilterException $fe) { |
106
|
|
|
return $this->edit($fe->getArrayMessages()); |
107
|
|
|
} catch (\Exception $e) { |
108
|
|
|
throw $e; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function delete() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
$id = $this->request->getAttribute('id'); |
116
|
|
|
$this->categoryService->delete($id); |
117
|
|
|
|
118
|
|
|
return $this->response->withStatus(302)->withHeader('Location', |
119
|
|
|
$this->router->generateUri('admin.categories') |
120
|
|
|
); |
121
|
|
|
} catch (\Exception $e) { |
122
|
|
|
return $this->response->withStatus(302)->withHeader( |
123
|
|
|
'Location', |
124
|
|
|
$this->router->generateUri('admin.categories') |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.