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 | 'inviteToTeam', |
||
38 | 'prizes', |
||
39 | 'prize', |
||
40 | 'fangate', |
||
41 | 'share', |
||
42 | 'optin', |
||
43 | 'login', |
||
44 | 'logout', |
||
45 | 'ajaxforgot', |
||
46 | 'play', |
||
47 | 'result', |
||
48 | 'preview', |
||
49 | 'list' |
||
50 | ); |
||
51 | |||
52 | protected $withOnlineGame = array( |
||
53 | 'leaderboard', |
||
54 | 'register', |
||
55 | 'bounce', |
||
56 | 'play', |
||
57 | 'result' |
||
58 | ); |
||
59 | |||
60 | protected $withAnyUser = array( |
||
61 | 'share', |
||
62 | 'result', |
||
63 | 'play', |
||
64 | 'logout', |
||
65 | 'inviteToTeam' |
||
66 | ); |
||
67 | |||
68 | public function setEventManager(\Zend\EventManager\EventManagerInterface $events) |
||
130 | |||
131 | /** |
||
132 | * Action called if matched action does not exist |
||
133 | * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy |
||
134 | * it has to be rendered in the controller. Hence the code below. |
||
135 | * |
||
136 | * This action is injected as a catchall action for each custom_games definition |
||
137 | * This way, when a custom_game is created, the 404 is it's responsability and the |
||
138 | * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml |
||
139 | * |
||
140 | * |
||
141 | * @return \Zend\Stdlib\ResponseInterface |
||
142 | */ |
||
143 | public function notFoundAction() |
||
144 | { |
||
145 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
146 | |||
147 | // I create a template path in which I can find a custom template |
||
148 | $controller = explode('\\', get_class($this)); |
||
149 | $controllerPath = str_replace('Controller', '', end($controller)); |
||
150 | $controllerPath = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '-\\1', $controllerPath)); |
||
151 | $uri = $this->getRequest()->getUri()->getPath(); |
||
152 | if ($this->game) { |
||
153 | $uri = str_replace($controllerPath.'/'.$this->game->getIdentifier().'/', '', $uri); |
||
154 | } |
||
155 | $uri = str_replace("/" . $this->getEvent()->getRouteMatch()->getParam('locale') . "/", "/", $uri); |
||
156 | $template = 'playground-game/'.$controllerPath . '/custom' . $uri; |
||
157 | |||
158 | if (false === $templatePathResolver->resolve($template)) { |
||
159 | $viewRender = $this->getServiceLocator()->get('ViewRenderer'); |
||
160 | |||
161 | $this->getEvent()->getRouteMatch()->setParam('action', 'not-found'); |
||
162 | $this->response->setStatusCode(404); |
||
163 | |||
164 | $res = 'error/404'; |
||
165 | |||
166 | $viewModel = $this->buildView($this->game); |
||
167 | $viewModel->setTemplate($res); |
||
168 | |||
169 | $this->layout()->setVariable("content", $viewRender->render($viewModel)); |
||
170 | $this->response->setContent($viewRender->render($this->layout())); |
||
171 | |||
172 | return $this->response; |
||
173 | } |
||
174 | |||
175 | $viewModel = $this->buildView($this->game); |
||
176 | $viewModel->setTemplate($template); |
||
177 | |||
178 | return $viewModel; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * This action acts as a hub : Depending on the first step of the game, it will forward the action to this step |
||
183 | */ |
||
184 | public function homeAction() |
||
216 | |||
217 | /** |
||
218 | * Homepage of the game |
||
219 | */ |
||
220 | public function indexAction() |
||
236 | |||
237 | /** |
||
238 | * leaderboardAction |
||
239 | * |
||
240 | * @return ViewModel $viewModel |
||
241 | */ |
||
242 | public function leaderboardAction() |
||
267 | |||
268 | /** |
||
269 | * This action has been designed to be called by other controllers |
||
270 | * It gives the ability to display an information form and persist it in the game entry |
||
271 | * |
||
272 | * @return \Zend\View\Model\ViewModel |
||
273 | */ |
||
274 | public function registerAction() |
||
275 | { |
||
276 | $form = $this->getGameService()->createFormFromJson($this->game->getPlayerForm()->getForm(), 'playerForm'); |
||
277 | |||
278 | if ($this->getRequest()->isPost()) { |
||
279 | // POST Request: Process form |
||
280 | $data = array_merge_recursive( |
||
281 | $this->getRequest()->getPost()->toArray(), |
||
282 | $this->getRequest()->getFiles()->toArray() |
||
283 | ); |
||
284 | |||
285 | $form->setData($data); |
||
286 | |||
287 | if ($form->isValid()) { |
||
288 | // steps of the game |
||
289 | $steps = $this->game->getStepsArray(); |
||
290 | // sub steps of the game |
||
291 | $viewSteps = $this->game->getStepsViewsArray(); |
||
292 | |||
293 | // register position |
||
294 | $key = array_search($this->params('action'), $viewSteps); |
||
295 | if (!$key) { |
||
296 | // register is not a substep of the game so it's a step |
||
297 | $key = array_search($this->params('action'), $steps); |
||
298 | $keyStep = true; |
||
299 | } else { |
||
300 | // register was a substep, i search the index of its parent |
||
301 | $key = array_search($key, $steps); |
||
302 | $keyStep = false; |
||
303 | } |
||
304 | |||
305 | // play position |
||
306 | $keyplay = array_search('play', $viewSteps); |
||
307 | |||
308 | if (!$keyplay) { |
||
309 | // play is not a substep, so it's a step |
||
310 | $keyplay = array_search('play', $steps); |
||
311 | $keyplayStep = true; |
||
312 | } else { |
||
313 | // play is a substep so I search the index of its parent |
||
314 | $keyplay = array_search($keyplay, $steps); |
||
315 | $keyplayStep = false; |
||
316 | } |
||
317 | |||
318 | // If register step before play, I don't have no entry yet. I have to create one |
||
319 | // If register after play step, I search for the last entry created by play step. |
||
320 | |||
321 | if ($key < $keyplay || ($keyStep && !$keyplayStep && $key <= $keyplay)) { |
||
322 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
323 | View Code Duplication | if (!$entry) { |
|
324 | // the user has already taken part of this game and the participation limit has been reached |
||
325 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
326 | |||
327 | return $this->redirect()->toUrl( |
||
328 | $this->frontendUrl()->fromRoute( |
||
329 | $this->game->getClassType().'/result', |
||
330 | array( |
||
331 | 'id' => $this->game->getIdentifier(), |
||
332 | |||
333 | ) |
||
334 | ) |
||
335 | ); |
||
336 | } |
||
337 | } else { |
||
338 | // I'm looking for an entry without anonymousIdentifier (the active entry in fact). |
||
339 | $entry = $this->getGameService()->findLastEntry($this->game, $this->user); |
||
340 | if ($this->getGameService()->hasReachedPlayLimit($this->game, $this->user)) { |
||
341 | // the user has already taken part of this game and the participation limit has been reached |
||
342 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
343 | |||
344 | return $this->redirect()->toUrl( |
||
345 | $this->frontendUrl()->fromRoute( |
||
346 | $this->game->getClassType().'/result', |
||
347 | array( |
||
348 | 'id' => $this->game->getIdentifier(), |
||
349 | |||
350 | ) |
||
351 | ) |
||
352 | ); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | $this->getGameService()->updateEntryPlayerForm($form->getData(), $this->game, $this->user, $entry); |
||
357 | |||
358 | if (!empty($this->game->nextStep($this->params('action')))) { |
||
359 | return $this->redirect()->toUrl( |
||
360 | $this->frontendUrl()->fromRoute( |
||
361 | $this->game->getClassType() .'/' . $this->game->nextStep($this->params('action')), |
||
362 | array('id' => $this->game->getIdentifier()), |
||
363 | array('force_canonical' => true) |
||
364 | ) |
||
365 | ); |
||
366 | } |
||
367 | } |
||
368 | } |
||
369 | |||
370 | $viewModel = $this->buildView($this->game); |
||
371 | $viewModel->setVariables(array( |
||
372 | 'form' => $form |
||
373 | )); |
||
374 | |||
375 | return $viewModel; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * This action takes care of the terms of the game |
||
380 | */ |
||
381 | public function termsAction() |
||
387 | |||
388 | /** |
||
389 | * This action takes care of the conditions of the game |
||
390 | */ |
||
391 | public function conditionsAction() |
||
397 | |||
398 | /** |
||
399 | * This action takes care of bounce page of the game |
||
400 | */ |
||
401 | public function bounceAction() |
||
420 | |||
421 | |||
422 | /** |
||
423 | * This action displays the Prizes page associated to the game |
||
424 | */ |
||
425 | public function prizesAction() |
||
435 | |||
436 | /** |
||
437 | * This action displays a specific Prize page among those associated to the game |
||
438 | */ |
||
439 | public function prizeAction() |
||
453 | |||
454 | public function gameslistAction() |
||
501 | |||
502 | public function fangateAction() |
||
508 | |||
509 | public function shareAction() |
||
510 | { |
||
511 | $statusMail = null; |
||
512 | $lastEntry = null; |
||
513 | |||
514 | // steps of the game |
||
515 | $steps = $this->game->getStepsArray(); |
||
516 | // sub steps of the game |
||
517 | $viewSteps = $this->game->getStepsViewsArray(); |
||
518 | |||
519 | // share position |
||
520 | $key = array_search($this->params('action'), $viewSteps); |
||
521 | if (!$key) { |
||
522 | // share is not a substep of the game so it's a step |
||
523 | $key = array_search($this->params('action'), $steps); |
||
524 | } else { |
||
525 | // share was a substep, I search the index of its parent |
||
526 | $key = array_search($key, $steps); |
||
527 | } |
||
528 | |||
529 | // play position |
||
530 | $keyplay = array_search('play', $viewSteps); |
||
531 | |||
532 | if (!$keyplay) { |
||
533 | // play is not a substep, so it's a step |
||
534 | $keyplay = array_search('play', $steps); |
||
535 | } else { |
||
536 | // play is a substep so I search the index of its parent |
||
537 | $keyplay = array_search($keyplay, $steps); |
||
538 | } |
||
539 | |||
540 | if ($key && $keyplay && $keyplay <= $key) { |
||
541 | // Has the user finished the game ? |
||
542 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
543 | |||
544 | View Code Duplication | if ($lastEntry === null) { |
|
545 | return $this->redirect()->toUrl( |
||
546 | $this->frontendUrl()->fromRoute( |
||
547 | $this->game->getClassType(), |
||
548 | array('id' => $this->game->getIdentifier()) |
||
549 | ) |
||
550 | ); |
||
551 | } |
||
552 | } |
||
553 | |||
554 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
555 | $form->setAttribute('method', 'post'); |
||
556 | |||
557 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
558 | $viewModel = $this->buildView($this->game); |
||
559 | |||
560 | if ($this->getRequest()->isPost()) { |
||
561 | $data = $this->getRequest()->getPost()->toArray(); |
||
562 | $form->setData($data); |
||
563 | View Code Duplication | if ($form->isValid()) { |
|
564 | $result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
||
565 | if ($result) { |
||
566 | $statusMail = true; |
||
567 | } |
||
568 | } |
||
569 | } |
||
570 | |||
571 | $viewModel->setVariables(array( |
||
572 | 'statusMail' => $statusMail, |
||
573 | 'form' => $form, |
||
574 | )); |
||
575 | |||
576 | return $viewModel; |
||
577 | } |
||
578 | |||
579 | public function inviteToTeamAction() |
||
610 | |||
611 | View Code Duplication | public function fbshareAction() |
|
612 | { |
||
613 | $viewModel = new JsonModel(); |
||
614 | $viewModel->setTerminal(true); |
||
615 | $fbId = $this->params()->fromQuery('fbId'); |
||
616 | if (!$this->game) { |
||
617 | return $this->errorJson(); |
||
618 | } |
||
619 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user); |
||
620 | if (! $entry) { |
||
621 | return $this->errorJson(); |
||
622 | } |
||
623 | if (!$fbId) { |
||
624 | return $this->errorJson(); |
||
625 | } |
||
626 | |||
627 | $this->getGameService()->postFbWall($fbId, $this->game, $this->user, $entry); |
||
628 | |||
629 | return $this->successJson(); |
||
630 | } |
||
631 | |||
632 | public function fbrequestAction() |
||
633 | { |
||
634 | $viewModel = new ViewModel(); |
||
635 | $viewModel->setTerminal(true); |
||
636 | $fbId = $this->params()->fromQuery('fbId'); |
||
637 | $to = $this->params()->fromQuery('to'); |
||
638 | |||
639 | if (!$this->game) { |
||
640 | return $this->errorJson(); |
||
641 | } |
||
642 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user); |
||
643 | if (! $entry) { |
||
644 | return $this->errorJson(); |
||
645 | } |
||
646 | if (!$fbId) { |
||
647 | return $this->errorJson(); |
||
648 | } |
||
649 | |||
650 | $this->getGameService()->postFbRequest($fbId, $this->game, $this->user, $entry, $to); |
||
651 | |||
652 | return $this->successJson(); |
||
653 | } |
||
654 | |||
655 | public function tweetAction() |
||
656 | { |
||
657 | $tweetId = $this->params()->fromQuery('tweetId'); |
||
658 | |||
659 | if (!$this->game) { |
||
660 | return $this->errorJson(); |
||
661 | } |
||
662 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user); |
||
663 | if (! $entry) { |
||
664 | return $this->errorJson(); |
||
665 | } |
||
666 | if (!$tweetId) { |
||
667 | return $this->errorJson(); |
||
668 | } |
||
669 | |||
670 | $this->getGameService()->postTwitter($tweetId, $this->game, $this->user, $entry); |
||
671 | |||
672 | return $this->successJson(); |
||
673 | } |
||
674 | |||
675 | View Code Duplication | public function googleAction() |
|
676 | { |
||
677 | $viewModel = new ViewModel(); |
||
678 | $viewModel->setTerminal(true); |
||
679 | $googleId = $this->params()->fromQuery('googleId'); |
||
680 | |||
681 | if (!$this->game) { |
||
682 | return $this->errorJson(); |
||
683 | } |
||
684 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user); |
||
685 | if (! $entry) { |
||
686 | return $this->errorJson(); |
||
687 | } |
||
688 | if (!$googleId) { |
||
689 | return $this->errorJson(); |
||
690 | } |
||
691 | |||
692 | $this->getGameService()->postGoogle($googleId, $this->game, $this->user, $entry); |
||
693 | |||
694 | return $this->successJson(); |
||
695 | } |
||
696 | |||
697 | public function optinAction() |
||
698 | { |
||
699 | $userService = $this->getServiceLocator()->get('zfcuser_user_service'); |
||
700 | |||
701 | if ($this->getRequest()->isPost()) { |
||
702 | $data['optin'] = ($this->params()->fromPost('optin'))? 1:0; |
||
703 | $data['optinPartner'] = ($this->params()->fromPost('optinPartner'))? 1:0; |
||
704 | |||
705 | $userService->updateNewsletter($data); |
||
706 | } |
||
707 | |||
708 | return $this->redirect()->toUrl( |
||
709 | $this->frontendUrl()->fromRoute( |
||
710 | $this->game->getClassType(), |
||
711 | array('id' => $this->game->getIdentifier()) |
||
712 | ) |
||
713 | ); |
||
714 | } |
||
715 | |||
716 | public function loginAction() |
||
717 | { |
||
718 | $request = $this->getRequest(); |
||
719 | $form = $this->getServiceLocator()->get('zfcuser_login_form'); |
||
720 | |||
721 | if ($request->isPost()) { |
||
722 | $form->setData($request->getPost()); |
||
723 | |||
724 | if (!$form->isValid()) { |
||
725 | $this->flashMessenger()->addMessage( |
||
726 | 'Authentication failed. Please try again.' |
||
727 | ); |
||
728 | |||
729 | $viewModel = $this->buildView($this->game); |
||
730 | $viewModel->setVariables(array( |
||
731 | 'form' => $form, |
||
732 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
733 | )); |
||
734 | |||
735 | return $viewModel; |
||
736 | } |
||
737 | |||
738 | // clear adapters |
||
739 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
740 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
741 | |||
742 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
743 | |||
744 | if (!$logged) { |
||
745 | $this->flashMessenger()->addMessage( |
||
746 | 'Authentication failed. Please try again.' |
||
747 | ); |
||
748 | |||
749 | return $this->redirect()->toUrl( |
||
750 | $this->frontendUrl()->fromRoute( |
||
751 | $this->game->getClassType() . '/login', |
||
752 | array('id' => $this->game->getIdentifier()) |
||
753 | ) |
||
754 | ); |
||
755 | } else { |
||
756 | return $this->redirect()->toUrl( |
||
757 | $this->frontendUrl()->fromRoute( |
||
758 | $this->game->getClassType() . '/index', |
||
759 | array('id' => $this->game->getIdentifier()) |
||
760 | ) |
||
761 | ); |
||
762 | } |
||
763 | } |
||
764 | |||
765 | $form->setAttribute( |
||
766 | 'action', |
||
767 | $this->frontendUrl()->fromRoute( |
||
768 | $this->game->getClassType().'/login', |
||
769 | array('id' => $this->game->getIdentifier()) |
||
770 | ) |
||
771 | ); |
||
772 | $viewModel = $this->buildView($this->game); |
||
773 | $viewModel->setVariables(array( |
||
774 | 'form' => $form, |
||
775 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
776 | )); |
||
777 | return $viewModel; |
||
778 | } |
||
779 | |||
780 | View Code Duplication | public function logoutAction() |
|
798 | |||
799 | public function userregisterAction() |
||
800 | { |
||
801 | $userOptions = $this->getServiceLocator()->get('zfcuser_module_options'); |
||
802 | |||
803 | if ($this->zfcUserAuthentication()->hasIdentity()) { |
||
804 | return $this->redirect()->toUrl( |
||
805 | $this->frontendUrl()->fromRoute( |
||
806 | $this->game->getClassType().'/'.$this->game->nextStep('index'), |
||
807 | array('id' => $this->game->getIdentifier()) |
||
808 | ) |
||
809 | ); |
||
810 | } |
||
811 | $request = $this->getRequest(); |
||
812 | $service = $this->getServiceLocator()->get('zfcuser_user_service'); |
||
813 | $form = $this->getServiceLocator()->get('playgroundgame_register_form'); |
||
814 | $socialnetwork = $this->params()->fromRoute('socialnetwork', false); |
||
815 | $form->setAttribute( |
||
816 | 'action', |
||
817 | $this->frontendUrl()->fromRoute( |
||
818 | $this->game->getClassType().'/user-register', |
||
819 | array('id' => $this->game->getIdentifier()) |
||
820 | ) |
||
821 | ); |
||
822 | $params = array(); |
||
823 | $socialCredentials = array(); |
||
824 | |||
825 | if ($userOptions->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) { |
||
826 | $redirect = $request->getQuery()->get('redirect'); |
||
827 | } else { |
||
828 | $redirect = false; |
||
829 | } |
||
830 | |||
831 | if ($socialnetwork) { |
||
832 | $infoMe = $this->getProviderService()->getInfoMe($socialnetwork); |
||
833 | |||
834 | if (!empty($infoMe)) { |
||
835 | $user = $this->getProviderService()->getUserProviderMapper()->findUserByProviderId( |
||
836 | $infoMe->identifier, |
||
837 | $socialnetwork |
||
838 | ); |
||
839 | |||
840 | if ($user || $service->getOptions()->getCreateUserAutoSocial() === true) { |
||
841 | //on le dirige vers l'action d'authentification |
||
842 | if (! $redirect && $userOptions->getLoginRedirectRoute() != '') { |
||
843 | $redirect = $this->frontendUrl()->fromRoute( |
||
844 | $this->game->getClassType().'/login', |
||
845 | array('id' => $this->game->getIdentifier()) |
||
846 | ); |
||
847 | } |
||
848 | $redir = $this->frontendUrl()->fromRoute( |
||
849 | $this->game->getClassType().'/login', |
||
850 | array('id' => $this->game->getIdentifier()) |
||
851 | ) .'/' . $socialnetwork . ($redirect ? '?redirect=' . $redirect : ''); |
||
852 | |||
853 | return $this->redirect()->toUrl($redir); |
||
854 | } |
||
855 | |||
856 | // Je retire la saisie du login/mdp |
||
857 | $form->setAttribute( |
||
858 | 'action', |
||
859 | $this->frontendUrl()->fromRoute( |
||
860 | $this->game->getClassType().'/user-register', |
||
861 | array( |
||
862 | 'id' => $this->game->getIdentifier(), |
||
863 | 'socialnetwork' => $socialnetwork, |
||
864 | |||
865 | ) |
||
866 | ) |
||
867 | ); |
||
868 | $form->remove('password'); |
||
869 | $form->remove('passwordVerify'); |
||
870 | |||
871 | $birthMonth = $infoMe->birthMonth; |
||
872 | if (strlen($birthMonth) <= 1) { |
||
873 | $birthMonth = '0'.$birthMonth; |
||
874 | } |
||
875 | $birthDay = $infoMe->birthDay; |
||
876 | if (strlen($birthDay) <= 1) { |
||
877 | $birthDay = '0'.$birthDay; |
||
878 | } |
||
879 | |||
880 | $gender = $infoMe->gender; |
||
881 | if ($gender == 'female') { |
||
882 | $title = 'Me'; |
||
883 | } else { |
||
884 | $title = 'M'; |
||
885 | } |
||
886 | |||
887 | $params = array( |
||
888 | //'birth_year' => $infoMe->birthYear, |
||
889 | 'title' => $title, |
||
890 | 'dob' => $birthDay.'/'.$birthMonth.'/'.$infoMe->birthYear, |
||
891 | 'firstname' => $infoMe->firstName, |
||
892 | 'lastname' => $infoMe->lastName, |
||
893 | 'email' => $infoMe->email, |
||
894 | 'postalCode' => $infoMe->zip, |
||
895 | ); |
||
896 | $socialCredentials = array( |
||
897 | 'socialNetwork' => strtolower($socialnetwork), |
||
898 | 'socialId' => $infoMe->identifier, |
||
899 | ); |
||
900 | } |
||
901 | } |
||
902 | |||
903 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
904 | $this->game->getClassType().'/user-register', |
||
905 | array('id' => $this->game->getIdentifier()) |
||
906 | ) .($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
907 | $prg = $this->prg($redirectUrl, true); |
||
908 | |||
909 | if ($prg instanceof Response) { |
||
910 | return $prg; |
||
911 | } elseif ($prg === false) { |
||
912 | $form->setData($params); |
||
913 | $viewModel = $this->buildView($this->game); |
||
914 | $viewModel->setVariables(array( |
||
915 | 'registerForm' => $form, |
||
916 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
917 | 'redirect' => $redirect, |
||
918 | )); |
||
919 | return $viewModel; |
||
920 | } |
||
921 | |||
922 | $post = $prg; |
||
923 | $post = array_merge( |
||
924 | $post, |
||
925 | $socialCredentials |
||
926 | ); |
||
927 | |||
928 | if ($this->game->getOnInvitation()) { |
||
929 | $credential = trim( |
||
930 | $post[$this->getGameService()->getOptions()->getOnInvitationField()] |
||
931 | ); |
||
932 | if (!$credential) { |
||
933 | $credential = $this->params()->fromQuery( |
||
934 | $this->getGameService()->getOptions()->getOnInvitationField() |
||
935 | ); |
||
936 | } |
||
937 | $found = $this->getGameService()->getInvitationMapper()->findOneBy(array('requestKey'=>$credential)); |
||
938 | |||
939 | if (!$found || !empty($found->getUser())) { |
||
940 | $this->flashMessenger()->addMessage( |
||
941 | 'Authentication failed. Please try again.' |
||
942 | ); |
||
943 | $form->setData($post); |
||
944 | $viewModel = $this->buildView($this->game); |
||
945 | $viewModel->setVariables(array( |
||
946 | 'registerForm' => $form, |
||
947 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
948 | 'redirect' => $redirect, |
||
949 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
950 | 'flashErrors' => $this->flashMessenger()->getErrorMessages(), |
||
951 | )); |
||
952 | |||
953 | return $viewModel; |
||
954 | } |
||
955 | } |
||
956 | |||
957 | $user = $service->register($post, 'playgroundgame_register_form'); |
||
958 | |||
959 | if (! $user) { |
||
960 | $viewModel = $this->buildView($this->game); |
||
961 | $viewModel->setVariables(array( |
||
962 | 'registerForm' => $form, |
||
963 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
964 | 'redirect' => $redirect, |
||
965 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
966 | 'flashErrors' => $this->flashMessenger()->getErrorMessages(), |
||
967 | )); |
||
968 | |||
969 | return $viewModel; |
||
970 | } |
||
971 | |||
972 | if ($this->game->getOnInvitation()) { |
||
973 | // user has been created, associate the code with the userId |
||
974 | $found->setUser($user); |
||
975 | $this->getGameService()->getInvitationMapper()->update($found); |
||
976 | } |
||
977 | |||
978 | if ($service->getOptions()->getEmailVerification()) { |
||
979 | $vm = new ViewModel(array('userEmail' => $user->getEmail())); |
||
980 | $vm->setTemplate('playground-user/register/registermail'); |
||
981 | |||
982 | return $vm; |
||
983 | } elseif ($service->getOptions()->getLoginAfterRegistration()) { |
||
984 | $identityFields = $service->getOptions()->getAuthIdentityFields(); |
||
985 | if (in_array('email', $identityFields)) { |
||
986 | $post['identity'] = $user->getEmail(); |
||
987 | } elseif (in_array('username', $identityFields)) { |
||
988 | $post['identity'] = $user->getUsername(); |
||
989 | } |
||
990 | $post['credential'] = isset($post['password'])?$post['password']:''; |
||
991 | $request->setPost(new Parameters($post)); |
||
992 | |||
993 | // clear adapters |
||
994 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
995 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
996 | |||
997 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
998 | |||
999 | if ($logged) { |
||
1000 | return $this->redirect()->toUrl( |
||
1001 | $this->frontendUrl()->fromRoute( |
||
1002 | $this->game->getClassType(), |
||
1003 | array('id' => $this->game->getIdentifier()) |
||
1004 | ) |
||
1005 | ); |
||
1006 | } else { |
||
1007 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( |
||
1008 | 'Authentication failed. Please try again.' |
||
1009 | ); |
||
1010 | return $this->redirect()->toUrl( |
||
1011 | $this->frontendUrl()->fromRoute( |
||
1012 | $this->game->getClassType() . '/login', |
||
1013 | array('id' => $this->game->getIdentifier()) |
||
1014 | ) |
||
1015 | ); |
||
1016 | } |
||
1017 | } |
||
1018 | |||
1019 | $redirect = $this->frontendUrl()->fromRoute( |
||
1020 | $this->game->getClassType().'/login', |
||
1021 | array('id' => $this->game->getIdentifier()) |
||
1022 | ) . ($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
1023 | |||
1024 | return $this->redirect()->toUrl($redirect); |
||
1025 | } |
||
1026 | |||
1027 | View Code Duplication | public function userProfileAction() |
|
1045 | |||
1046 | public function userresetAction() |
||
1066 | |||
1067 | View Code Duplication | public function ajaxforgotAction() |
|
1085 | |||
1086 | View Code Duplication | public function cmsPageAction() |
|
1105 | |||
1106 | View Code Duplication | public function cmsListAction() |
|
1125 | |||
1126 | /** |
||
1127 | * |
||
1128 | * @param \PlaygroundGame\Entity\Game $game |
||
1129 | * @param \PlaygroundUser\Entity\User $user |
||
1130 | */ |
||
1131 | public function checkFbRegistration($user, $game) |
||
1132 | { |
||
1133 | $redirect = false; |
||
1180 | |||
1181 | /** |
||
1182 | * This method create the basic Game view |
||
1183 | * @param \PlaygroundGame\Entity\Game $game |
||
1184 | */ |
||
1185 | public function buildView($game) |
||
1186 | { |
||
1187 | if ($this->getRequest()->isXmlHttpRequest()) { |
||
1188 | $viewModel = new JsonModel(); |
||
1189 | if ($game) { |
||
1190 | $view = $this->addAdditionalView($game); |
||
1191 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
1192 | $viewModel->setVariables($view->getVariables()); |
||
1193 | } |
||
1194 | } |
||
1195 | } else { |
||
1196 | $viewModel = new ViewModel(); |
||
1197 | |||
1198 | if ($game) { |
||
1199 | $this->addMetaTitle($game); |
||
1200 | $this->addMetaBitly(); |
||
1201 | $this->addGaEvent($game); |
||
1202 | |||
1203 | $this->customizeGameDesign($game); |
||
1204 | |||
1205 | $view = $this->addAdditionalView($game); |
||
1206 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
1207 | $viewModel->addChild($view, 'additional'); |
||
1208 | } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
1209 | return $view; |
||
1210 | } |
||
1211 | |||
1212 | $this->layout()->setVariables( |
||
1213 | array( |
||
1214 | 'action' => $this->params('action'), |
||
1215 | 'game' => $game, |
||
1216 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
1217 | |||
1218 | ) |
||
1219 | ); |
||
1220 | |||
1221 | $viewModel->setVariables($this->getShareData($game)); |
||
1222 | $viewModel->setVariables(array('game' => $game, 'user' => $this->user)); |
||
1223 | } |
||
1224 | } |
||
1225 | |||
1226 | return $viewModel; |
||
1227 | } |
||
1228 | |||
1229 | /** |
||
1230 | * @param \PlaygroundGame\Entity\Game $game |
||
1231 | */ |
||
1232 | public function addAdditionalView($game) |
||
1233 | { |
||
1234 | $view = false; |
||
1235 | |||
1236 | $actionName = $this->getEvent()->getRouteMatch()->getParam('action', 'not-found'); |
||
1237 | $stepsViews = json_decode($game->getStepsViews(), true); |
||
1238 | if ($stepsViews && isset($stepsViews[$actionName])) { |
||
1239 | $beforeLayout = $this->layout()->getTemplate(); |
||
1240 | $actionData = $stepsViews[$actionName]; |
||
1241 | if (is_string($actionData)) { |
||
1242 | $action = $actionData; |
||
1243 | $controller = $this->getEvent()->getRouteMatch()->getParam('controller', 'playgroundgame_game'); |
||
1244 | $view = $this->forward()->dispatch( |
||
1245 | $controller, |
||
1246 | array( |
||
1247 | 'action' => $action, |
||
1248 | 'id' => $game->getIdentifier() |
||
1249 | ) |
||
1250 | ); |
||
1251 | } elseif (is_array($actionData) && count($actionData)>0) { |
||
1252 | $action = key($actionData); |
||
1253 | $controller = $actionData[$action]; |
||
1254 | $view = $this->forward()->dispatch( |
||
1255 | $controller, |
||
1256 | array( |
||
1257 | 'action' => $action, |
||
1258 | 'id' => $game->getIdentifier() |
||
1259 | ) |
||
1260 | ); |
||
1261 | } |
||
1262 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
1263 | $this->layout()->setTemplate($beforeLayout); |
||
1264 | } |
||
1265 | |||
1266 | return $view; |
||
1267 | } |
||
1268 | |||
1269 | public function addMetaBitly() |
||
1270 | { |
||
1271 | $bitlyclient = $this->getOptions()->getBitlyUrl(); |
||
1272 | $bitlyuser = $this->getOptions()->getBitlyUsername(); |
||
1273 | $bitlykey = $this->getOptions()->getBitlyApiKey(); |
||
1274 | |||
1275 | $this->getViewHelper('HeadMeta')->setProperty('bt:client', $bitlyclient); |
||
1276 | $this->getViewHelper('HeadMeta')->setProperty('bt:user', $bitlyuser); |
||
1277 | $this->getViewHelper('HeadMeta')->setProperty('bt:key', $bitlykey); |
||
1278 | } |
||
1279 | |||
1280 | /** |
||
1281 | * @param \PlaygroundGame\Entity\Game $game |
||
1282 | */ |
||
1283 | public function addGaEvent($game) |
||
1291 | |||
1292 | /** |
||
1293 | * @param \PlaygroundGame\Entity\Game $game |
||
1294 | */ |
||
1295 | public function addMetaTitle($game) |
||
1296 | { |
||
1297 | $title = $this->translate($game->getTitle()); |
||
1298 | $this->getGameService()->getServiceManager()->get('ViewHelperManager')->get('HeadTitle')->set($title); |
||
1299 | // Meta set in the layout |
||
1300 | $this->layout()->setVariables( |
||
1301 | array( |
||
1302 | 'breadcrumbTitle' => $title, |
||
1303 | 'currentPage' => array( |
||
1304 | 'pageGames' => 'games', |
||
1305 | 'pageWinners' => '' |
||
1306 | ), |
||
1307 | 'headParams' => array( |
||
1308 | 'headTitle' => $title, |
||
1309 | 'headDescription' => $title, |
||
1310 | ), |
||
1311 | 'bodyCss' => $game->getIdentifier() |
||
1312 | ) |
||
1313 | ); |
||
1314 | } |
||
1315 | |||
1316 | /** |
||
1317 | * @param \PlaygroundGame\Entity\Game $game |
||
1318 | */ |
||
1319 | public function customizeGameDesign($game) |
||
1334 | |||
1335 | /** |
||
1336 | * @param \PlaygroundGame\Entity\Game $game |
||
1337 | */ |
||
1338 | public function getShareData($game) |
||
1339 | { |
||
1340 | $fo = $this->getServiceLocator()->get('facebook-opengraph'); |
||
1341 | $session = new Container('facebook'); |
||
1342 | |||
1343 | // I change the fbappid if i'm in fb |
||
1344 | if ($session->offsetExists('signed_request')) { |
||
1345 | $fo->setId($game->getFbAppId()); |
||
1346 | } |
||
1347 | |||
1348 | // If I want to add a share block in my view |
||
1349 | View Code Duplication | if ($game->getFbShareMessage()) { |
|
1350 | $fbShareMessage = $game->getFbShareMessage(); |
||
1351 | } else { |
||
1352 | $fbShareMessage = str_replace( |
||
1353 | '__placeholder__', |
||
1354 | $game->getTitle(), |
||
1355 | $this->getOptions()->getDefaultShareMessage() |
||
1356 | ); |
||
1357 | } |
||
1358 | |||
1359 | if ($game->getFbShareImage()) { |
||
1360 | $fbShareImage = $this->frontendUrl()->fromRoute( |
||
1361 | '', |
||
1362 | array(), |
||
1363 | array('force_canonical' => true), |
||
1364 | false |
||
1365 | ) . $game->getFbShareImage(); |
||
1366 | } else { |
||
1367 | $fbShareImage = $this->frontendUrl()->fromRoute( |
||
1368 | '', |
||
1369 | array(), |
||
1370 | array('force_canonical' => true), |
||
1371 | false |
||
1372 | ) . $game->getMainImage(); |
||
1373 | } |
||
1374 | |||
1375 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
1376 | |||
1377 | // Without bit.ly shortener |
||
1378 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
1379 | $game->getClassType(), |
||
1380 | array('id' => $game->getIdentifier()), |
||
1381 | array('force_canonical' => true) |
||
1382 | ); |
||
1383 | // With core shortener helper |
||
1384 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
1385 | |||
1386 | // FB Requests only work when it's a FB app |
||
1387 | if ($game->getFbRequestMessage()) { |
||
1388 | $fbRequestMessage = urlencode($game->getFbRequestMessage()); |
||
1389 | } else { |
||
1390 | $fbRequestMessage = str_replace( |
||
1391 | '__placeholder__', |
||
1392 | $game->getTitle(), |
||
1393 | $this->getOptions()->getDefaultShareMessage() |
||
1394 | ); |
||
1395 | } |
||
1396 | |||
1397 | View Code Duplication | if ($game->getTwShareMessage()) { |
|
1398 | $twShareMessage = $game->getTwShareMessage() . $socialLinkUrl; |
||
1399 | } else { |
||
1400 | $twShareMessage = str_replace( |
||
1401 | '__placeholder__', |
||
1402 | $game->getTitle(), |
||
1403 | $this->getOptions()->getDefaultShareMessage() |
||
1404 | ) . $socialLinkUrl; |
||
1405 | } |
||
1406 | |||
1407 | $ogTitle = new \PlaygroundCore\Opengraph\Tag('og:title', $fbShareMessage); |
||
1408 | $ogImage = new \PlaygroundCore\Opengraph\Tag('og:image', $fbShareImage); |
||
1409 | |||
1410 | $fo->addTag($ogTitle); |
||
1411 | $fo->addTag($ogImage); |
||
1412 | |||
1413 | $data = array( |
||
1414 | 'socialLinkUrl' => $socialLinkUrl, |
||
1415 | 'secretKey' => $secretKey, |
||
1416 | 'fbShareMessage' => $fbShareMessage, |
||
1417 | 'fbShareImage' => $fbShareImage, |
||
1418 | 'fbRequestMessage' => $fbRequestMessage, |
||
1419 | 'twShareMessage' => $twShareMessage, |
||
1420 | ); |
||
1421 | |||
1422 | return $data; |
||
1423 | } |
||
1424 | |||
1425 | /** |
||
1426 | * return ajax response in json format |
||
1427 | * |
||
1428 | * @param array $data |
||
1429 | * @return \Zend\View\Model\JsonModel |
||
1430 | */ |
||
1431 | View Code Duplication | protected function successJson($data = null) |
|
1432 | { |
||
1433 | $model = new JsonModel(array( |
||
1434 | 'success' => true, |
||
1435 | 'data' => $data |
||
1436 | )); |
||
1437 | return $model->setTerminal(true); |
||
1438 | } |
||
1439 | |||
1440 | /** |
||
1441 | * return ajax response in json format |
||
1442 | * |
||
1443 | * @param string $message |
||
1444 | * @return \Zend\View\Model\JsonModel |
||
1445 | */ |
||
1446 | View Code Duplication | protected function errorJson($message = null) |
|
1454 | |||
1455 | /** |
||
1456 | * @param string $helperName |
||
1457 | */ |
||
1458 | protected function getViewHelper($helperName) |
||
1462 | |||
1463 | public function getGameService() |
||
1464 | { |
||
1465 | if (!$this->gameService) { |
||
1466 | $this->gameService = $this->getServiceLocator()->get('playgroundgame_lottery_service'); |
||
1467 | } |
||
1468 | |||
1469 | return $this->gameService; |
||
1470 | } |
||
1471 | |||
1472 | public function setGameService(GameService $gameService) |
||
1473 | { |
||
1474 | $this->gameService = $gameService; |
||
1475 | |||
1476 | return $this; |
||
1477 | } |
||
1478 | |||
1479 | public function getPrizeService() |
||
1480 | { |
||
1481 | if (!$this->prizeService) { |
||
1482 | $this->prizeService = $this->getServiceLocator()->get('playgroundgame_prize_service'); |
||
1483 | } |
||
1484 | |||
1485 | return $this->prizeService; |
||
1486 | } |
||
1487 | |||
1488 | public function setPrizeService(PrizeService $prizeService) |
||
1489 | { |
||
1490 | $this->prizeService = $prizeService; |
||
1491 | |||
1492 | return $this; |
||
1493 | } |
||
1494 | |||
1495 | public function getOptions() |
||
1496 | { |
||
1497 | if (!$this->options) { |
||
1498 | $this->setOptions($this->getServiceLocator()->get('playgroundcore_module_options')); |
||
1499 | } |
||
1500 | |||
1501 | return $this->options; |
||
1502 | } |
||
1503 | |||
1504 | public function setOptions($options) |
||
1510 | } |
||
1511 |
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: