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 GameController 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 GameController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class GameController extends AbstractActionController |
||
15 | { |
||
16 | /** |
||
17 | * @var \PlaygroundGame\Service\GameService |
||
18 | */ |
||
19 | protected $gameService; |
||
20 | |||
21 | protected $prizeService; |
||
22 | |||
23 | protected $options; |
||
24 | |||
25 | protected $game; |
||
26 | |||
27 | protected $user; |
||
28 | |||
29 | protected $withGame = array( |
||
30 | 'home', |
||
31 | 'index', |
||
32 | 'terms', |
||
33 | 'conditions', |
||
34 | 'leaderboard', |
||
35 | 'register', |
||
36 | 'bounce', |
||
37 | 'prizes', |
||
38 | 'prize', |
||
39 | 'fangate', |
||
40 | 'share', |
||
41 | 'optin', |
||
42 | 'login', |
||
43 | 'play', |
||
44 | 'result', |
||
45 | 'preview', |
||
46 | 'list' |
||
47 | ); |
||
48 | |||
49 | protected $withOnlineGame = array( |
||
50 | 'leaderboard', |
||
51 | 'register', |
||
52 | 'bounce', |
||
53 | 'play', |
||
54 | 'result' |
||
55 | ); |
||
56 | |||
57 | protected $withAnyUser = array( |
||
58 | 'share', |
||
59 | 'result', |
||
60 | 'play' |
||
61 | ); |
||
62 | |||
63 | public function setEventManager(\Zend\EventManager\EventManagerInterface $events) |
||
108 | |||
109 | /** |
||
110 | * Action called if matched action does not exist |
||
111 | * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy |
||
112 | * it has to be rendered in the controller. Hence the code below. |
||
113 | * |
||
114 | * This action is injected as a catchall action for each custom_games definition |
||
115 | * This way, when a custom_game is created, the 404 is it's responsability and the |
||
116 | * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml |
||
117 | * |
||
118 | * |
||
119 | * @return \Zend\Stdlib\ResponseInterface |
||
120 | */ |
||
121 | public function notFoundAction() |
||
138 | |||
139 | /** |
||
140 | * This action acts as a hub : Depending on the first step of the game, it will forward the action to this step |
||
141 | */ |
||
142 | public function homeAction() |
||
174 | |||
175 | /** |
||
176 | * Homepage of the game |
||
177 | */ |
||
178 | public function indexAction() |
||
194 | |||
195 | /** |
||
196 | * leaderboardAction |
||
197 | * |
||
198 | * @return ViewModel $viewModel |
||
199 | */ |
||
200 | public function leaderboardAction() |
||
201 | { |
||
202 | $filter = $this->getEvent()->getRouteMatch()->getParam('filter'); |
||
203 | $p = $this->getEvent()->getRouteMatch()->getParam('p'); |
||
204 | |||
205 | $beforeLayout = $this->layout()->getTemplate(); |
||
206 | $subViewModel = $this->forward()->dispatch( |
||
207 | 'playgroundreward', |
||
208 | array('action' => 'leaderboard', 'filter' => $filter, 'p' => $p) |
||
209 | ); |
||
210 | |||
211 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
212 | $this->layout()->setTemplate($beforeLayout); |
||
213 | |||
214 | // give the ability to the game to have its customized look and feel. |
||
215 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
216 | $l = $templatePathResolver->getPaths(); |
||
217 | |||
218 | $templatePathResolver->addPath($l[0].'custom/'.$this->game->getIdentifier()); |
||
219 | |||
220 | return $subViewModel; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * This action has been designed to be called by other controllers |
||
225 | * It gives the ability to display an information form and persist it in the game entry |
||
226 | * |
||
227 | * @return \Zend\View\Model\ViewModel |
||
228 | */ |
||
229 | public function registerAction() |
||
230 | { |
||
231 | $form = $this->getGameService()->createFormFromJson($this->game->getPlayerForm()->getForm(), 'playerForm'); |
||
232 | |||
233 | if ($this->getRequest()->isPost()) { |
||
234 | // POST Request: Process form |
||
235 | $data = array_merge_recursive( |
||
236 | $this->getRequest()->getPost()->toArray(), |
||
237 | $this->getRequest()->getFiles()->toArray() |
||
238 | ); |
||
239 | |||
240 | $form->setData($data); |
||
241 | |||
242 | if ($form->isValid()) { |
||
243 | // steps of the game |
||
244 | $steps = $this->game->getStepsArray(); |
||
245 | // sub steps of the game |
||
246 | $viewSteps = $this->game->getStepsViewsArray(); |
||
247 | |||
248 | // register position |
||
249 | $key = array_search($this->params('action'), $viewSteps); |
||
250 | if (!$key) { |
||
251 | // register is not a substep of the game so it's a step |
||
252 | $key = array_search($this->params('action'), $steps); |
||
253 | $keyStep = true; |
||
254 | } else { |
||
255 | // register was a substep, i search the index of its parent |
||
256 | $key = array_search($key, $steps); |
||
257 | $keyStep = false; |
||
258 | } |
||
259 | |||
260 | // play position |
||
261 | $keyplay = array_search('play', $viewSteps); |
||
262 | |||
263 | if (!$keyplay) { |
||
264 | // play is not a substep, so it's a step |
||
265 | $keyplay = array_search('play', $steps); |
||
266 | $keyplayStep = true; |
||
267 | } else { |
||
268 | // play is a substep so I search the index of its parent |
||
269 | $keyplay = array_search($keyplay, $steps); |
||
270 | $keyplayStep = false; |
||
271 | } |
||
272 | |||
273 | // If register step before play, I don't have no entry yet. I have to create one |
||
274 | // If register after play step, I search for the last entry created by play step. |
||
275 | |||
276 | if ($key < $keyplay || ($keyStep && !$keyplayStep && $key <= $keyplay)) { |
||
277 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
278 | if (!$entry) { |
||
279 | // the user has already taken part of this game and the participation limit has been reached |
||
280 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
281 | |||
282 | return $this->redirect()->toUrl( |
||
283 | $this->frontendUrl()->fromRoute( |
||
284 | $this->game->getClassType().'/result', |
||
285 | array( |
||
286 | 'id' => $this->game->getIdentifier(), |
||
287 | |||
288 | ) |
||
289 | ) |
||
290 | ); |
||
291 | } |
||
292 | } else { |
||
293 | // I'm looking for an entry without anonymousIdentifier (the active entry in fact). |
||
294 | $entry = $this->getGameService()->findLastEntry($this->game, $this->user); |
||
295 | if ($this->getGameService()->hasReachedPlayLimit($this->game, $this->user)) { |
||
296 | // the user has already taken part of this game and the participation limit has been reached |
||
297 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
298 | |||
299 | return $this->redirect()->toUrl( |
||
300 | $this->frontendUrl()->fromRoute( |
||
301 | $this->game->getClassType().'/result', |
||
302 | array( |
||
303 | 'id' => $this->game->getIdentifier(), |
||
304 | |||
305 | ) |
||
306 | ) |
||
307 | ); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | $this->getGameService()->updateEntryPlayerForm($form->getData(), $this->game, $this->user, $entry); |
||
312 | |||
313 | if (!empty($this->game->nextStep($this->params('action')))) { |
||
314 | return $this->redirect()->toUrl( |
||
315 | $this->frontendUrl()->fromRoute( |
||
316 | $this->game->getClassType() .'/' . $this->game->nextStep($this->params('action')), |
||
317 | array('id' => $this->game->getIdentifier()), |
||
318 | array('force_canonical' => true) |
||
319 | ) |
||
320 | ); |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | |||
325 | $viewModel = $this->buildView($this->game); |
||
326 | $viewModel->setVariables(array( |
||
327 | 'form' => $form |
||
328 | )); |
||
329 | |||
330 | return $viewModel; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * This action takes care of the terms of the game |
||
335 | */ |
||
336 | public function termsAction() |
||
337 | { |
||
338 | $viewModel = $this->buildView($this->game); |
||
339 | |||
340 | return $viewModel; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * This action takes care of the conditions of the game |
||
345 | */ |
||
346 | public function conditionsAction() |
||
347 | { |
||
348 | $viewModel = $this->buildView($this->game); |
||
349 | |||
350 | return $viewModel; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * This action takes care of bounce page of the game |
||
355 | */ |
||
356 | public function bounceAction() |
||
357 | { |
||
358 | $availableGames = $this->getGameService()->getAvailableGames($this->user); |
||
359 | |||
360 | $rssUrl = ''; |
||
361 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
362 | if (isset($config['rss']['url'])) { |
||
363 | $rssUrl = $config['rss']['url']; |
||
364 | } |
||
365 | |||
366 | $viewModel = $this->buildView($this->game); |
||
367 | $viewModel->setVariables(array( |
||
368 | 'rssUrl' => $rssUrl, |
||
369 | 'user' => $this->user, |
||
370 | 'availableGames' => $availableGames, |
||
371 | )); |
||
372 | |||
373 | return $viewModel; |
||
374 | } |
||
375 | |||
376 | |||
377 | /** |
||
378 | * This action displays the Prizes page associated to the game |
||
379 | */ |
||
380 | public function prizesAction() |
||
390 | |||
391 | /** |
||
392 | * This action displays a specific Prize page among those associated to the game |
||
393 | */ |
||
394 | public function prizeAction() |
||
408 | |||
409 | public function gameslistAction() |
||
456 | |||
457 | public function fangateAction() |
||
463 | |||
464 | public function shareAction() |
||
465 | { |
||
466 | $statusMail = null; |
||
467 | |||
468 | // Has the user finished the game ? |
||
469 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
470 | |||
471 | if ($lastEntry === null) { |
||
472 | return $this->redirect()->toUrl( |
||
473 | $this->frontendUrl()->fromRoute( |
||
474 | 'postvote', |
||
475 | array('id' => $this->game->getIdentifier()) |
||
476 | ) |
||
477 | ); |
||
478 | } |
||
479 | |||
480 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
481 | $form->setAttribute('method', 'post'); |
||
482 | |||
483 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
484 | $data = $this->getRequest()->getPost()->toArray(); |
||
485 | $form->setData($data); |
||
486 | if ($form->isValid()) { |
||
487 | $result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
||
488 | if ($result) { |
||
489 | $statusMail = true; |
||
490 | } |
||
491 | } |
||
492 | } |
||
493 | |||
494 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
495 | $viewModel = $this->buildView($this->game); |
||
496 | |||
497 | $this->getGameService()->sendMail($this->game, $this->user, $lastEntry); |
||
498 | |||
499 | $viewModel->setVariables(array( |
||
500 | 'statusMail' => $statusMail, |
||
501 | 'form' => $form, |
||
502 | )); |
||
503 | |||
504 | return $viewModel; |
||
505 | } |
||
506 | |||
507 | View Code Duplication | public function fbshareAction() |
|
527 | |||
528 | public function fbrequestAction() |
||
550 | |||
551 | public function tweetAction() |
||
570 | |||
571 | View Code Duplication | public function googleAction() |
|
592 | |||
593 | public function optinAction() |
||
594 | { |
||
595 | $userService = $this->getServiceLocator()->get('zfcuser_user_service'); |
||
596 | |||
597 | if ($this->getRequest()->isPost()) { |
||
598 | $data['optin'] = ($this->params()->fromPost('optin'))? 1:0; |
||
599 | $data['optinPartner'] = ($this->params()->fromPost('optinPartner'))? 1:0; |
||
600 | |||
601 | $userService->updateNewsletter($data); |
||
602 | } |
||
603 | |||
604 | return $this->redirect()->toUrl( |
||
605 | $this->frontendUrl()->fromRoute( |
||
606 | 'frontend/' . $this->game->getClassType() . '/index', |
||
607 | array('id' => $this->game->getIdentifier()) |
||
608 | ) |
||
609 | ); |
||
610 | } |
||
611 | |||
612 | public function loginAction() |
||
613 | { |
||
614 | $request = $this->getRequest(); |
||
615 | $form = $this->getServiceLocator()->get('zfcuser_login_form'); |
||
616 | |||
617 | if ($request->isPost()) { |
||
618 | $form->setData($request->getPost()); |
||
619 | |||
620 | if (!$form->isValid()) { |
||
621 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( |
||
622 | 'Authentication failed. Please try again.' |
||
623 | ); |
||
624 | |||
625 | |||
626 | return $this->redirect()->toUrl( |
||
627 | $this->frontendUrl()->fromRoute( |
||
628 | $this->game->getClassType() . '/login', |
||
629 | array('id' => $this->game->getIdentifier()) |
||
630 | ) |
||
631 | ); |
||
632 | } |
||
633 | |||
634 | // clear adapters |
||
635 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
636 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
637 | |||
638 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
639 | |||
640 | View Code Duplication | if (!$logged) { |
|
641 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( |
||
642 | 'Authentication failed. Please try again.' |
||
643 | ); |
||
644 | |||
645 | return $this->redirect()->toUrl( |
||
646 | $this->frontendUrl()->fromRoute( |
||
647 | $this->game->getClassType() . '/login', |
||
648 | array('id' => $this->game->getIdentifier()) |
||
649 | ) |
||
650 | ); |
||
651 | } else { |
||
652 | return $this->redirect()->toUrl( |
||
653 | $this->frontendUrl()->fromRoute( |
||
654 | $this->game->getClassType() . '/' . $this->game->nextStep('index'), |
||
655 | array('id' => $this->game->getIdentifier()) |
||
656 | ) |
||
657 | ); |
||
658 | } |
||
659 | } |
||
660 | |||
661 | $form->setAttribute( |
||
662 | 'action', |
||
663 | $this->frontendUrl()->fromRoute( |
||
664 | $this->game->getClassType().'/login', |
||
665 | array('id' => $this->game->getIdentifier()) |
||
666 | ) |
||
667 | ); |
||
668 | $viewModel = $this->buildView($this->game); |
||
669 | $viewModel->setVariables(array( |
||
670 | 'form' => $form, |
||
671 | )); |
||
672 | return $viewModel; |
||
673 | } |
||
674 | |||
675 | public function userregisterAction() |
||
676 | { |
||
677 | $userOptions = $this->getServiceLocator()->get('zfcuser_module_options'); |
||
678 | |||
679 | if ($this->zfcUserAuthentication()->hasIdentity()) { |
||
680 | return $this->redirect()->toUrl( |
||
681 | $this->frontendUrl()->fromRoute( |
||
682 | $this->game->getClassType().'/'.$this->game->nextStep('index'), |
||
683 | array('id' => $this->game->getIdentifier()) |
||
684 | ) |
||
685 | ); |
||
686 | } |
||
687 | $request = $this->getRequest(); |
||
688 | $service = $this->getServiceLocator()->get('zfcuser_user_service'); |
||
689 | $form = $this->getServiceLocator()->get('playgroundgame_register_form'); |
||
690 | $socialnetwork = $this->params()->fromRoute('socialnetwork', false); |
||
691 | $form->setAttribute( |
||
692 | 'action', |
||
693 | $this->frontendUrl()->fromRoute( |
||
694 | $this->game->getClassType().'/user-register', |
||
695 | array('id' => $this->game->getIdentifier()) |
||
696 | ) |
||
697 | ); |
||
698 | $params = array(); |
||
699 | $socialCredentials = array(); |
||
700 | |||
701 | if ($userOptions->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) { |
||
702 | $redirect = $request->getQuery()->get('redirect'); |
||
703 | } else { |
||
704 | $redirect = false; |
||
705 | } |
||
706 | |||
707 | if ($socialnetwork) { |
||
708 | $infoMe = $this->getProviderService()->getInfoMe($socialnetwork); |
||
709 | |||
710 | if (!empty($infoMe)) { |
||
711 | $user = $this->getProviderService()->getUserProviderMapper()->findUserByProviderId( |
||
712 | $infoMe->identifier, |
||
713 | $socialnetwork |
||
714 | ); |
||
715 | |||
716 | if ($user || $service->getOptions()->getCreateUserAutoSocial() === true) { |
||
717 | //on le dirige vers l'action d'authentification |
||
718 | if (! $redirect && $userOptions->getLoginRedirectRoute() != '') { |
||
719 | $redirect = $this->frontendUrl()->fromRoute( |
||
720 | $this->game->getClassType().'/login', |
||
721 | array('id' => $this->game->getIdentifier()) |
||
722 | ); |
||
723 | } |
||
724 | $redir = $this->frontendUrl()->fromRoute( |
||
725 | $this->game->getClassType().'/login', |
||
726 | array('id' => $this->game->getIdentifier()) |
||
727 | ) .'/' . $socialnetwork . ($redirect ? '?redirect=' . $redirect : ''); |
||
728 | |||
729 | return $this->redirect()->toUrl($redir); |
||
730 | } |
||
731 | |||
732 | // Je retire la saisie du login/mdp |
||
733 | $form->setAttribute( |
||
734 | 'action', |
||
735 | $this->frontendUrl()->fromRoute( |
||
736 | $this->game->getClassType().'/user-register', |
||
737 | array( |
||
738 | 'id' => $this->game->getIdentifier(), |
||
739 | 'socialnetwork' => $socialnetwork, |
||
740 | |||
741 | ) |
||
742 | ) |
||
743 | ); |
||
744 | $form->remove('password'); |
||
745 | $form->remove('passwordVerify'); |
||
746 | |||
747 | $birthMonth = $infoMe->birthMonth; |
||
748 | if (strlen($birthMonth) <= 1) { |
||
749 | $birthMonth = '0'.$birthMonth; |
||
750 | } |
||
751 | $birthDay = $infoMe->birthDay; |
||
752 | if (strlen($birthDay) <= 1) { |
||
753 | $birthDay = '0'.$birthDay; |
||
754 | } |
||
755 | |||
756 | $gender = $infoMe->gender; |
||
757 | if ($gender == 'female') { |
||
758 | $title = 'Me'; |
||
759 | } else { |
||
760 | $title = 'M'; |
||
761 | } |
||
762 | |||
763 | $params = array( |
||
764 | //'birth_year' => $infoMe->birthYear, |
||
765 | 'title' => $title, |
||
766 | 'dob' => $birthDay.'/'.$birthMonth.'/'.$infoMe->birthYear, |
||
767 | 'firstname' => $infoMe->firstName, |
||
768 | 'lastname' => $infoMe->lastName, |
||
769 | 'email' => $infoMe->email, |
||
770 | 'postalCode' => $infoMe->zip, |
||
771 | ); |
||
772 | $socialCredentials = array( |
||
773 | 'socialNetwork' => strtolower($socialnetwork), |
||
774 | 'socialId' => $infoMe->identifier, |
||
775 | ); |
||
776 | } |
||
777 | } |
||
778 | |||
779 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
780 | $this->game->getClassType().'/user-register', |
||
781 | array('id' => $this->game->getIdentifier()) |
||
782 | ) .($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
783 | $prg = $this->prg($redirectUrl, true); |
||
784 | |||
785 | if ($prg instanceof Response) { |
||
786 | return $prg; |
||
787 | } elseif ($prg === false) { |
||
788 | $form->setData($params); |
||
789 | $viewModel = $this->buildView($this->game); |
||
790 | $viewModel->setVariables(array( |
||
791 | 'registerForm' => $form, |
||
792 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
793 | 'redirect' => $redirect, |
||
794 | )); |
||
795 | return $viewModel; |
||
796 | } |
||
797 | |||
798 | $post = $prg; |
||
799 | $post = array_merge( |
||
800 | $post, |
||
801 | $socialCredentials |
||
802 | ); |
||
803 | |||
804 | $user = $service->register($post, 'playgroundgame_register_form'); |
||
805 | |||
806 | if (! $user) { |
||
807 | $viewModel = $this->buildView($this->game); |
||
808 | $viewModel->setVariables(array( |
||
809 | 'registerForm' => $form, |
||
810 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
811 | 'redirect' => $redirect, |
||
812 | )); |
||
813 | |||
814 | return $viewModel; |
||
815 | } |
||
816 | |||
817 | if ($service->getOptions()->getEmailVerification()) { |
||
818 | $vm = new ViewModel(array('userEmail' => $user->getEmail())); |
||
819 | $vm->setTemplate('playground-user/register/registermail'); |
||
820 | |||
821 | return $vm; |
||
822 | } elseif ($service->getOptions()->getLoginAfterRegistration()) { |
||
823 | $identityFields = $service->getOptions()->getAuthIdentityFields(); |
||
824 | if (in_array('email', $identityFields)) { |
||
825 | $post['identity'] = $user->getEmail(); |
||
826 | } elseif (in_array('username', $identityFields)) { |
||
827 | $post['identity'] = $user->getUsername(); |
||
828 | } |
||
829 | $post['credential'] = isset($post['password'])?$post['password']:''; |
||
830 | $request->setPost(new Parameters($post)); |
||
831 | |||
832 | // clear adapters |
||
833 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
834 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
835 | |||
836 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
837 | |||
838 | View Code Duplication | if ($logged) { |
|
839 | return $this->redirect()->toUrl( |
||
840 | $this->frontendUrl()->fromRoute( |
||
841 | $this->game->getClassType() . '/' . $this->game->nextStep('index'), |
||
842 | array('id' => $this->game->getIdentifier()) |
||
843 | ) |
||
844 | ); |
||
845 | } else { |
||
846 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( |
||
847 | 'Authentication failed. Please try again.' |
||
848 | ); |
||
849 | return $this->redirect()->toUrl( |
||
850 | $this->frontendUrl()->fromRoute( |
||
851 | $this->game->getClassType() . '/login', |
||
852 | array('id' => $this->game->getIdentifier()) |
||
853 | ) |
||
854 | ); |
||
855 | } |
||
856 | } |
||
857 | |||
858 | $redirect = $this->frontendUrl()->fromRoute( |
||
859 | $this->game->getClassType().'/login', |
||
860 | array( |
||
861 | 'id' => $this->game->getIdentifier(), |
||
862 | |||
863 | ) |
||
864 | ) . ($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
865 | |||
866 | return $this->redirect()->toUrl($redirect); |
||
867 | } |
||
868 | |||
869 | /** |
||
870 | * |
||
871 | * @param \PlaygroundGame\Entity\Game $game |
||
872 | * @param \PlaygroundUser\Entity\User $user |
||
873 | */ |
||
874 | public function checkFbRegistration($user, $game) |
||
875 | { |
||
876 | $redirect = false; |
||
877 | $session = new Container('facebook'); |
||
878 | if ($session->offsetExists('signed_request')) { |
||
879 | if (!$user) { |
||
880 | // Get Playground user from Facebook info |
||
881 | $beforeLayout = $this->layout()->getTemplate(); |
||
882 | $view = $this->forward()->dispatch( |
||
883 | 'playgrounduser_user', |
||
884 | array( |
||
885 | 'controller' => 'playgrounduser_user', |
||
886 | 'action' => 'registerFacebookUser', |
||
887 | 'provider' => 'facebook' |
||
888 | ) |
||
889 | ); |
||
890 | |||
891 | $this->layout()->setTemplate($beforeLayout); |
||
892 | $user = $view->user; |
||
893 | |||
894 | // If the user can not be created/retrieved from Facebook info, redirect to login/register form |
||
895 | if (!$user) { |
||
896 | $redirectUrl = urlencode( |
||
897 | $this->frontendUrl()->fromRoute( |
||
898 | $game->getClassType() .'/play', |
||
899 | array('id' => $game->getIdentifier()), |
||
900 | array('force_canonical' => true) |
||
901 | ) |
||
902 | ); |
||
903 | $redirect = $this->redirect()->toUrl( |
||
904 | $this->frontendUrl()->fromRoute( |
||
905 | 'zfcuser/register' |
||
906 | ) . '?redirect='.$redirectUrl |
||
907 | ); |
||
908 | } |
||
909 | } |
||
910 | |||
911 | if ($game->getFbFan()) { |
||
912 | if ($this->getGameService()->checkIsFan($game) === false) { |
||
913 | $redirect = $this->redirect()->toRoute( |
||
914 | $game->getClassType().'/fangate', |
||
915 | array('id' => $game->getIdentifier()) |
||
916 | ); |
||
917 | } |
||
918 | } |
||
919 | } |
||
920 | |||
921 | return $redirect; |
||
922 | } |
||
923 | |||
924 | /** |
||
925 | * This method create the basic Game view |
||
926 | * @param \PlaygroundGame\Entity\Game $game |
||
927 | */ |
||
928 | public function buildView($game) |
||
929 | { |
||
930 | if ($this->getRequest()->isXmlHttpRequest()) { |
||
931 | $viewModel = new JsonModel(); |
||
932 | if ($game) { |
||
933 | $view = $this->addAdditionalView($game); |
||
934 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
935 | $viewModel->setVariables($view->getVariables()); |
||
936 | } |
||
937 | } |
||
938 | } else { |
||
939 | $viewModel = new ViewModel(); |
||
940 | |||
941 | if ($game) { |
||
942 | $this->addMetaTitle($game); |
||
943 | $this->addMetaBitly(); |
||
944 | $this->addGaEvent($game); |
||
945 | |||
946 | $this->customizeGameDesign($game); |
||
947 | |||
948 | // this is possible to create a specific game design in /design/frontend/default/custom. |
||
949 | //It will precede all others templates. |
||
950 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
951 | $l = $templatePathResolver->getPaths(); |
||
952 | $templatePathResolver->addPath($l[0].'custom/'.$game->getIdentifier()); |
||
953 | |||
954 | $view = $this->addAdditionalView($game); |
||
955 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
956 | $viewModel->addChild($view, 'additional'); |
||
957 | } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
958 | return $view; |
||
959 | } |
||
960 | |||
961 | $this->layout()->setVariables( |
||
962 | array( |
||
963 | 'action' => $this->params('action'), |
||
964 | 'game' => $game, |
||
965 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
966 | |||
967 | ) |
||
968 | ); |
||
969 | } |
||
970 | } |
||
971 | |||
972 | if ($game) { |
||
973 | $viewModel->setVariables($this->getShareData($game)); |
||
974 | $viewModel->setVariables(array('game' => $game)); |
||
975 | } |
||
976 | |||
977 | return $viewModel; |
||
978 | } |
||
979 | |||
980 | /** |
||
981 | * @param \PlaygroundGame\Entity\Game $game |
||
982 | */ |
||
983 | public function addAdditionalView($game) |
||
984 | { |
||
985 | $view = false; |
||
986 | |||
987 | $actionName = $this->getEvent()->getRouteMatch()->getParam('action', 'not-found'); |
||
988 | $stepsViews = json_decode($game->getStepsViews(), true); |
||
989 | if ($stepsViews && isset($stepsViews[$actionName])) { |
||
990 | $beforeLayout = $this->layout()->getTemplate(); |
||
991 | $actionData = $stepsViews[$actionName]; |
||
992 | if (is_string($actionData)) { |
||
993 | $action = $actionData; |
||
994 | $controller = $this->getEvent()->getRouteMatch()->getParam('controller', 'playgroundgame_game'); |
||
995 | $view = $this->forward()->dispatch( |
||
996 | $controller, |
||
997 | array( |
||
998 | 'action' => $action, |
||
999 | 'id' => $game->getIdentifier() |
||
1000 | ) |
||
1001 | ); |
||
1002 | } elseif (is_array($actionData) && count($actionData)>0) { |
||
1003 | $action = key($actionData); |
||
1004 | $controller = $actionData[$action]; |
||
1005 | $view = $this->forward()->dispatch( |
||
1006 | $controller, |
||
1007 | array( |
||
1008 | 'action' => $action, |
||
1009 | 'id' => $game->getIdentifier() |
||
1010 | ) |
||
1011 | ); |
||
1012 | } |
||
1013 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
1014 | $this->layout()->setTemplate($beforeLayout); |
||
1015 | } |
||
1016 | |||
1017 | return $view; |
||
1018 | } |
||
1019 | |||
1020 | public function addMetaBitly() |
||
1021 | { |
||
1022 | $bitlyclient = $this->getOptions()->getBitlyUrl(); |
||
1023 | $bitlyuser = $this->getOptions()->getBitlyUsername(); |
||
1024 | $bitlykey = $this->getOptions()->getBitlyApiKey(); |
||
1025 | |||
1026 | $this->getViewHelper('HeadMeta')->setProperty('bt:client', $bitlyclient); |
||
1027 | $this->getViewHelper('HeadMeta')->setProperty('bt:user', $bitlyuser); |
||
1028 | $this->getViewHelper('HeadMeta')->setProperty('bt:key', $bitlykey); |
||
1029 | } |
||
1030 | |||
1031 | /** |
||
1032 | * @param \PlaygroundGame\Entity\Game $game |
||
1033 | */ |
||
1034 | public function addGaEvent($game) |
||
1035 | { |
||
1036 | // Google Analytics event |
||
1037 | $ga = $this->getServiceLocator()->get('google-analytics'); |
||
1038 | $event = new \PlaygroundCore\Analytics\Event($game->getClassType(), $this->params('action')); |
||
1039 | $event->setLabel($game->getTitle()); |
||
1040 | $ga->addEvent($event); |
||
1041 | } |
||
1042 | |||
1043 | /** |
||
1044 | * @param \PlaygroundGame\Entity\Game $game |
||
1045 | */ |
||
1046 | public function addMetaTitle($game) |
||
1066 | |||
1067 | /** |
||
1068 | * @param \PlaygroundGame\Entity\Game $game |
||
1069 | */ |
||
1070 | public function customizeGameDesign($game) |
||
1085 | |||
1086 | /** |
||
1087 | * @param \PlaygroundGame\Entity\Game $game |
||
1088 | */ |
||
1089 | public function getShareData($game) |
||
1175 | |||
1176 | /** |
||
1177 | * return ajax response in json format |
||
1178 | * |
||
1179 | * @param array $data |
||
1180 | * @return \Zend\View\Model\JsonModel |
||
1181 | */ |
||
1182 | View Code Duplication | protected function successJson($data = null) |
|
1190 | |||
1191 | /** |
||
1192 | * return ajax response in json format |
||
1193 | * |
||
1194 | * @param string $message |
||
1195 | * @return \Zend\View\Model\JsonModel |
||
1196 | */ |
||
1197 | View Code Duplication | protected function errorJson($message = null) |
|
1205 | |||
1206 | /** |
||
1207 | * @param string $helperName |
||
1208 | */ |
||
1209 | protected function getViewHelper($helperName) |
||
1213 | |||
1214 | public function getGameService() |
||
1222 | |||
1223 | public function setGameService(GameService $gameService) |
||
1224 | { |
||
1225 | $this->gameService = $gameService; |
||
1226 | |||
1227 | return $this; |
||
1228 | } |
||
1229 | |||
1230 | public function getPrizeService() |
||
1231 | { |
||
1232 | if (!$this->prizeService) { |
||
1233 | $this->prizeService = $this->getServiceLocator()->get('playgroundgame_prize_service'); |
||
1234 | } |
||
1235 | |||
1236 | return $this->prizeService; |
||
1237 | } |
||
1238 | |||
1239 | public function setPrizeService(PrizeService $prizeService) |
||
1240 | { |
||
1241 | $this->prizeService = $prizeService; |
||
1242 | |||
1243 | return $this; |
||
1244 | } |
||
1245 | |||
1246 | public function getOptions() |
||
1254 | |||
1255 | public function setOptions($options) |
||
1261 | } |
||
1262 |
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: