1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Page\Controller; |
4
|
|
|
|
5
|
|
|
use Page\Service\PageService; |
6
|
|
|
use Std\AbstractController; |
7
|
|
|
use Std\FilterException; |
8
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
9
|
|
|
use Zend\Expressive\Router\RouterInterface as Router; |
10
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
11
|
|
|
use Zend\Http\PhpEnvironment\Request; |
12
|
|
|
|
13
|
|
|
class PageController extends AbstractController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Template |
17
|
|
|
*/ |
18
|
|
|
private $template; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Router |
22
|
|
|
*/ |
23
|
|
|
private $router; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var PageService |
27
|
|
|
*/ |
28
|
|
|
private $pageService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* PageController constructor. |
32
|
|
|
* |
33
|
|
|
* @param Template $template |
34
|
|
|
* @param Router $router |
35
|
|
|
* @param PageService $pageService |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Template $template, Router $router, PageService $pageService) |
38
|
|
|
{ |
39
|
|
|
$this->template = $template; |
40
|
|
|
$this->router = $router; |
41
|
|
|
$this->pageService = $pageService; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return HtmlResponse |
46
|
|
|
*/ |
47
|
|
|
public function index(): HtmlResponse |
48
|
|
|
{ |
49
|
|
|
$params = $this->request->getQueryParams(); |
50
|
|
|
$page = isset($params['page']) ? $params['page'] : 1; |
51
|
|
|
$limit = isset($params['limit']) ? $params['limit'] : 15; |
52
|
|
|
$pagination = $this->pageService->getPagination($page, $limit); |
53
|
|
|
|
54
|
|
|
return new HtmlResponse( |
55
|
|
|
$this->template->render( |
56
|
|
|
'page::index', [ |
57
|
|
|
'pagination' => $pagination, |
58
|
|
|
'layout' => 'layout/admin', |
59
|
|
|
] |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function edit($errors = []): HtmlResponse |
65
|
|
|
{ |
66
|
|
|
$id = $this->request->getAttribute('id'); |
67
|
|
|
$page = $this->pageService->getPage($id); |
68
|
|
|
|
69
|
|
|
if ($this->request->getParsedBody()) { |
70
|
|
|
$page = new \Page\Entity\Page(); |
71
|
|
|
$page->exchangeArray($this->request->getParsedBody() + (array) $page); |
72
|
|
|
$page->setPageId($id); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return new HtmlResponse( |
76
|
|
|
$this->template->render( |
77
|
|
|
'page::edit', [ |
78
|
|
|
'page' => $page, |
79
|
|
|
'errors' => $errors, |
80
|
|
|
'layout' => 'layout/admin', |
81
|
|
|
] |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function save(): \Psr\Http\Message\ResponseInterface |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
$pageId = $this->request->getAttribute('id'); |
90
|
|
|
$data = $this->request->getParsedBody(); |
91
|
|
|
$data += (new Request())->getFiles()->toArray(); |
92
|
|
|
|
93
|
|
|
if ($pageId) { |
94
|
|
|
$this->pageService->updatePage($data, $pageId); |
95
|
|
|
} else { |
96
|
|
|
$this->pageService->createPage($data); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.pages')); |
100
|
|
|
} catch (FilterException $fe) { |
101
|
|
|
return $this->edit($fe->getArrayMessages()); |
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
throw $e; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function delete(): \Psr\Http\Message\ResponseInterface |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
$pageId = $this->request->getAttribute('id'); |
111
|
|
|
$this->pageService->delete($pageId); |
112
|
|
|
|
113
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.pages')); |
114
|
|
|
} catch (\Exception $e) { |
115
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.pages')); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.