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 | 'logout', |
||
| 44 | 'ajaxforgot', |
||
| 45 | 'play', |
||
| 46 | 'result', |
||
| 47 | 'preview', |
||
| 48 | 'list' |
||
| 49 | ); |
||
| 50 | |||
| 51 | protected $withOnlineGame = array( |
||
| 52 | 'leaderboard', |
||
| 53 | 'register', |
||
| 54 | 'bounce', |
||
| 55 | 'play', |
||
| 56 | 'result' |
||
| 57 | ); |
||
| 58 | |||
| 59 | protected $withAnyUser = array( |
||
| 60 | 'share', |
||
| 61 | 'result', |
||
| 62 | 'play', |
||
| 63 | 'logout' |
||
| 64 | ); |
||
| 65 | |||
| 66 | public function setEventManager(\Zend\EventManager\EventManagerInterface $events) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Action called if matched action does not exist |
||
| 131 | * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy |
||
| 132 | * it has to be rendered in the controller. Hence the code below. |
||
| 133 | * |
||
| 134 | * This action is injected as a catchall action for each custom_games definition |
||
| 135 | * This way, when a custom_game is created, the 404 is it's responsability and the |
||
| 136 | * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml |
||
| 137 | * |
||
| 138 | * |
||
| 139 | * @return \Zend\Stdlib\ResponseInterface |
||
| 140 | */ |
||
| 141 | public function notFoundAction() |
||
| 142 | { |
||
| 143 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
| 144 | |||
| 145 | // I create a template path in which I can find a custom template |
||
| 146 | $controller = explode('\\', get_class($this)); |
||
| 147 | $controllerPath = str_replace('Controller', '', end($controller)); |
||
| 148 | $controllerPath = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '-\\1', $controllerPath)); |
||
| 149 | $template = 'playground-game/'.$controllerPath . '/custom' .$this->getRequest()->getUri()->getPath(); |
||
| 150 | |||
| 151 | if (false === $templatePathResolver->resolve($template)) { |
||
| 152 | $viewRender = $this->getServiceLocator()->get('ViewRenderer'); |
||
| 153 | |||
| 154 | $this->getEvent()->getRouteMatch()->setParam('action', 'not-found'); |
||
| 155 | $this->response->setStatusCode(404); |
||
| 156 | |||
| 157 | $res = 'error/404'; |
||
| 158 | |||
| 159 | $viewModel = $this->buildView($this->game); |
||
| 160 | $viewModel->setTemplate($res); |
||
| 161 | |||
| 162 | $this->layout()->setVariable("content", $viewRender->render($viewModel)); |
||
| 163 | $this->response->setContent($viewRender->render($this->layout())); |
||
| 164 | |||
| 165 | return $this->response; |
||
| 166 | } |
||
| 167 | |||
| 168 | $viewModel = $this->buildView($this->game); |
||
| 169 | $viewModel->setTemplate($template); |
||
| 170 | |||
| 171 | return $viewModel; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * This action acts as a hub : Depending on the first step of the game, it will forward the action to this step |
||
| 176 | */ |
||
| 177 | public function homeAction() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Homepage of the game |
||
| 212 | */ |
||
| 213 | public function indexAction() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * leaderboardAction |
||
| 232 | * |
||
| 233 | * @return ViewModel $viewModel |
||
| 234 | */ |
||
| 235 | public function leaderboardAction() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * This action has been designed to be called by other controllers |
||
| 260 | * It gives the ability to display an information form and persist it in the game entry |
||
| 261 | * |
||
| 262 | * @return \Zend\View\Model\ViewModel |
||
| 263 | */ |
||
| 264 | public function registerAction() |
||
| 265 | { |
||
| 266 | $form = $this->getGameService()->createFormFromJson($this->game->getPlayerForm()->getForm(), 'playerForm'); |
||
| 267 | |||
| 268 | if ($this->getRequest()->isPost()) { |
||
| 269 | // POST Request: Process form |
||
| 270 | $data = array_merge_recursive( |
||
| 271 | $this->getRequest()->getPost()->toArray(), |
||
| 272 | $this->getRequest()->getFiles()->toArray() |
||
| 273 | ); |
||
| 274 | |||
| 275 | $form->setData($data); |
||
| 276 | |||
| 277 | if ($form->isValid()) { |
||
| 278 | // steps of the game |
||
| 279 | $steps = $this->game->getStepsArray(); |
||
| 280 | // sub steps of the game |
||
| 281 | $viewSteps = $this->game->getStepsViewsArray(); |
||
| 282 | |||
| 283 | // register position |
||
| 284 | $key = array_search($this->params('action'), $viewSteps); |
||
| 285 | View Code Duplication | if (!$key) { |
|
| 286 | // register is not a substep of the game so it's a step |
||
| 287 | $key = array_search($this->params('action'), $steps); |
||
| 288 | $keyStep = true; |
||
| 289 | } else { |
||
| 290 | // register was a substep, i search the index of its parent |
||
| 291 | $key = array_search($key, $steps); |
||
| 292 | $keyStep = false; |
||
| 293 | } |
||
| 294 | |||
| 295 | // play position |
||
| 296 | $keyplay = array_search('play', $viewSteps); |
||
| 297 | |||
| 298 | View Code Duplication | if (!$keyplay) { |
|
| 299 | // play is not a substep, so it's a step |
||
| 300 | $keyplay = array_search('play', $steps); |
||
| 301 | $keyplayStep = true; |
||
| 302 | } else { |
||
| 303 | // play is a substep so I search the index of its parent |
||
| 304 | $keyplay = array_search($keyplay, $steps); |
||
| 305 | $keyplayStep = false; |
||
| 306 | } |
||
| 307 | |||
| 308 | // If register step before play, I don't have no entry yet. I have to create one |
||
| 309 | // If register after play step, I search for the last entry created by play step. |
||
| 310 | |||
| 311 | if ($key < $keyplay || ($keyStep && !$keyplayStep && $key <= $keyplay)) { |
||
| 312 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
| 313 | View Code Duplication | if (!$entry) { |
|
| 314 | // the user has already taken part of this game and the participation limit has been reached |
||
| 315 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
| 316 | |||
| 317 | return $this->redirect()->toUrl( |
||
| 318 | $this->frontendUrl()->fromRoute( |
||
| 319 | $this->game->getClassType().'/result', |
||
| 320 | array( |
||
| 321 | 'id' => $this->game->getIdentifier(), |
||
| 322 | |||
| 323 | ) |
||
| 324 | ) |
||
| 325 | ); |
||
| 326 | } |
||
| 327 | } else { |
||
| 328 | // I'm looking for an entry without anonymousIdentifier (the active entry in fact). |
||
| 329 | $entry = $this->getGameService()->findLastEntry($this->game, $this->user); |
||
| 330 | if ($this->getGameService()->hasReachedPlayLimit($this->game, $this->user)) { |
||
| 331 | // the user has already taken part of this game and the participation limit has been reached |
||
| 332 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
| 333 | |||
| 334 | return $this->redirect()->toUrl( |
||
| 335 | $this->frontendUrl()->fromRoute( |
||
| 336 | $this->game->getClassType().'/result', |
||
| 337 | array( |
||
| 338 | 'id' => $this->game->getIdentifier(), |
||
| 339 | |||
| 340 | ) |
||
| 341 | ) |
||
| 342 | ); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | $this->getGameService()->updateEntryPlayerForm($form->getData(), $this->game, $this->user, $entry); |
||
| 347 | |||
| 348 | if (!empty($this->game->nextStep($this->params('action')))) { |
||
| 349 | return $this->redirect()->toUrl( |
||
| 350 | $this->frontendUrl()->fromRoute( |
||
| 351 | $this->game->getClassType() .'/' . $this->game->nextStep($this->params('action')), |
||
| 352 | array('id' => $this->game->getIdentifier()), |
||
| 353 | array('force_canonical' => true) |
||
| 354 | ) |
||
| 355 | ); |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | $viewModel = $this->buildView($this->game); |
||
| 361 | $viewModel->setVariables(array( |
||
| 362 | 'form' => $form |
||
| 363 | )); |
||
| 364 | |||
| 365 | return $viewModel; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * This action takes care of the terms of the game |
||
| 370 | */ |
||
| 371 | public function termsAction() |
||
| 372 | { |
||
| 373 | $viewModel = $this->buildView($this->game); |
||
| 374 | |||
| 375 | return $viewModel; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * This action takes care of the conditions of the game |
||
| 380 | */ |
||
| 381 | public function conditionsAction() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * This action takes care of bounce page of the game |
||
| 390 | */ |
||
| 391 | public function bounceAction() |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * This action displays the Prizes page associated to the game |
||
| 414 | */ |
||
| 415 | public function prizesAction() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * This action displays a specific Prize page among those associated to the game |
||
| 428 | */ |
||
| 429 | public function prizeAction() |
||
| 443 | |||
| 444 | public function gameslistAction() |
||
| 491 | |||
| 492 | public function fangateAction() |
||
| 498 | |||
| 499 | public function shareAction() |
||
| 500 | { |
||
| 501 | $statusMail = null; |
||
| 502 | $lastEntry = null; |
||
| 503 | |||
| 504 | // steps of the game |
||
| 505 | $steps = $this->game->getStepsArray(); |
||
| 506 | // sub steps of the game |
||
| 507 | $viewSteps = $this->game->getStepsViewsArray(); |
||
| 508 | |||
| 509 | // share position |
||
| 510 | $key = array_search($this->params('action'), $viewSteps); |
||
| 511 | View Code Duplication | if (!$key) { |
|
| 512 | // share is not a substep of the game so it's a step |
||
| 513 | $key = array_search($this->params('action'), $steps); |
||
| 514 | $keyStep = true; |
||
| 515 | } else { |
||
| 516 | // share was a substep, I search the index of its parent |
||
| 517 | $key = array_search($key, $steps); |
||
| 518 | $keyStep = false; |
||
| 519 | } |
||
| 520 | |||
| 521 | // play position |
||
| 522 | $keyplay = array_search('play', $viewSteps); |
||
| 523 | |||
| 524 | View Code Duplication | if (!$keyplay) { |
|
| 525 | // play is not a substep, so it's a step |
||
| 526 | $keyplay = array_search('play', $steps); |
||
| 527 | $keyplayStep = true; |
||
| 528 | } else { |
||
| 529 | // play is a substep so I search the index of its parent |
||
| 530 | $keyplay = array_search($keyplay, $steps); |
||
| 531 | $keyplayStep = false; |
||
| 532 | } |
||
| 533 | |||
| 534 | if ($key && $keyplay && $keyplay <= $key) { |
||
| 535 | // Has the user finished the game ? |
||
| 536 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
| 537 | |||
| 538 | if ($lastEntry === null) { |
||
| 539 | return $this->redirect()->toUrl( |
||
| 540 | $this->frontendUrl()->fromRoute( |
||
| 541 | $this->game->getClassType(), |
||
| 542 | array('id' => $this->game->getIdentifier()) |
||
| 543 | ) |
||
| 544 | ); |
||
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 549 | $form->setAttribute('method', 'post'); |
||
| 550 | |||
| 551 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
| 552 | $viewModel = $this->buildView($this->game); |
||
| 553 | |||
| 554 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
| 555 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 556 | $form->setData($data); |
||
| 557 | if ($form->isValid()) { |
||
| 558 | $result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
||
| 559 | if ($result) { |
||
| 560 | $statusMail = true; |
||
| 561 | } |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | $viewModel->setVariables(array( |
||
| 566 | 'statusMail' => $statusMail, |
||
| 567 | 'form' => $form, |
||
| 568 | )); |
||
| 569 | |||
| 570 | return $viewModel; |
||
| 571 | } |
||
| 572 | |||
| 573 | View Code Duplication | public function fbshareAction() |
|
| 593 | |||
| 594 | public function fbrequestAction() |
||
| 616 | |||
| 617 | public function tweetAction() |
||
| 636 | |||
| 637 | View Code Duplication | public function googleAction() |
|
| 658 | |||
| 659 | public function optinAction() |
||
| 677 | |||
| 678 | public function loginAction() |
||
| 679 | { |
||
| 680 | $request = $this->getRequest(); |
||
| 681 | $form = $this->getServiceLocator()->get('zfcuser_login_form'); |
||
| 682 | |||
| 683 | if ($request->isPost()) { |
||
| 684 | $form->setData($request->getPost()); |
||
| 685 | |||
| 686 | if (!$form->isValid()) { |
||
| 687 | $this->flashMessenger()->addMessage( |
||
| 688 | 'Authentication failed. Please try again.' |
||
| 689 | ); |
||
| 690 | |||
| 691 | $viewModel = $this->buildView($this->game); |
||
| 692 | $viewModel->setVariables(array( |
||
| 693 | 'form' => $form, |
||
| 694 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 695 | )); |
||
| 696 | |||
| 697 | return $viewModel; |
||
| 698 | } |
||
| 699 | |||
| 700 | // clear adapters |
||
| 701 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
| 702 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
| 703 | |||
| 704 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
| 705 | |||
| 706 | View Code Duplication | if (!$logged) { |
|
| 707 | $this->flashMessenger()->addMessage( |
||
| 708 | 'Authentication failed. Please try again.' |
||
| 709 | ); |
||
| 710 | |||
| 711 | return $this->redirect()->toUrl( |
||
| 712 | $this->frontendUrl()->fromRoute( |
||
| 713 | $this->game->getClassType() . '/login', |
||
| 714 | array('id' => $this->game->getIdentifier()) |
||
| 715 | ) |
||
| 716 | ); |
||
| 717 | } else { |
||
| 718 | return $this->redirect()->toUrl( |
||
| 719 | $this->frontendUrl()->fromRoute( |
||
| 720 | $this->game->getClassType() . '/index', |
||
| 721 | array('id' => $this->game->getIdentifier()) |
||
| 722 | ) |
||
| 723 | ); |
||
| 724 | } |
||
| 725 | } |
||
| 726 | |||
| 727 | $form->setAttribute( |
||
| 728 | 'action', |
||
| 729 | $this->frontendUrl()->fromRoute( |
||
| 730 | $this->game->getClassType().'/login', |
||
| 731 | array('id' => $this->game->getIdentifier()) |
||
| 732 | ) |
||
| 733 | ); |
||
| 734 | $viewModel = $this->buildView($this->game); |
||
| 735 | $viewModel->setVariables(array( |
||
| 736 | 'form' => $form, |
||
| 737 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 738 | )); |
||
| 739 | return $viewModel; |
||
| 740 | } |
||
| 741 | |||
| 742 | View Code Duplication | public function logoutAction() |
|
| 760 | |||
| 761 | public function userregisterAction() |
||
| 762 | { |
||
| 763 | $userOptions = $this->getServiceLocator()->get('zfcuser_module_options'); |
||
| 764 | |||
| 765 | if ($this->zfcUserAuthentication()->hasIdentity()) { |
||
| 766 | return $this->redirect()->toUrl( |
||
| 767 | $this->frontendUrl()->fromRoute( |
||
| 768 | $this->game->getClassType().'/'.$this->game->nextStep('index'), |
||
| 769 | array('id' => $this->game->getIdentifier()) |
||
| 770 | ) |
||
| 771 | ); |
||
| 772 | } |
||
| 773 | $request = $this->getRequest(); |
||
| 774 | $service = $this->getServiceLocator()->get('zfcuser_user_service'); |
||
| 775 | $form = $this->getServiceLocator()->get('playgroundgame_register_form'); |
||
| 776 | $socialnetwork = $this->params()->fromRoute('socialnetwork', false); |
||
| 777 | $form->setAttribute( |
||
| 778 | 'action', |
||
| 779 | $this->frontendUrl()->fromRoute( |
||
| 780 | $this->game->getClassType().'/user-register', |
||
| 781 | array('id' => $this->game->getIdentifier()) |
||
| 782 | ) |
||
| 783 | ); |
||
| 784 | $params = array(); |
||
| 785 | $socialCredentials = array(); |
||
| 786 | |||
| 787 | if ($userOptions->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) { |
||
| 788 | $redirect = $request->getQuery()->get('redirect'); |
||
| 789 | } else { |
||
| 790 | $redirect = false; |
||
| 791 | } |
||
| 792 | |||
| 793 | if ($socialnetwork) { |
||
| 794 | $infoMe = $this->getProviderService()->getInfoMe($socialnetwork); |
||
| 795 | |||
| 796 | if (!empty($infoMe)) { |
||
| 797 | $user = $this->getProviderService()->getUserProviderMapper()->findUserByProviderId( |
||
| 798 | $infoMe->identifier, |
||
| 799 | $socialnetwork |
||
| 800 | ); |
||
| 801 | |||
| 802 | if ($user || $service->getOptions()->getCreateUserAutoSocial() === true) { |
||
| 803 | //on le dirige vers l'action d'authentification |
||
| 804 | if (! $redirect && $userOptions->getLoginRedirectRoute() != '') { |
||
| 805 | $redirect = $this->frontendUrl()->fromRoute( |
||
| 806 | $this->game->getClassType().'/login', |
||
| 807 | array('id' => $this->game->getIdentifier()) |
||
| 808 | ); |
||
| 809 | } |
||
| 810 | $redir = $this->frontendUrl()->fromRoute( |
||
| 811 | $this->game->getClassType().'/login', |
||
| 812 | array('id' => $this->game->getIdentifier()) |
||
| 813 | ) .'/' . $socialnetwork . ($redirect ? '?redirect=' . $redirect : ''); |
||
| 814 | |||
| 815 | return $this->redirect()->toUrl($redir); |
||
| 816 | } |
||
| 817 | |||
| 818 | // Je retire la saisie du login/mdp |
||
| 819 | $form->setAttribute( |
||
| 820 | 'action', |
||
| 821 | $this->frontendUrl()->fromRoute( |
||
| 822 | $this->game->getClassType().'/user-register', |
||
| 823 | array( |
||
| 824 | 'id' => $this->game->getIdentifier(), |
||
| 825 | 'socialnetwork' => $socialnetwork, |
||
| 826 | |||
| 827 | ) |
||
| 828 | ) |
||
| 829 | ); |
||
| 830 | $form->remove('password'); |
||
| 831 | $form->remove('passwordVerify'); |
||
| 832 | |||
| 833 | $birthMonth = $infoMe->birthMonth; |
||
| 834 | if (strlen($birthMonth) <= 1) { |
||
| 835 | $birthMonth = '0'.$birthMonth; |
||
| 836 | } |
||
| 837 | $birthDay = $infoMe->birthDay; |
||
| 838 | if (strlen($birthDay) <= 1) { |
||
| 839 | $birthDay = '0'.$birthDay; |
||
| 840 | } |
||
| 841 | |||
| 842 | $gender = $infoMe->gender; |
||
| 843 | if ($gender == 'female') { |
||
| 844 | $title = 'Me'; |
||
| 845 | } else { |
||
| 846 | $title = 'M'; |
||
| 847 | } |
||
| 848 | |||
| 849 | $params = array( |
||
| 850 | //'birth_year' => $infoMe->birthYear, |
||
| 851 | 'title' => $title, |
||
| 852 | 'dob' => $birthDay.'/'.$birthMonth.'/'.$infoMe->birthYear, |
||
| 853 | 'firstname' => $infoMe->firstName, |
||
| 854 | 'lastname' => $infoMe->lastName, |
||
| 855 | 'email' => $infoMe->email, |
||
| 856 | 'postalCode' => $infoMe->zip, |
||
| 857 | ); |
||
| 858 | $socialCredentials = array( |
||
| 859 | 'socialNetwork' => strtolower($socialnetwork), |
||
| 860 | 'socialId' => $infoMe->identifier, |
||
| 861 | ); |
||
| 862 | } |
||
| 863 | } |
||
| 864 | |||
| 865 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
| 866 | $this->game->getClassType().'/user-register', |
||
| 867 | array('id' => $this->game->getIdentifier()) |
||
| 868 | ) .($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
| 869 | $prg = $this->prg($redirectUrl, true); |
||
| 870 | |||
| 871 | if ($prg instanceof Response) { |
||
| 872 | return $prg; |
||
| 873 | } elseif ($prg === false) { |
||
| 874 | $form->setData($params); |
||
| 875 | $viewModel = $this->buildView($this->game); |
||
| 876 | $viewModel->setVariables(array( |
||
| 877 | 'registerForm' => $form, |
||
| 878 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
| 879 | 'redirect' => $redirect, |
||
| 880 | )); |
||
| 881 | return $viewModel; |
||
| 882 | } |
||
| 883 | |||
| 884 | $post = $prg; |
||
| 885 | $post = array_merge( |
||
| 886 | $post, |
||
| 887 | $socialCredentials |
||
| 888 | ); |
||
| 889 | |||
| 890 | if ($this->game->getOnInvitation()) { |
||
| 891 | $credential = trim( |
||
| 892 | $post[$this->getGameService()->getOptions()->getOnInvitationField()] |
||
| 893 | ); |
||
| 894 | if (!$credential) { |
||
| 895 | $credential = $this->params()->fromQuery( |
||
| 896 | $this->getGameService()->getOptions()->getOnInvitationField() |
||
| 897 | ); |
||
| 898 | } |
||
| 899 | $found = $this->getGameService()->getInvitationMapper()->findOneBy(array('requestKey'=>$credential)); |
||
| 900 | |||
| 901 | if (!$found || !empty($found->getUser())) { |
||
| 902 | $this->flashMessenger()->addMessage( |
||
| 903 | 'Authentication failed. Please try again.' |
||
| 904 | ); |
||
| 905 | $form->setData($post); |
||
| 906 | $viewModel = $this->buildView($this->game); |
||
| 907 | $viewModel->setVariables(array( |
||
| 908 | 'registerForm' => $form, |
||
| 909 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
| 910 | 'redirect' => $redirect, |
||
| 911 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 912 | 'flashErrors' => $this->flashMessenger()->getErrorMessages(), |
||
| 913 | )); |
||
| 914 | |||
| 915 | return $viewModel; |
||
| 916 | } |
||
| 917 | } |
||
| 918 | |||
| 919 | $user = $service->register($post, 'playgroundgame_register_form'); |
||
| 920 | |||
| 921 | if (! $user) { |
||
| 922 | $viewModel = $this->buildView($this->game); |
||
| 923 | $viewModel->setVariables(array( |
||
| 924 | 'registerForm' => $form, |
||
| 925 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
| 926 | 'redirect' => $redirect, |
||
| 927 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 928 | 'flashErrors' => $this->flashMessenger()->getErrorMessages(), |
||
| 929 | )); |
||
| 930 | |||
| 931 | return $viewModel; |
||
| 932 | } |
||
| 933 | |||
| 934 | if ($this->game->getOnInvitation()) { |
||
| 935 | // user has been created, associate the code with the userId |
||
| 936 | $found->setUser($user); |
||
| 937 | $this->getGameService()->getInvitationMapper()->update($found); |
||
| 938 | } |
||
| 939 | |||
| 940 | if ($service->getOptions()->getEmailVerification()) { |
||
| 941 | $vm = new ViewModel(array('userEmail' => $user->getEmail())); |
||
| 942 | $vm->setTemplate('playground-user/register/registermail'); |
||
| 943 | |||
| 944 | return $vm; |
||
| 945 | } elseif ($service->getOptions()->getLoginAfterRegistration()) { |
||
| 946 | $identityFields = $service->getOptions()->getAuthIdentityFields(); |
||
| 947 | if (in_array('email', $identityFields)) { |
||
| 948 | $post['identity'] = $user->getEmail(); |
||
| 949 | } elseif (in_array('username', $identityFields)) { |
||
| 950 | $post['identity'] = $user->getUsername(); |
||
| 951 | } |
||
| 952 | $post['credential'] = isset($post['password'])?$post['password']:''; |
||
| 953 | $request->setPost(new Parameters($post)); |
||
| 954 | |||
| 955 | // clear adapters |
||
| 956 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
| 957 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
| 958 | |||
| 959 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
| 960 | |||
| 961 | View Code Duplication | if ($logged) { |
|
| 962 | return $this->redirect()->toUrl( |
||
| 963 | $this->frontendUrl()->fromRoute( |
||
| 964 | $this->game->getClassType(), |
||
| 965 | array('id' => $this->game->getIdentifier()) |
||
| 966 | ) |
||
| 967 | ); |
||
| 968 | } else { |
||
| 969 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( |
||
| 970 | 'Authentication failed. Please try again.' |
||
| 971 | ); |
||
| 972 | return $this->redirect()->toUrl( |
||
| 973 | $this->frontendUrl()->fromRoute( |
||
| 974 | $this->game->getClassType() . '/login', |
||
| 975 | array('id' => $this->game->getIdentifier()) |
||
| 976 | ) |
||
| 977 | ); |
||
| 978 | } |
||
| 979 | } |
||
| 980 | |||
| 981 | $redirect = $this->frontendUrl()->fromRoute( |
||
| 982 | $this->game->getClassType().'/login', |
||
| 983 | array('id' => $this->game->getIdentifier()) |
||
| 984 | ) . ($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
| 985 | |||
| 986 | return $this->redirect()->toUrl($redirect); |
||
| 987 | } |
||
| 988 | |||
| 989 | View Code Duplication | public function userProfileAction() |
|
| 1007 | |||
| 1008 | public function userresetAction() |
||
| 1028 | |||
| 1029 | View Code Duplication | public function ajaxforgotAction() |
|
| 1047 | |||
| 1048 | View Code Duplication | public function cmsPageAction() |
|
| 1067 | |||
| 1068 | View Code Duplication | public function cmsListAction() |
|
| 1087 | |||
| 1088 | /** |
||
| 1089 | * |
||
| 1090 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1091 | * @param \PlaygroundUser\Entity\User $user |
||
| 1092 | */ |
||
| 1093 | public function checkFbRegistration($user, $game) |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * This method create the basic Game view |
||
| 1145 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1146 | */ |
||
| 1147 | public function buildView($game) |
||
| 1148 | { |
||
| 1149 | if ($this->getRequest()->isXmlHttpRequest()) { |
||
| 1150 | $viewModel = new JsonModel(); |
||
| 1151 | if ($game) { |
||
| 1152 | $view = $this->addAdditionalView($game); |
||
| 1153 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
| 1154 | $viewModel->setVariables($view->getVariables()); |
||
| 1155 | } |
||
| 1156 | } |
||
| 1157 | } else { |
||
| 1158 | $viewModel = new ViewModel(); |
||
| 1159 | |||
| 1160 | if ($game) { |
||
| 1161 | $this->addMetaTitle($game); |
||
| 1162 | $this->addMetaBitly(); |
||
| 1163 | $this->addGaEvent($game); |
||
| 1164 | |||
| 1165 | $this->customizeGameDesign($game); |
||
| 1166 | |||
| 1167 | $view = $this->addAdditionalView($game); |
||
| 1168 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
| 1169 | $viewModel->addChild($view, 'additional'); |
||
| 1170 | } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
| 1171 | return $view; |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | $this->layout()->setVariables( |
||
| 1175 | array( |
||
| 1176 | 'action' => $this->params('action'), |
||
| 1177 | 'game' => $game, |
||
| 1178 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 1179 | |||
| 1180 | ) |
||
| 1181 | ); |
||
| 1182 | } |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | if ($game) { |
||
| 1186 | $viewModel->setVariables($this->getShareData($game)); |
||
| 1187 | $viewModel->setVariables(array('game' => $game, 'user' => $this->user)); |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | return $viewModel; |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1195 | */ |
||
| 1196 | public function addAdditionalView($game) |
||
| 1197 | { |
||
| 1198 | $view = false; |
||
| 1199 | |||
| 1200 | $actionName = $this->getEvent()->getRouteMatch()->getParam('action', 'not-found'); |
||
| 1201 | $stepsViews = json_decode($game->getStepsViews(), true); |
||
| 1202 | if ($stepsViews && isset($stepsViews[$actionName])) { |
||
| 1203 | $beforeLayout = $this->layout()->getTemplate(); |
||
| 1204 | $actionData = $stepsViews[$actionName]; |
||
| 1205 | if (is_string($actionData)) { |
||
| 1206 | $action = $actionData; |
||
| 1207 | $controller = $this->getEvent()->getRouteMatch()->getParam('controller', 'playgroundgame_game'); |
||
| 1208 | $view = $this->forward()->dispatch( |
||
| 1209 | $controller, |
||
| 1210 | array( |
||
| 1211 | 'action' => $action, |
||
| 1212 | 'id' => $game->getIdentifier() |
||
| 1213 | ) |
||
| 1214 | ); |
||
| 1215 | } elseif (is_array($actionData) && count($actionData)>0) { |
||
| 1216 | $action = key($actionData); |
||
| 1217 | $controller = $actionData[$action]; |
||
| 1218 | $view = $this->forward()->dispatch( |
||
| 1219 | $controller, |
||
| 1220 | array( |
||
| 1221 | 'action' => $action, |
||
| 1222 | 'id' => $game->getIdentifier() |
||
| 1223 | ) |
||
| 1224 | ); |
||
| 1225 | } |
||
| 1226 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
| 1227 | $this->layout()->setTemplate($beforeLayout); |
||
| 1228 | } |
||
| 1229 | |||
| 1230 | return $view; |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | public function addMetaBitly() |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1246 | */ |
||
| 1247 | public function addGaEvent($game) |
||
| 1255 | |||
| 1256 | /** |
||
| 1257 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1258 | */ |
||
| 1259 | public function addMetaTitle($game) |
||
| 1260 | { |
||
| 1261 | $title = $this->translate($game->getTitle()); |
||
| 1262 | $this->getGameService()->getServiceManager()->get('ViewHelperManager')->get('HeadTitle')->set($title); |
||
| 1263 | // Meta set in the layout |
||
| 1264 | $this->layout()->setVariables( |
||
| 1265 | array( |
||
| 1266 | 'breadcrumbTitle' => $title, |
||
| 1267 | 'currentPage' => array( |
||
| 1268 | 'pageGames' => 'games', |
||
| 1269 | 'pageWinners' => '' |
||
| 1270 | ), |
||
| 1271 | 'headParams' => array( |
||
| 1272 | 'headTitle' => $title, |
||
| 1273 | 'headDescription' => $title, |
||
| 1274 | ), |
||
| 1275 | 'bodyCss' => $game->getIdentifier() |
||
| 1276 | ) |
||
| 1277 | ); |
||
| 1278 | } |
||
| 1279 | |||
| 1280 | /** |
||
| 1281 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1282 | */ |
||
| 1283 | public function customizeGameDesign($game) |
||
| 1298 | |||
| 1299 | /** |
||
| 1300 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1301 | */ |
||
| 1302 | public function getShareData($game) |
||
| 1388 | |||
| 1389 | /** |
||
| 1390 | * return ajax response in json format |
||
| 1391 | * |
||
| 1392 | * @param array $data |
||
| 1393 | * @return \Zend\View\Model\JsonModel |
||
| 1394 | */ |
||
| 1395 | View Code Duplication | protected function successJson($data = null) |
|
| 1403 | |||
| 1404 | /** |
||
| 1405 | * return ajax response in json format |
||
| 1406 | * |
||
| 1407 | * @param string $message |
||
| 1408 | * @return \Zend\View\Model\JsonModel |
||
| 1409 | */ |
||
| 1410 | View Code Duplication | protected function errorJson($message = null) |
|
| 1418 | |||
| 1419 | /** |
||
| 1420 | * @param string $helperName |
||
| 1421 | */ |
||
| 1422 | protected function getViewHelper($helperName) |
||
| 1426 | |||
| 1427 | public function getGameService() |
||
| 1435 | |||
| 1436 | public function setGameService(GameService $gameService) |
||
| 1442 | |||
| 1443 | public function getPrizeService() |
||
| 1451 | |||
| 1452 | public function setPrizeService(PrizeService $prizeService) |
||
| 1458 | |||
| 1459 | public function getOptions() |
||
| 1467 | |||
| 1468 | public function setOptions($options) |
||
| 1474 | } |
||
| 1475 |
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: