1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Article\Controller; |
6
|
|
|
|
7
|
|
|
use Article\Entity\ArticleType; |
8
|
|
|
use Article\Service\DiscussionService; |
9
|
|
|
use Category\Service\CategoryService; |
10
|
|
|
use Std\AbstractController; |
11
|
|
|
use Std\FilterException; |
12
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
13
|
|
|
use Zend\Expressive\Router\RouterInterface as Router; |
14
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
15
|
|
|
use Zend\Session\SessionManager; |
16
|
|
|
|
17
|
|
View Code Duplication |
class DiscussionController extends AbstractController |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Template |
21
|
|
|
*/ |
22
|
|
|
private $template; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Router |
26
|
|
|
*/ |
27
|
|
|
private $router; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DiscussionService |
31
|
|
|
*/ |
32
|
|
|
private $discussionService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var SessionManager |
36
|
|
|
*/ |
37
|
|
|
private $session; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var CategoryService |
41
|
|
|
*/ |
42
|
|
|
private $categoryService; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* DiscussionController constructor. |
46
|
|
|
* |
47
|
|
|
* @param Template $template |
48
|
|
|
* @param Router $router |
49
|
|
|
* @param DiscussionService $discussionService |
50
|
|
|
* @param SessionManager $session |
51
|
|
|
* @param CategoryService $categoryService |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
Template $template, |
55
|
|
|
Router $router, |
56
|
|
|
DiscussionService $discussionService, |
57
|
|
|
SessionManager $session, |
58
|
|
|
CategoryService $categoryService |
59
|
|
|
) { |
60
|
|
|
$this->template = $template; |
61
|
|
|
$this->router = $router; |
62
|
|
|
$this->discussionService = $discussionService; |
63
|
|
|
$this->session = $session; |
64
|
|
|
$this->categoryService = $categoryService; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Displays a list of discussions. |
69
|
|
|
* |
70
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
71
|
|
|
*/ |
72
|
|
|
public function index(): \Psr\Http\Message\ResponseInterface |
73
|
|
|
{ |
74
|
|
|
$params = $this->request->getQueryParams(); |
75
|
|
|
$page = isset($params['page']) ? $params['page'] : 1; |
76
|
|
|
$limit = isset($params['limit']) ? $params['limit'] : 15; |
77
|
|
|
|
78
|
|
|
$discussions = $this->discussionService->fetchAllArticles($page, $limit); |
79
|
|
|
|
80
|
|
|
return new HtmlResponse($this->template->render( |
81
|
|
|
'article::discussion/index', |
82
|
|
|
['list' => $discussions, 'layout' => 'layout/admin']) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Displays a discussion edit form, with data from request, if any. |
88
|
|
|
* |
89
|
|
|
* @param array $errors errors for displaying to user |
90
|
|
|
* |
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
92
|
|
|
*/ |
93
|
|
|
public function edit($errors = []): \Psr\Http\Message\ResponseInterface |
94
|
|
|
{ |
95
|
|
|
$id = $this->request->getAttribute('id'); |
96
|
|
|
$discussion = $this->discussionService->fetchSingleArticle($id); |
97
|
|
|
$categories = $this->categoryService->getAll(ArticleType::DISCUSSION); |
98
|
|
|
|
99
|
|
|
if ($this->request->getParsedBody()) { |
100
|
|
|
$discussion = (object) ($this->request->getParsedBody() + (array) $discussion); |
101
|
|
|
$discussion->article_id = $id; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return new HtmlResponse( |
105
|
|
|
$this->template->render( |
106
|
|
|
'article::discussion/edit', [ |
107
|
|
|
'discussion' => $discussion, |
108
|
|
|
'categories' => $categories, |
109
|
|
|
'errors' => $errors, |
110
|
|
|
'layout' => 'layout/admin', |
111
|
|
|
] |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function save() |
117
|
|
|
{ |
118
|
|
|
try { |
119
|
|
|
$id = $this->request->getAttribute('id'); |
120
|
|
|
$data = $this->request->getParsedBody(); |
121
|
|
|
$user = $this->session->getStorage()->user; |
|
|
|
|
122
|
|
|
|
123
|
|
|
if ($id) { |
124
|
|
|
$this->discussionService->updateArticle($data, $id); |
125
|
|
|
} else { |
126
|
|
|
$this->discussionService->createArticle($user, $data); |
127
|
|
|
} |
128
|
|
|
} catch (FilterException $fe) { |
129
|
|
|
return $this->edit($fe->getArrayMessages()); |
130
|
|
|
} catch (\Exception $e) { |
131
|
|
|
throw $e; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->response->withStatus(302)->withHeader( |
135
|
|
|
'Location', |
136
|
|
|
$this->router->generateUri('admin.discussions') |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function delete() |
141
|
|
|
{ |
142
|
|
|
try { |
143
|
|
|
$this->discussionService->deleteArticle($this->request->getAttribute('id')); |
144
|
|
|
} catch (\Exception $e) { |
145
|
|
|
throw $e; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->response->withStatus(302)->withHeader( |
149
|
|
|
'Location', $this->router->generateUri('admin.discussions') |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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.