Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PostVoteController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PostVoteController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class PostVoteController extends GameController |
||
9 | { |
||
10 | /** |
||
11 | * @var gameService |
||
12 | */ |
||
13 | protected $gameService; |
||
14 | |||
15 | public function __construct(ServiceLocatorInterface $locator) |
||
19 | |||
20 | /** |
||
21 | * --DONE-- 1. try to change the Game Id (on le redirige vers la home du jeu) |
||
22 | * --DONE-- 2. try to modify questions (the form is recreated and verified in the controller) |
||
23 | * --DONE-- 3. don't answer to questions (form is checked controller side) |
||
24 | * 4. try to game the chrono |
||
25 | * 5. try to play again |
||
26 | * 6. try to change answers |
||
27 | * --DONE-- 7. essaie de répondre sans être inscrit (on le redirige vers la home du jeu) |
||
28 | */ |
||
29 | public function playAction() |
||
30 | { |
||
31 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
32 | |||
33 | if (!$entry) { |
||
34 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
35 | if ($lastEntry === null) { |
||
36 | return $this->redirect()->toUrl( |
||
37 | $this->frontendUrl()->fromRoute( |
||
|
|||
38 | 'postvote', |
||
39 | array('id' => $this->game->getIdentifier()) |
||
40 | ) |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | $lastEntryId = $lastEntry->getId(); |
||
45 | $lastPost = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntryId)); |
||
46 | $postId = $lastPost->getId(); |
||
47 | if ($lastPost->getStatus() == 2) { |
||
48 | // the user has already taken part to this game and the participation limit has been reached |
||
49 | $this->flashMessenger()->addMessage( |
||
50 | $this->getServiceLocator()->get('MvcTranslator')->translate('You have already a Post') |
||
51 | ); |
||
52 | |||
53 | return $this->redirect()->toUrl( |
||
54 | $this->frontendUrl()->fromRoute( |
||
55 | 'postvote/post', |
||
56 | array( |
||
57 | 'id' => $this->game->getIdentifier(), |
||
58 | 'post' => $postId, |
||
59 | ) |
||
60 | ) |
||
61 | ); |
||
62 | View Code Duplication | } else { |
|
63 | $this->flashMessenger()->addMessage( |
||
64 | $this->getServiceLocator()->get('MvcTranslator')->translate('Your Post is waiting for validation') |
||
65 | ); |
||
66 | |||
67 | return $this->redirect()->toUrl( |
||
68 | $this->frontendUrl()->fromRoute( |
||
69 | 'postvote/post', |
||
70 | array( |
||
71 | 'id' => $this->game->getIdentifier(), |
||
72 | 'post' => $postId, |
||
73 | |||
74 | ) |
||
75 | ) |
||
76 | ); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | if (! $this->game->getForm()) { |
||
81 | return $this->redirect()->toUrl( |
||
82 | $this->frontendUrl()->fromRoute( |
||
83 | 'postvote', |
||
84 | array('id' => $this->game->getIdentifier()) |
||
85 | ) |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | $form = $this->getGameService()->createFormFromJson($this->game->getForm()->getForm(), 'postvoteForm'); |
||
90 | |||
91 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
92 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
93 | if ($post) { |
||
94 | foreach ($post->getPostElements() as $element) { |
||
95 | try { |
||
96 | $form->get($element->getName())->setValue($element->getValue()); |
||
97 | |||
98 | $elementType = $form->get($element->getName())->getAttribute('type'); |
||
99 | if ($elementType == 'file' && $element->getValue() != '') { |
||
100 | $filter = $form->getInputFilter(); |
||
101 | $elementInput = $filter->get($element->getName()); |
||
102 | $elementInput->setRequired(false); |
||
103 | $form->get($element->getName())->setAttribute('required', false); |
||
104 | } |
||
105 | } catch (\Zend\Form\Exception\InvalidElementException $e) { |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | $viewModel = $this->buildView($this->game); |
||
111 | |||
112 | if ($this->getRequest()->isPost()) { |
||
113 | // POST Request: Process form |
||
114 | $data = array_merge_recursive( |
||
115 | $this->getRequest()->getPost()->toArray(), |
||
116 | $this->getRequest()->getFiles()->toArray() |
||
117 | ); |
||
118 | |||
119 | $form->setData($data); |
||
120 | |||
121 | if ($form->isValid()) { |
||
122 | $data = $form->getData(); |
||
123 | $post = $this->getGameService()->createPost($data, $this->game, $this->user, $form); |
||
124 | |||
125 | View Code Duplication | if ($post && !empty($this->game->nextStep('play'))) { |
|
126 | // determine the route where the user should go |
||
127 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
128 | 'postvote/'.$this->game->nextStep('play'), |
||
129 | array('id' => $this->game->getIdentifier()) |
||
130 | ); |
||
131 | |||
132 | return $this->redirect()->toUrl($redirectUrl); |
||
133 | } |
||
134 | } else { |
||
135 | $messages = $form->getMessages(); |
||
136 | $viewModel = $this->buildView($this->game); |
||
137 | $viewModel->setVariables(array( |
||
138 | 'success' => false, |
||
139 | 'message' => implode(',', $messages['title']), |
||
140 | )); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $viewModel->setVariables(array( |
||
145 | 'playerData' => $entry->getPlayerData(), |
||
146 | 'form' => $form, |
||
147 | 'post' => $post, |
||
148 | )); |
||
149 | |||
150 | return $viewModel; |
||
151 | } |
||
152 | |||
153 | public function previewAction() |
||
154 | { |
||
155 | $entry = $this->getGameService()->findLastActiveEntry($this->game, $this->user); |
||
156 | |||
157 | View Code Duplication | if (!$entry) { |
|
158 | // the user has already taken part of this game and the participation limit has been reached |
||
159 | return $this->redirect()->toUrl( |
||
160 | $this->frontendUrl()->fromRoute( |
||
161 | 'postvote/'.$this->game->nextStep('preview'), |
||
162 | array('id' => $this->game->getIdentifier()) |
||
163 | ) |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
168 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
169 | |||
170 | if (! $post) { |
||
171 | return $this->redirect()->toUrl( |
||
172 | $this->frontendUrl()->fromRoute( |
||
173 | 'postvote', |
||
174 | array('id' => $this->game->getIdentifier()) |
||
175 | ) |
||
176 | ); |
||
177 | } |
||
178 | |||
179 | if ($this->getRequest()->isPost()) { |
||
180 | $post = $this->getGameService()->confirmPost($this->game, $this->user); |
||
181 | |||
182 | if ($post) { |
||
183 | if (!($step = $this->game->nextStep('preview'))) { |
||
184 | $step = 'result'; |
||
185 | } |
||
186 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
187 | 'postvote/'.$step, |
||
188 | array('id' => $this->game->getIdentifier()) |
||
189 | ); |
||
190 | |||
191 | return $this->redirect()->toUrl($redirectUrl); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | $viewModel = $this->buildView($this->game); |
||
196 | $viewModel->setVariables(array('post' => $post)); |
||
197 | |||
198 | return $viewModel; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * View the Post page |
||
203 | * @return multitype:|\Zend\Http\Response|\Zend\View\Model\ViewModel |
||
204 | */ |
||
205 | public function postAction() |
||
206 | { |
||
207 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
208 | $voted = false; |
||
209 | $statusMail = false; |
||
210 | $mailService = $this->getServiceLocator()->get('playgroundgame_message'); |
||
211 | $to = ''; |
||
212 | $skinUrl = $this->getGameService()->getServiceManager()->get('ViewRenderer')->url( |
||
213 | 'frontend', |
||
214 | array(), |
||
215 | array('force_canonical' => true) |
||
216 | ); |
||
217 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
218 | if (isset($config['moderation']['email'])) { |
||
219 | $to = $config['moderation']['email']; |
||
220 | } |
||
221 | |||
222 | if (! $postId) { |
||
223 | return $this->notFoundAction(); |
||
224 | } |
||
225 | |||
226 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
227 | $post = $this->getGameService()->getPostVotePostMapper()->findById($postId); |
||
228 | |||
229 | View Code Duplication | if (! $post || $post->getStatus() === 9) { |
|
230 | return $this->redirect()->toUrl( |
||
231 | $this->frontendUrl()->fromRoute( |
||
232 | 'postvote', |
||
233 | array('id' => $this->game->getIdentifier()) |
||
234 | ) |
||
235 | ); |
||
236 | } |
||
237 | |||
238 | $formModeration = new Form(); |
||
239 | $formModeration->setAttribute('method', 'post'); |
||
240 | |||
241 | $formModeration->add(array( |
||
242 | 'name' => 'moderation', |
||
243 | 'attributes' => array( |
||
244 | 'type' => 'hidden', |
||
245 | 'value' => '1' |
||
246 | ), |
||
247 | )); |
||
248 | |||
249 | $form = new \PlaygroundGame\Form\Frontend\PostVoteVote( |
||
250 | $this->frontendUrl()->fromRoute( |
||
251 | 'postvote/post/captcha', |
||
252 | array( |
||
253 | 'id' => $this->game->getIdentifier(), |
||
254 | 'post' => $postId, |
||
255 | ) |
||
256 | ) |
||
257 | ); |
||
258 | |||
259 | if ($this->user) { |
||
260 | $form->remove('captcha'); |
||
261 | if (count($this->getGameService()->getPostvoteVoteMapper()->findBy(array('user' => $this->user, 'post' =>$post))) > 0) { |
||
262 | $voted = true; |
||
263 | } |
||
264 | } |
||
265 | |||
266 | $alreadyVoted = ''; |
||
267 | $reportId = ''; |
||
268 | |||
269 | $request = $this->getRequest(); |
||
270 | if ($request->isPost()) { |
||
271 | $data = $request->getPost()->toArray(); |
||
272 | if (isset($data['moderation'])) { |
||
273 | $formModeration->setData($data); |
||
274 | if ($formModeration->isValid()) { |
||
275 | $from = $to; |
||
276 | $subject= 'Moderation Post and Vote'; |
||
277 | $result = $mailService->createHtmlMessage( |
||
278 | $from, |
||
279 | $to, |
||
280 | $subject, |
||
281 | 'playground-game/email/moderation', |
||
282 | array('data' => $data, 'skinUrl' => $skinUrl) |
||
283 | ); |
||
284 | $mailService->send($result); |
||
285 | if ($result) { |
||
286 | $statusMail = true; |
||
287 | $reportId = $data['reportid']; |
||
288 | } |
||
289 | } |
||
290 | } else { |
||
291 | $form->setData($request->getPost()); |
||
292 | if ($form->isValid()) { |
||
293 | if ($this->getGameService()->addVote( |
||
294 | $this->user, |
||
295 | $this->getRequest()->getServer('REMOTE_ADDR'), |
||
296 | $post |
||
297 | )) { |
||
298 | $voted = true; |
||
299 | } else { |
||
300 | $alreadyVoted = 'Vous avez déjà voté!'; |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | } |
||
305 | |||
306 | $viewModel = $this->buildView($this->game); |
||
307 | $viewModel->setVariables( |
||
308 | array( |
||
309 | 'post' => $post, |
||
310 | 'voted' => $voted, |
||
311 | 'form' => $form, |
||
312 | 'formModeration' => $formModeration, |
||
313 | 'statusMail' => $statusMail, |
||
314 | 'alreadyVoted' => $alreadyVoted, |
||
315 | 'reportId' => $reportId, |
||
316 | ) |
||
317 | ); |
||
318 | |||
319 | return $viewModel; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * |
||
324 | */ |
||
325 | public function resultAction() |
||
326 | { |
||
327 | $playLimitReached = false; |
||
328 | if ($this->getRequest()->getQuery()->get('playLimitReached')) { |
||
329 | $playLimitReached = true; |
||
330 | } |
||
331 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
332 | if ($lastEntry === null) { |
||
333 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier))); |
||
334 | } |
||
335 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
336 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
337 | |||
338 | if (! $post) { |
||
339 | return $this->redirect()->toUrl( |
||
340 | $this->frontendUrl()->fromRoute( |
||
341 | 'postvote', |
||
342 | array('id' => $this->game->getIdentifier()) |
||
343 | ) |
||
344 | ); |
||
345 | } |
||
346 | |||
347 | // DEPRECATED: we should be able to add a specific view to each action |
||
348 | // based on config in the admin (tab design) |
||
349 | // $view = $this->forward()->dispatch( |
||
350 | // 'playgroundgame_'.$this->game->getClassType(), |
||
351 | // array( |
||
352 | // 'controller' => 'playgroundgame_'.$this->game->getClassType(), |
||
353 | // 'action' => 'share', |
||
354 | // 'id' => $this->game->getIdentifier() |
||
355 | // ) |
||
356 | // ); |
||
357 | |||
358 | // if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
359 | // $view->setVariables(array('post' => $post)); |
||
360 | |||
361 | // return $view; |
||
362 | // } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
363 | // return $view; |
||
364 | // } else { |
||
365 | $form = $this->getServiceLocator() |
||
366 | ->get('playgroundgame_sharemail_form') |
||
367 | ->setAttribute('method', 'post'); |
||
368 | |||
369 | $viewModel = $this->buildView($this->game); |
||
370 | |||
371 | $viewModel->setVariables(array( |
||
372 | 'statusMail' => null, |
||
373 | 'post' => $post, |
||
374 | 'form' => $form, |
||
375 | 'playLimitReached' => $playLimitReached, |
||
376 | )); |
||
377 | |||
378 | return $viewModel; |
||
379 | //} |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Example of AJAX File Upload with Session Progress and partial validation. |
||
384 | * It's now possible to send a base64 image in this case the call is the form : |
||
385 | * this._ajax( |
||
386 | * { |
||
387 | * url: url.dataset.url, |
||
388 | * method: 'post', |
||
389 | * body: 'photo=' + image |
||
390 | * }, |
||
391 | * |
||
392 | * @return \Zend\Stdlib\ResponseInterface |
||
393 | */ |
||
394 | public function ajaxuploadAction() |
||
443 | |||
444 | public function ajaxdeleteAction() |
||
476 | |||
477 | public function ajaxrejectPostAction() |
||
513 | |||
514 | public function listAction() |
||
628 | |||
629 | /** |
||
630 | * If the user has already voted, we cancel her vote, else we create the vote. |
||
631 | * TODO : we should distinguish between vote and like (so we should add the like field in post) |
||
632 | */ |
||
633 | public function ajaxVoteAction() |
||
634 | { |
||
635 | // Call this for the session lock to be released (other ajax calls can then be made) |
||
636 | session_write_close(); |
||
637 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
638 | $commentId = $this->getEvent()->getRouteMatch()->getParam('comment'); |
||
639 | $request = $this->getRequest(); |
||
640 | $response = $this->getResponse(); |
||
641 | |||
642 | if (! $this->game) { |
||
643 | $response->setContent(\Zend\Json\Json::encode(array( |
||
644 | 'success' => 0 |
||
645 | ))); |
||
646 | |||
647 | return $response; |
||
648 | } |
||
649 | |||
650 | if (!$this->zfcUserAuthentication()->hasIdentity()) { |
||
651 | $response->setContent(\Zend\Json\Json::encode(array( |
||
652 | 'success' => 0 |
||
653 | ))); |
||
654 | } else { |
||
655 | if ($request->isPost()) { |
||
656 | $post = $this->getGameService()->getPostvotePostMapper()->findById($postId); |
||
657 | $comment = ($commentId !== null) ? $this->getGameService()->getPostVoteCommentMapper()->findById($commentId) : null; |
||
658 | if ($this->getGameService()->toggleVote( |
||
659 | $this->user, |
||
660 | $this->getRequest()->getServer('REMOTE_ADDR'), |
||
661 | $post, |
||
662 | $comment |
||
663 | )) { |
||
664 | $response->setContent(\Zend\Json\Json::encode(array( |
||
665 | 'success' => 1 |
||
666 | ))); |
||
667 | } else { |
||
668 | $response->setContent(\Zend\Json\Json::encode(array( |
||
669 | 'success' => 0 |
||
670 | ))); |
||
671 | } |
||
672 | } |
||
673 | } |
||
674 | |||
675 | return $response; |
||
676 | } |
||
677 | |||
678 | public function commentsAction() |
||
701 | |||
702 | public function commentAction() |
||
703 | { |
||
704 | if ($this->getRequest()->isXmlHttpRequest()) { |
||
705 | // Call this for the session lock to be released (other ajax calls can then be made) |
||
706 | session_write_close(); |
||
707 | } |
||
708 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
709 | $request = $this->getRequest(); |
||
710 | $response = $this->getResponse(); |
||
711 | $post = $this->getGameService()->getPostvotePostMapper()->findById($postId); |
||
712 | |||
713 | if (!$this->zfcUserAuthentication()->hasIdentity()) { |
||
714 | $response->setContent(\Zend\Json\Json::encode(array( |
||
715 | 'success' => 0 |
||
716 | ))); |
||
717 | View Code Duplication | } else { |
|
718 | if ($request->isPost()) { |
||
719 | if ($this->getGameService()->addComment( |
||
720 | $this->user, |
||
721 | $this->getRequest()->getServer('REMOTE_ADDR'), |
||
722 | $post, |
||
723 | $this->params()->fromPost('comment'), |
||
724 | $this->params()->fromPost('category') |
||
725 | )) { |
||
726 | $response->setContent(\Zend\Json\Json::encode(array( |
||
727 | 'success' => 1 |
||
728 | ))); |
||
729 | } else { |
||
730 | $response->setContent(\Zend\Json\Json::encode(array( |
||
731 | 'success' => 0 |
||
732 | ))); |
||
733 | } |
||
734 | } |
||
735 | } |
||
736 | |||
737 | //ajax call ? |
||
738 | if ($this->getRequest()->isXmlHttpRequest()) { |
||
739 | return $response; |
||
740 | } |
||
741 | |||
742 | $this->flashMessenger()->addMessage( |
||
743 | $this->getServiceLocator()->get('MvcTranslator')->translate('Your comment has been recorded') |
||
744 | ); |
||
745 | |||
746 | return $this->redirect()->toUrl($this->getRequest()->getServer('HTTP_REFERER')); |
||
747 | } |
||
748 | |||
749 | public function ajaxRemoveCommentAction() |
||
750 | { |
||
751 | // Call this for the session lock to be released (other ajax calls can then be made) |
||
752 | session_write_close(); |
||
753 | $commentId = $this->getEvent()->getRouteMatch()->getParam('comment'); |
||
754 | $request = $this->getRequest(); |
||
755 | $response = $this->getResponse(); |
||
756 | |||
757 | if (! $this->game) { |
||
758 | $response->setContent(\Zend\Json\Json::encode(array( |
||
759 | 'success' => 0 |
||
760 | ))); |
||
761 | |||
762 | return $response; |
||
763 | } |
||
764 | |||
765 | View Code Duplication | if (!$this->zfcUserAuthentication()->hasIdentity()) { |
|
766 | $response->setContent(\Zend\Json\Json::encode(array( |
||
767 | 'success' => 0 |
||
768 | ))); |
||
769 | } else { |
||
770 | if ($request->isPost()) { |
||
771 | if ($this->getGameService()->removeComment( |
||
772 | $this->user, |
||
773 | $this->getRequest()->getServer('REMOTE_ADDR'), |
||
774 | $commentId |
||
775 | )) { |
||
776 | $response->setContent(\Zend\Json\Json::encode(array( |
||
777 | 'success' => 1 |
||
778 | ))); |
||
779 | } else { |
||
780 | $response->setContent(\Zend\Json\Json::encode(array( |
||
781 | 'success' => 0 |
||
782 | ))); |
||
783 | } |
||
784 | } |
||
785 | } |
||
786 | |||
787 | return $response; |
||
788 | } |
||
789 | |||
790 | public function captchaAction() |
||
814 | |||
815 | public function sharePostAction() |
||
869 | |||
870 | public function shareCommentAction() |
||
911 | |||
912 | public function shareAction() |
||
985 | |||
986 | public function getGameService() |
||
994 | } |
||
995 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: