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 |
||
7 | class PostVoteController extends GameController |
||
8 | { |
||
9 | /** |
||
10 | * @var gameService |
||
11 | */ |
||
12 | protected $gameService; |
||
13 | |||
14 | /** |
||
15 | * --DONE-- 1. try to change the Game Id (on le redirige vers la home du jeu) |
||
16 | * --DONE-- 2. try to modify questions (the form is recreated and verified in the controller) |
||
17 | * --DONE-- 3. don't answer to questions (form is checked controller side) |
||
18 | * 4. try to game the chrono |
||
19 | * 5. try to play again |
||
20 | * 6. try to change answers |
||
21 | * --DONE-- 7. essaie de répondre sans être inscrit (on le redirige vers la home du jeu) |
||
22 | */ |
||
23 | public function playAction() |
||
24 | { |
||
25 | $sg = $this->getGameService(); |
||
26 | |||
27 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
28 | $channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
||
29 | |||
30 | $game = $sg->checkGame($identifier); |
||
31 | if (! $game || $game->isClosed()) { |
||
32 | return $this->notFoundAction(); |
||
33 | } |
||
34 | |||
35 | $redirectFb = $this->checkFbRegistration($this->zfcUserAuthentication()->getIdentity(), $game, $channel); |
||
|
|||
36 | if ($redirectFb) { |
||
37 | return $redirectFb; |
||
38 | } |
||
39 | |||
40 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
41 | |||
42 | if (!$user && !$game->getAnonymousAllowed()) { |
||
43 | $redirect = urlencode($this->frontendUrl()->fromRoute(''. $game->getClassType() . '/play', array('id' => $game->getIdentifier(), 'channel' => $channel), array('force_canonical' => true))); |
||
44 | |||
45 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('zfcuser/register', array('channel' => $channel)) . '?redirect='.$redirect); |
||
46 | } |
||
47 | |||
48 | $entry = $sg->play($game, $user); |
||
49 | |||
50 | if (!$entry) { |
||
51 | $lastEntry = $sg->findLastInactiveEntry($game, $user); |
||
52 | if ($lastEntry === null) { |
||
53 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
54 | } |
||
55 | |||
56 | $lastEntryId = $lastEntry->getId(); |
||
57 | $lastPost = $sg->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntryId)); |
||
58 | $postId = $lastPost->getId(); |
||
59 | if ($lastPost->getStatus() == 2) { |
||
60 | // the user has already taken part of this game and the participation limit has been reached |
||
61 | $this->flashMessenger()->addMessage($this->getServiceLocator()->get('translator')->translate('You have already a Post')); |
||
62 | |||
63 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote/post', array('id' => $identifier, 'post' => $postId, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
64 | } else { |
||
65 | $this->flashMessenger()->addMessage($this->getServiceLocator()->get('translator')->translate('Your Post is waiting for validation')); |
||
66 | |||
67 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote/post', array('id' => $identifier, 'post' => $postId, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | if (! $game->getForm()) { |
||
72 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
73 | } |
||
74 | |||
75 | $form = $sg->createFormFromJson($game->getForm()->getForm(), 'postvoteForm'); |
||
76 | |||
77 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
78 | $post = $sg->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
79 | if ($post) { |
||
80 | foreach ($post->getPostElements() as $element) { |
||
81 | try { |
||
82 | $form->get($element->getName())->setValue($element->getValue()); |
||
83 | |||
84 | $elementType = $form->get($element->getName())->getAttribute('type'); |
||
85 | if ($elementType == 'file' && $element->getValue() != '') { |
||
86 | $filter = $form->getInputFilter(); |
||
87 | $elementInput = $filter->get($element->getName()); |
||
88 | $elementInput->setRequired(false); |
||
89 | $form->get($element->getName())->setAttribute('required', false); |
||
90 | } |
||
91 | } catch (\Zend\Form\Exception\InvalidElementException $e) { |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | $viewModel = $this->buildView($game); |
||
97 | |||
98 | if ($this->getRequest()->isPost()) { |
||
99 | // POST Request: Process form |
||
100 | $data = array_merge_recursive( |
||
101 | $this->getRequest()->getPost()->toArray(), |
||
102 | $this->getRequest()->getFiles()->toArray() |
||
103 | ); |
||
104 | |||
105 | $form->setData($data); |
||
106 | |||
107 | if ($form->isValid()) { |
||
108 | $data = $form->getData(); |
||
109 | $post = $this->getGameService()->createPost($data, $game, $user, $form); |
||
110 | |||
111 | if ($post && !empty($game->nextStep('play'))) { |
||
112 | // determine the route where the user should go |
||
113 | $redirectUrl = $this->frontendUrl()->fromRoute('postvote/'.$game->nextStep('play'), array('id' => $game->getIdentifier(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel'))); |
||
114 | |||
115 | return $this->redirect()->toUrl($redirectUrl); |
||
116 | } |
||
117 | } else { |
||
118 | $messages = $form->getMessages(); |
||
119 | $viewModel = $this->buildView($game); |
||
120 | $viewModel->setVariables(array( |
||
121 | 'success' => false, |
||
122 | 'message' => implode(',', $messages['title']), |
||
123 | )); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | $viewModel->setVariables(array( |
||
128 | 'playerData' => $entry->getPlayerData(), |
||
129 | 'form' => $form, |
||
130 | 'post' => $post, |
||
131 | )); |
||
132 | |||
133 | return $viewModel; |
||
134 | } |
||
135 | |||
136 | public function previewAction() |
||
137 | { |
||
138 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
139 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
140 | $sg = $this->getGameService(); |
||
141 | |||
142 | $game = $sg->checkGame($identifier); |
||
143 | if (! $game) { |
||
144 | return $this->notFoundAction(); |
||
145 | } |
||
146 | |||
147 | $entry = $sg->findLastActiveEntry($game, $user); |
||
148 | |||
149 | if (!$entry) { |
||
150 | // the user has already taken part of this game and the participation limit has been reached |
||
151 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote/'.$game->nextStep('preview'), array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
152 | } |
||
153 | |||
154 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
155 | $post = $sg->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
156 | |||
157 | if (! $post) { |
||
158 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
159 | } |
||
160 | |||
161 | if ($this->getRequest()->isPost()) { |
||
162 | $post = $this->getGameService()->confirmPost($game, $user); |
||
163 | |||
164 | if ($post) { |
||
165 | if (!($step = $game->nextStep('play'))) { |
||
166 | $step = 'result'; |
||
167 | } |
||
168 | $redirectUrl = $this->frontendUrl()->fromRoute('postvote/'.$step, array('id' => $game->getIdentifier(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel'))); |
||
169 | |||
170 | return $this->redirect()->toUrl($redirectUrl); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | $viewModel = $this->buildView($game); |
||
175 | $viewModel->setVariables(array('post' => $post)); |
||
176 | |||
177 | return $viewModel; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * View the Post page |
||
182 | * @return multitype:|\Zend\Http\Response|\Zend\View\Model\ViewModel |
||
183 | */ |
||
184 | public function postAction() |
||
185 | { |
||
186 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
187 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
188 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
189 | $sg = $this->getGameService(); |
||
190 | $voted = false; |
||
191 | |||
192 | $statusMail = false; |
||
193 | $mailService = $this->getServiceLocator()->get('playgroundgame_message'); |
||
194 | $to = ''; |
||
195 | $skinUrl = $sg->getServiceManager()->get('ViewRenderer')->url('frontend', array('channel' => $this->getEvent()->getRouteMatch()->getParam('channel')), array('force_canonical' => true)); |
||
196 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
197 | if (isset($config['moderation']['email'])) { |
||
198 | $to = $config['moderation']['email']; |
||
199 | } |
||
200 | |||
201 | $game = $sg->checkGame($identifier, false); |
||
202 | |||
203 | if (! $postId) { |
||
204 | return $this->notFoundAction(); |
||
205 | } |
||
206 | |||
207 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
208 | $post = $sg->getPostVotePostMapper()->findById($postId); |
||
209 | |||
210 | if (! $post || $post->getStatus() === 9) { |
||
211 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
212 | } |
||
213 | |||
214 | $formModeration = new Form(); |
||
215 | $formModeration->setAttribute('method', 'post'); |
||
216 | |||
217 | $formModeration->add(array( |
||
218 | 'name' => 'moderation', |
||
219 | 'attributes' => array( |
||
220 | 'type' => 'hidden', |
||
221 | 'value' => '1' |
||
222 | ), |
||
223 | )); |
||
224 | |||
225 | $form = new \PlaygroundGame\Form\Frontend\PostVoteVote($this->frontendUrl()->fromRoute('postvote/post/captcha', array('id' => $identifier, 'post' => $postId, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
226 | |||
227 | if ($user) { |
||
228 | $form->remove('captcha'); |
||
229 | } |
||
230 | |||
231 | $alreadyVoted = ''; |
||
232 | $reportId = ''; |
||
233 | |||
234 | $request = $this->getRequest(); |
||
235 | if ($request->isPost()) { |
||
236 | $data = $request->getPost()->toArray(); |
||
237 | if (isset($data['moderation'])) { |
||
238 | $formModeration->setData($data); |
||
239 | if ($formModeration->isValid()) { |
||
240 | $from = $to; |
||
241 | $subject= 'Moderation Post and Vote'; |
||
242 | $result = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/moderation', array('data' => $data, 'skinUrl' => $skinUrl)); |
||
243 | $mailService->send($result); |
||
244 | if ($result) { |
||
245 | $statusMail = true; |
||
246 | $reportId = $data['reportid']; |
||
247 | } |
||
248 | } |
||
249 | } else { |
||
250 | $form->setData($request->getPost()); |
||
251 | if ($form->isValid()) { |
||
252 | if ($sg->addVote($user, $this->getRequest()->getServer('REMOTE_ADDR'), $post)) { |
||
253 | $voted = true; |
||
254 | } else { |
||
255 | $alreadyVoted = 'Vous avez déjà voté!'; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | $viewModel = $this->buildView($game); |
||
262 | $viewModel->setVariables( |
||
263 | array( |
||
264 | 'post' => $post, |
||
265 | 'voted' => $voted, |
||
266 | 'form' => $form, |
||
267 | 'formModeration' => $formModeration, |
||
268 | 'statusMail' => $statusMail, |
||
269 | 'alreadyVoted' => $alreadyVoted, |
||
270 | 'reportId' => $reportId, |
||
271 | ) |
||
272 | ); |
||
273 | |||
274 | return $viewModel; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * |
||
279 | */ |
||
280 | public function resultAction() |
||
281 | { |
||
282 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
283 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
284 | $channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
||
285 | |||
286 | $statusMail = null; |
||
287 | |||
288 | if (!$identifier) { |
||
289 | return $this->notFoundAction(); |
||
290 | } |
||
291 | |||
292 | $postVoteMapper = $this->getGameService()->getPostVoteMapper(); |
||
293 | $game = $postVoteMapper->findByIdentifier($identifier); |
||
294 | |||
295 | if (!$game || $game->isClosed()) { |
||
296 | return $this->notFoundAction(); |
||
297 | } |
||
298 | |||
299 | // Has the user finished the game ? |
||
300 | $lastEntry = $this->getGameService()->findLastInactiveEntry($game, $user); |
||
301 | |||
302 | if ($lastEntry === null) { |
||
303 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
304 | } |
||
305 | |||
306 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
307 | $redirect = urlencode($this->frontendUrl()->fromRoute('postvote/result', array('id' => $game->getIdentifier(), 'channel' => $channel))); |
||
308 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('zfcuser/register', array('channel' => $channel)) . '?redirect='.$redirect); |
||
309 | } |
||
310 | |||
311 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
312 | $form->setAttribute('method', 'post'); |
||
313 | |||
314 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
315 | $data = $this->getRequest()->getPost()->toArray(); |
||
316 | $form->setData($data); |
||
317 | if ($form->isValid()) { |
||
318 | $result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
||
319 | if ($result) { |
||
320 | $statusMail = true; |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | |||
325 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
326 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
327 | |||
328 | if (! $post) { |
||
329 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
330 | } |
||
331 | |||
332 | $viewModel = $this->buildView($game); |
||
333 | |||
334 | $viewModel->setVariables(array( |
||
335 | 'statusMail' => $statusMail, |
||
336 | 'post' => $post, |
||
337 | 'form' => $form, |
||
338 | )); |
||
339 | |||
340 | return $viewModel; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Example of AJAX File Upload with Session Progress and partial validation. |
||
345 | * It's now possible to send a base64 image in this case the call is the form : |
||
346 | * this._ajax( |
||
347 | * { |
||
348 | * url: url.dataset.url, |
||
349 | * method: 'post', |
||
350 | * body: 'photo=' + image |
||
351 | * }, |
||
352 | * |
||
353 | * @return \Zend\Stdlib\ResponseInterface |
||
354 | */ |
||
355 | public function ajaxuploadAction() |
||
406 | |||
407 | public function ajaxdeleteAction() |
||
445 | |||
446 | public function listAction() |
||
545 | |||
546 | public function ajaxVoteAction() |
||
589 | |||
590 | public function captchaAction() |
||
614 | |||
615 | public function shareAction() |
||
616 | { |
||
617 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
618 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
619 | $sg = $this->getGameService(); |
||
620 | |||
621 | $statusMail = null; |
||
622 | |||
623 | if (!$identifier) { |
||
624 | return $this->notFoundAction(); |
||
625 | } |
||
626 | |||
627 | $gameMapper = $this->getGameService()->getGameMapper(); |
||
628 | $game = $gameMapper->findByIdentifier($identifier); |
||
629 | |||
630 | if (!$game || $game->isClosed()) { |
||
631 | return $this->notFoundAction(); |
||
632 | } |
||
633 | |||
634 | // Has the user finished the game ? |
||
635 | $lastEntry = $this->getGameService()->findLastInactiveEntry($game, $user); |
||
636 | |||
637 | if ($lastEntry === null) { |
||
638 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
639 | } |
||
640 | |||
641 | $post = $sg->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
642 | |||
643 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
644 | $socialLinkUrl = $this->frontendUrl()->fromRoute('postvote/post', array('id' => $identifier, 'post' => $post->getId(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')), array('force_canonical' => true)).'?key='.$secretKey; |
||
645 | // With core shortener helper |
||
646 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
647 | |||
648 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
649 | $redirect = urlencode($this->frontendUrl()->fromRoute('postvote/result', array('id' => $game->getIdentifier(), 'channel' => $channel))); |
||
650 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('zfcuser/register', array('channel' => $channel)) . '?redirect='.$redirect); |
||
651 | } |
||
652 | |||
653 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
654 | $form->setAttribute('method', 'post'); |
||
655 | |||
656 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
657 | $data = $this->getRequest()->getPost()->toArray(); |
||
658 | $form->setData($data); |
||
659 | if ($form->isValid()) { |
||
660 | $result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
||
661 | if ($result) { |
||
662 | $statusMail = true; |
||
663 | } |
||
664 | } |
||
665 | } |
||
666 | |||
667 | $viewModel = $this->buildView($game); |
||
668 | |||
669 | View Code Duplication | foreach ($post->getPostElements() as $element) { |
|
670 | $fbShareImage = $this->frontendUrl()->fromRoute('', array('channel' => ''), array('force_canonical' => true), false) . $element->getValue(); |
||
671 | break; |
||
672 | } |
||
673 | |||
674 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
675 | |||
676 | // Without bit.ly shortener |
||
677 | $socialLinkUrl = $this->frontendUrl()->fromRoute('postvote/list', array('id' => $game->getIdentifier(), 'filter' => 'date', 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')), array('force_canonical' => true)).'?id='.$post->getId().'&key='.$secretKey; |
||
678 | // With core shortener helper |
||
679 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
680 | |||
681 | $this->getViewHelper('HeadMeta')->setProperty('og:image', $fbShareImage); |
||
682 | |||
683 | $this->getViewHelper('HeadMeta')->setProperty('twitter:card', "photo"); |
||
684 | $this->getViewHelper('HeadMeta')->setProperty('twitter:site', "@playground"); |
||
685 | $this->getViewHelper('HeadMeta')->setProperty('twitter:title', $game->getTwShareMessage()); |
||
686 | $this->getViewHelper('HeadMeta')->setProperty('twitter:description', ""); |
||
687 | $this->getViewHelper('HeadMeta')->setProperty('twitter:image', $fbShareImage); |
||
688 | $this->getViewHelper('HeadMeta')->setProperty('twitter:url', $socialLinkUrl); |
||
689 | |||
690 | $viewModel->setVariables(array( |
||
691 | 'statusMail' => $statusMail, |
||
692 | 'form' => $form, |
||
693 | 'socialLinkUrl' => $socialLinkUrl, |
||
694 | 'post' => $post |
||
695 | )); |
||
696 | |||
697 | return $viewModel; |
||
698 | } |
||
699 | |||
700 | public function getGameService() |
||
708 | } |
||
709 |
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: