1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ContactUs\Controller; |
4
|
|
|
|
5
|
|
|
use ContactUs\Entity\ContactUs; |
6
|
|
|
use ContactUs\Service\ContactUsService; |
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 Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Std\AbstractController; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ContactUsController |
16
|
|
|
* |
17
|
|
|
* @package ContactUs |
18
|
|
|
* @author Djordje Stojiljkovic <[email protected]> |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
class ContactUsController extends AbstractController |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \Zend\Expressive\Router\RouterInterface $router |
24
|
|
|
*/ |
25
|
|
|
protected $router; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Zend\Expressive\Template\TemplateRendererInterface $template |
29
|
|
|
*/ |
30
|
|
|
protected $template; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \ContactUs\Service\ContactUsService $contactUsService |
34
|
|
|
*/ |
35
|
|
|
protected $contactUsService; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* ContactUsController constructor. |
39
|
|
|
* |
40
|
|
|
* @param Template $template |
41
|
|
|
* @param Router $router |
42
|
|
|
* @param ContactUsService $contactUsService |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Template $template, Router $router, ContactUsService $contactUsService) |
45
|
|
|
{ |
46
|
|
|
$this->template = $template; |
47
|
|
|
$this->router = $router; |
48
|
|
|
$this->contactUsService = $contactUsService; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return HtmlResponse |
53
|
|
|
*/ |
54
|
|
|
public function index(): HtmlResponse |
55
|
|
|
{ |
56
|
|
|
$params = $this->request->getQueryParams(); |
57
|
|
|
$page = isset($params['page']) ? $params['page'] : 1; |
58
|
|
|
$limit = isset($params['limit']) ? $params['limit'] : 15; |
59
|
|
|
$pagination = $this->contactUsService->getPagination($page, $limit); |
60
|
|
|
|
61
|
|
|
return new HtmlResponse( |
62
|
|
|
$this->template->render( |
63
|
|
|
'contact-us::index', [ |
64
|
|
|
'pagination' => $pagination, |
65
|
|
|
'layout' => 'layout/admin', |
66
|
|
|
] |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $errors |
73
|
|
|
* |
74
|
|
|
* @return HtmlResponse |
75
|
|
|
*/ |
76
|
|
|
public function edit($errors = []): HtmlResponse |
77
|
|
|
{ |
78
|
|
|
$id = $this->request->getAttribute('id'); |
79
|
|
|
$contactUs = $this->contactUsService->getById($id); |
80
|
|
|
|
81
|
|
|
if ($this->request->getParsedBody()) { |
82
|
|
|
$contactUs = new ContactUs(); |
83
|
|
|
$contactUs->exchangeArray( |
84
|
|
|
$this->request->getParsedBody() + (array) $contactUs |
85
|
|
|
); |
86
|
|
|
$contactUs->setContactId($id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return new HtmlResponse( |
90
|
|
|
$this->template->render( |
91
|
|
|
'contact-us::edit', [ |
92
|
|
|
'contact' => $contactUs, |
93
|
|
|
'errors' => $errors, |
94
|
|
|
'layout' => 'layout/admin', |
95
|
|
|
] |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return ResponseInterface |
102
|
|
|
* |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
|
|
public function save(): ResponseInterface |
106
|
|
|
{ |
107
|
|
|
try |
108
|
|
|
{ |
109
|
|
|
$id = $this->request->getAttribute('id'); |
110
|
|
|
$data = $this->request->getParsedBody(); |
111
|
|
|
|
112
|
|
|
if ($id) { |
113
|
|
|
$this->contactUsService->update($data, $id); |
|
|
|
|
114
|
|
|
} else { |
115
|
|
|
$this->contactUsService->create($data); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->response |
119
|
|
|
->withStatus(302) |
120
|
|
|
->withHeader('Location', $this->router->generateUri('admin.contact-us')) |
121
|
|
|
; |
122
|
|
|
|
123
|
|
|
} catch (FilterException $fe) { |
124
|
|
|
return $this->edit($fe->getArrayMessages()); |
125
|
|
|
} catch (\Exception $e) { |
126
|
|
|
throw $e; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return ResponseInterface |
132
|
|
|
*/ |
133
|
|
|
public function delete(): ResponseInterface |
134
|
|
|
{ |
135
|
|
|
try |
136
|
|
|
{ |
137
|
|
|
$id = $this->request->getAttribute('id'); |
138
|
|
|
$this->contactUsService->delete($id); |
139
|
|
|
|
140
|
|
|
return $this->response |
141
|
|
|
->withStatus(302) |
142
|
|
|
->withHeader('Location', $this->router->generateUri('admin.contact-us')) |
143
|
|
|
; |
144
|
|
|
|
145
|
|
|
} catch (\Exception $e) { |
146
|
|
|
return $this->response |
147
|
|
|
->withStatus(302) |
148
|
|
|
->withHeader('Location', $this->router->generateUri('admin.contact-us')) |
149
|
|
|
; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
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.