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) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Action called if matched action does not exist |
||
| 120 | * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy |
||
| 121 | * it has to be rendered in the controller. Hence the code below. |
||
| 122 | * |
||
| 123 | * This action is injected as a catchall action for each custom_games definition |
||
| 124 | * This way, when a custom_game is created, the 404 is it's responsability and the |
||
| 125 | * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml |
||
| 126 | * |
||
| 127 | * |
||
| 128 | * @return \Zend\Stdlib\ResponseInterface |
||
| 129 | */ |
||
| 130 | public function notFoundAction() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * This action acts as a hub : Depending on the first step of the game, it will forward the action to this step |
||
| 150 | */ |
||
| 151 | public function homeAction() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Homepage of the game |
||
| 186 | */ |
||
| 187 | public function indexAction() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * leaderboardAction |
||
| 206 | * |
||
| 207 | * @return ViewModel $viewModel |
||
| 208 | */ |
||
| 209 | public function leaderboardAction() |
||
| 210 | { |
||
| 211 | $filter = $this->getEvent()->getRouteMatch()->getParam('filter'); |
||
| 212 | $p = $this->getEvent()->getRouteMatch()->getParam('p'); |
||
| 213 | |||
| 214 | $beforeLayout = $this->layout()->getTemplate(); |
||
| 215 | $subViewModel = $this->forward()->dispatch( |
||
| 216 | 'playgroundreward', |
||
| 217 | array('action' => 'leaderboard', 'filter' => $filter, 'p' => $p) |
||
| 218 | ); |
||
| 219 | |||
| 220 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
| 221 | $this->layout()->setTemplate($beforeLayout); |
||
| 222 | |||
| 223 | // give the ability to the game to have its customized look and feel. |
||
| 224 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
| 225 | $l = $templatePathResolver->getPaths(); |
||
| 226 | |||
| 227 | $templatePathResolver->addPath($l[0].'custom/'.$this->game->getIdentifier()); |
||
| 228 | |||
| 229 | return $subViewModel; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * This action has been designed to be called by other controllers |
||
| 234 | * It gives the ability to display an information form and persist it in the game entry |
||
| 235 | * |
||
| 236 | * @return \Zend\View\Model\ViewModel |
||
| 237 | */ |
||
| 238 | public function registerAction() |
||
| 239 | { |
||
| 240 | $form = $this->getGameService()->createFormFromJson($this->game->getPlayerForm()->getForm(), 'playerForm'); |
||
| 241 | |||
| 242 | if ($this->getRequest()->isPost()) { |
||
| 243 | // POST Request: Process form |
||
| 244 | $data = array_merge_recursive( |
||
| 245 | $this->getRequest()->getPost()->toArray(), |
||
| 246 | $this->getRequest()->getFiles()->toArray() |
||
| 247 | ); |
||
| 248 | |||
| 249 | $form->setData($data); |
||
| 250 | |||
| 251 | if ($form->isValid()) { |
||
| 252 | // steps of the game |
||
| 253 | $steps = $this->game->getStepsArray(); |
||
| 254 | // sub steps of the game |
||
| 255 | $viewSteps = $this->game->getStepsViewsArray(); |
||
| 256 | |||
| 257 | // register position |
||
| 258 | $key = array_search($this->params('action'), $viewSteps); |
||
| 259 | View Code Duplication | if (!$key) { |
|
| 260 | // register is not a substep of the game so it's a step |
||
| 261 | $key = array_search($this->params('action'), $steps); |
||
| 262 | $keyStep = true; |
||
| 263 | } else { |
||
| 264 | // register was a substep, i search the index of its parent |
||
| 265 | $key = array_search($key, $steps); |
||
| 266 | $keyStep = false; |
||
| 267 | } |
||
| 268 | |||
| 269 | // play position |
||
| 270 | $keyplay = array_search('play', $viewSteps); |
||
| 271 | |||
| 272 | View Code Duplication | if (!$keyplay) { |
|
| 273 | // play is not a substep, so it's a step |
||
| 274 | $keyplay = array_search('play', $steps); |
||
| 275 | $keyplayStep = true; |
||
| 276 | } else { |
||
| 277 | // play is a substep so I search the index of its parent |
||
| 278 | $keyplay = array_search($keyplay, $steps); |
||
| 279 | $keyplayStep = false; |
||
| 280 | } |
||
| 281 | |||
| 282 | // If register step before play, I don't have no entry yet. I have to create one |
||
| 283 | // If register after play step, I search for the last entry created by play step. |
||
| 284 | |||
| 285 | if ($key < $keyplay || ($keyStep && !$keyplayStep && $key <= $keyplay)) { |
||
| 286 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
| 287 | if (!$entry) { |
||
| 288 | // the user has already taken part of this game and the participation limit has been reached |
||
| 289 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
| 290 | |||
| 291 | return $this->redirect()->toUrl( |
||
| 292 | $this->frontendUrl()->fromRoute( |
||
| 293 | $this->game->getClassType().'/result', |
||
| 294 | array( |
||
| 295 | 'id' => $this->game->getIdentifier(), |
||
| 296 | |||
| 297 | ) |
||
| 298 | ) |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | } else { |
||
| 302 | // I'm looking for an entry without anonymousIdentifier (the active entry in fact). |
||
| 303 | $entry = $this->getGameService()->findLastEntry($this->game, $this->user); |
||
| 304 | if ($this->getGameService()->hasReachedPlayLimit($this->game, $this->user)) { |
||
| 305 | // the user has already taken part of this game and the participation limit has been reached |
||
| 306 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
| 307 | |||
| 308 | return $this->redirect()->toUrl( |
||
| 309 | $this->frontendUrl()->fromRoute( |
||
| 310 | $this->game->getClassType().'/result', |
||
| 311 | array( |
||
| 312 | 'id' => $this->game->getIdentifier(), |
||
| 313 | |||
| 314 | ) |
||
| 315 | ) |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | $this->getGameService()->updateEntryPlayerForm($form->getData(), $this->game, $this->user, $entry); |
||
| 321 | |||
| 322 | if (!empty($this->game->nextStep($this->params('action')))) { |
||
| 323 | return $this->redirect()->toUrl( |
||
| 324 | $this->frontendUrl()->fromRoute( |
||
| 325 | $this->game->getClassType() .'/' . $this->game->nextStep($this->params('action')), |
||
| 326 | array('id' => $this->game->getIdentifier()), |
||
| 327 | array('force_canonical' => true) |
||
| 328 | ) |
||
| 329 | ); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | $viewModel = $this->buildView($this->game); |
||
| 335 | $viewModel->setVariables(array( |
||
| 336 | 'form' => $form |
||
| 337 | )); |
||
| 338 | |||
| 339 | return $viewModel; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * This action takes care of the terms of the game |
||
| 344 | */ |
||
| 345 | public function termsAction() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * This action takes care of the conditions of the game |
||
| 354 | */ |
||
| 355 | public function conditionsAction() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * This action takes care of bounce page of the game |
||
| 364 | */ |
||
| 365 | public function bounceAction() |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * This action displays the Prizes page associated to the game |
||
| 388 | */ |
||
| 389 | public function prizesAction() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * This action displays a specific Prize page among those associated to the game |
||
| 402 | */ |
||
| 403 | public function prizeAction() |
||
| 417 | |||
| 418 | public function gameslistAction() |
||
| 465 | |||
| 466 | public function fangateAction() |
||
| 472 | |||
| 473 | public function shareAction() |
||
| 474 | { |
||
| 475 | $statusMail = null; |
||
| 476 | $lastEntry = null; |
||
| 477 | |||
| 478 | // steps of the game |
||
| 479 | $steps = $this->game->getStepsArray(); |
||
| 480 | // sub steps of the game |
||
| 481 | $viewSteps = $this->game->getStepsViewsArray(); |
||
| 482 | |||
| 483 | // share position |
||
| 484 | $key = array_search($this->params('action'), $viewSteps); |
||
| 485 | View Code Duplication | if (!$key) { |
|
| 486 | // share is not a substep of the game so it's a step |
||
| 487 | $key = array_search($this->params('action'), $steps); |
||
| 488 | $keyStep = true; |
||
| 489 | } else { |
||
| 490 | // share was a substep, I search the index of its parent |
||
| 491 | $key = array_search($key, $steps); |
||
| 492 | $keyStep = false; |
||
| 493 | } |
||
| 494 | |||
| 495 | // play position |
||
| 496 | $keyplay = array_search('play', $viewSteps); |
||
| 497 | |||
| 498 | View Code Duplication | if (!$keyplay) { |
|
| 499 | // play is not a substep, so it's a step |
||
| 500 | $keyplay = array_search('play', $steps); |
||
| 501 | $keyplayStep = true; |
||
| 502 | } else { |
||
| 503 | // play is a substep so I search the index of its parent |
||
| 504 | $keyplay = array_search($keyplay, $steps); |
||
| 505 | $keyplayStep = false; |
||
| 506 | } |
||
| 507 | |||
| 508 | if ($key && $keyplay && $keyplay <= $key) { |
||
| 509 | // Has the user finished the game ? |
||
| 510 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
| 511 | |||
| 512 | View Code Duplication | if ($lastEntry === null) { |
|
| 513 | return $this->redirect()->toUrl( |
||
| 514 | $this->frontendUrl()->fromRoute( |
||
| 515 | $this->game->getClassType(), |
||
| 516 | array('id' => $this->game->getIdentifier()) |
||
| 517 | ) |
||
| 518 | ); |
||
| 519 | } |
||
| 520 | } |
||
| 521 | |||
| 522 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 523 | $form->setAttribute('method', 'post'); |
||
| 524 | |||
| 525 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
| 526 | $viewModel = $this->buildView($this->game); |
||
| 527 | |||
| 528 | if ($this->getRequest()->isPost()) { |
||
| 529 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 530 | $form->setData($data); |
||
| 531 | View Code Duplication | if ($form->isValid()) { |
|
| 532 | $result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
||
| 533 | if ($result) { |
||
| 534 | $statusMail = true; |
||
| 535 | } |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | $viewModel->setVariables(array( |
||
| 540 | 'statusMail' => $statusMail, |
||
| 541 | 'form' => $form, |
||
| 542 | )); |
||
| 543 | |||
| 544 | return $viewModel; |
||
| 545 | } |
||
| 546 | |||
| 547 | View Code Duplication | public function fbshareAction() |
|
| 567 | |||
| 568 | public function fbrequestAction() |
||
| 590 | |||
| 591 | public function tweetAction() |
||
| 610 | |||
| 611 | View Code Duplication | public function googleAction() |
|
| 632 | |||
| 633 | public function optinAction() |
||
| 634 | { |
||
| 635 | $userService = $this->getServiceLocator()->get('zfcuser_user_service'); |
||
| 636 | |||
| 637 | if ($this->getRequest()->isPost()) { |
||
| 638 | $data['optin'] = ($this->params()->fromPost('optin'))? 1:0; |
||
| 639 | $data['optinPartner'] = ($this->params()->fromPost('optinPartner'))? 1:0; |
||
| 640 | |||
| 641 | $userService->updateNewsletter($data); |
||
| 642 | } |
||
| 643 | |||
| 644 | return $this->redirect()->toUrl( |
||
| 645 | $this->frontendUrl()->fromRoute( |
||
| 646 | 'frontend/' . $this->game->getClassType() . '/index', |
||
| 647 | array('id' => $this->game->getIdentifier()) |
||
| 648 | ) |
||
| 649 | ); |
||
| 650 | } |
||
| 651 | |||
| 652 | public function loginAction() |
||
| 653 | { |
||
| 654 | $request = $this->getRequest(); |
||
| 655 | $form = $this->getServiceLocator()->get('zfcuser_login_form'); |
||
| 656 | |||
| 657 | if ($request->isPost()) { |
||
| 658 | $form->setData($request->getPost()); |
||
| 659 | |||
| 660 | if (!$form->isValid()) { |
||
| 661 | $this->flashMessenger()->addMessage( |
||
| 662 | 'Authentication failed. Please try again.' |
||
| 663 | ); |
||
| 664 | |||
| 665 | $viewModel = $this->buildView($this->game); |
||
| 666 | $viewModel->setVariables(array( |
||
| 667 | 'form' => $form, |
||
| 668 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 669 | )); |
||
| 670 | |||
| 671 | return $viewModel; |
||
| 672 | } |
||
| 673 | |||
| 674 | // clear adapters |
||
| 675 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
| 676 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
| 677 | |||
| 678 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
| 679 | |||
| 680 | View Code Duplication | if (!$logged) { |
|
| 681 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( |
||
| 682 | 'Authentication failed. Please try again.' |
||
| 683 | ); |
||
| 684 | |||
| 685 | return $this->redirect()->toUrl( |
||
| 686 | $this->frontendUrl()->fromRoute( |
||
| 687 | $this->game->getClassType() . '/login', |
||
| 688 | array('id' => $this->game->getIdentifier()) |
||
| 689 | ) |
||
| 690 | ); |
||
| 691 | } else { |
||
| 692 | return $this->redirect()->toUrl( |
||
| 693 | $this->frontendUrl()->fromRoute( |
||
| 694 | $this->game->getClassType() . '/index', |
||
| 695 | array('id' => $this->game->getIdentifier()) |
||
| 696 | ) |
||
| 697 | ); |
||
| 698 | } |
||
| 699 | } |
||
| 700 | |||
| 701 | $form->setAttribute( |
||
| 702 | 'action', |
||
| 703 | $this->frontendUrl()->fromRoute( |
||
| 704 | $this->game->getClassType().'/login', |
||
| 705 | array('id' => $this->game->getIdentifier()) |
||
| 706 | ) |
||
| 707 | ); |
||
| 708 | $viewModel = $this->buildView($this->game); |
||
| 709 | $viewModel->setVariables(array( |
||
| 710 | 'form' => $form, |
||
| 711 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 712 | )); |
||
| 713 | return $viewModel; |
||
| 714 | } |
||
| 715 | |||
| 716 | public function userregisterAction() |
||
| 717 | { |
||
| 718 | $userOptions = $this->getServiceLocator()->get('zfcuser_module_options'); |
||
| 719 | |||
| 720 | if ($this->zfcUserAuthentication()->hasIdentity()) { |
||
| 721 | return $this->redirect()->toUrl( |
||
| 722 | $this->frontendUrl()->fromRoute( |
||
| 723 | $this->game->getClassType().'/'.$this->game->nextStep('index'), |
||
| 724 | array('id' => $this->game->getIdentifier()) |
||
| 725 | ) |
||
| 726 | ); |
||
| 727 | } |
||
| 728 | $request = $this->getRequest(); |
||
| 729 | $service = $this->getServiceLocator()->get('zfcuser_user_service'); |
||
| 730 | $form = $this->getServiceLocator()->get('playgroundgame_register_form'); |
||
| 731 | $socialnetwork = $this->params()->fromRoute('socialnetwork', false); |
||
| 732 | $form->setAttribute( |
||
| 733 | 'action', |
||
| 734 | $this->frontendUrl()->fromRoute( |
||
| 735 | $this->game->getClassType().'/user-register', |
||
| 736 | array('id' => $this->game->getIdentifier()) |
||
| 737 | ) |
||
| 738 | ); |
||
| 739 | $params = array(); |
||
| 740 | $socialCredentials = array(); |
||
| 741 | |||
| 742 | if ($userOptions->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) { |
||
| 743 | $redirect = $request->getQuery()->get('redirect'); |
||
| 744 | } else { |
||
| 745 | $redirect = false; |
||
| 746 | } |
||
| 747 | |||
| 748 | if ($socialnetwork) { |
||
| 749 | $infoMe = $this->getProviderService()->getInfoMe($socialnetwork); |
||
| 750 | |||
| 751 | if (!empty($infoMe)) { |
||
| 752 | $user = $this->getProviderService()->getUserProviderMapper()->findUserByProviderId( |
||
| 753 | $infoMe->identifier, |
||
| 754 | $socialnetwork |
||
| 755 | ); |
||
| 756 | |||
| 757 | if ($user || $service->getOptions()->getCreateUserAutoSocial() === true) { |
||
| 758 | //on le dirige vers l'action d'authentification |
||
| 759 | if (! $redirect && $userOptions->getLoginRedirectRoute() != '') { |
||
| 760 | $redirect = $this->frontendUrl()->fromRoute( |
||
| 761 | $this->game->getClassType().'/login', |
||
| 762 | array('id' => $this->game->getIdentifier()) |
||
| 763 | ); |
||
| 764 | } |
||
| 765 | $redir = $this->frontendUrl()->fromRoute( |
||
| 766 | $this->game->getClassType().'/login', |
||
| 767 | array('id' => $this->game->getIdentifier()) |
||
| 768 | ) .'/' . $socialnetwork . ($redirect ? '?redirect=' . $redirect : ''); |
||
| 769 | |||
| 770 | return $this->redirect()->toUrl($redir); |
||
| 771 | } |
||
| 772 | |||
| 773 | // Je retire la saisie du login/mdp |
||
| 774 | $form->setAttribute( |
||
| 775 | 'action', |
||
| 776 | $this->frontendUrl()->fromRoute( |
||
| 777 | $this->game->getClassType().'/user-register', |
||
| 778 | array( |
||
| 779 | 'id' => $this->game->getIdentifier(), |
||
| 780 | 'socialnetwork' => $socialnetwork, |
||
| 781 | |||
| 782 | ) |
||
| 783 | ) |
||
| 784 | ); |
||
| 785 | $form->remove('password'); |
||
| 786 | $form->remove('passwordVerify'); |
||
| 787 | |||
| 788 | $birthMonth = $infoMe->birthMonth; |
||
| 789 | if (strlen($birthMonth) <= 1) { |
||
| 790 | $birthMonth = '0'.$birthMonth; |
||
| 791 | } |
||
| 792 | $birthDay = $infoMe->birthDay; |
||
| 793 | if (strlen($birthDay) <= 1) { |
||
| 794 | $birthDay = '0'.$birthDay; |
||
| 795 | } |
||
| 796 | |||
| 797 | $gender = $infoMe->gender; |
||
| 798 | if ($gender == 'female') { |
||
| 799 | $title = 'Me'; |
||
| 800 | } else { |
||
| 801 | $title = 'M'; |
||
| 802 | } |
||
| 803 | |||
| 804 | $params = array( |
||
| 805 | //'birth_year' => $infoMe->birthYear, |
||
| 806 | 'title' => $title, |
||
| 807 | 'dob' => $birthDay.'/'.$birthMonth.'/'.$infoMe->birthYear, |
||
| 808 | 'firstname' => $infoMe->firstName, |
||
| 809 | 'lastname' => $infoMe->lastName, |
||
| 810 | 'email' => $infoMe->email, |
||
| 811 | 'postalCode' => $infoMe->zip, |
||
| 812 | ); |
||
| 813 | $socialCredentials = array( |
||
| 814 | 'socialNetwork' => strtolower($socialnetwork), |
||
| 815 | 'socialId' => $infoMe->identifier, |
||
| 816 | ); |
||
| 817 | } |
||
| 818 | } |
||
| 819 | |||
| 820 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
| 821 | $this->game->getClassType().'/user-register', |
||
| 822 | array('id' => $this->game->getIdentifier()) |
||
| 823 | ) .($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
| 824 | $prg = $this->prg($redirectUrl, true); |
||
| 825 | |||
| 826 | if ($prg instanceof Response) { |
||
| 827 | return $prg; |
||
| 828 | } elseif ($prg === false) { |
||
| 829 | $form->setData($params); |
||
| 830 | $viewModel = $this->buildView($this->game); |
||
| 831 | $viewModel->setVariables(array( |
||
| 832 | 'registerForm' => $form, |
||
| 833 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
| 834 | 'redirect' => $redirect, |
||
| 835 | )); |
||
| 836 | return $viewModel; |
||
| 837 | } |
||
| 838 | |||
| 839 | $post = $prg; |
||
| 840 | $post = array_merge( |
||
| 841 | $post, |
||
| 842 | $socialCredentials |
||
| 843 | ); |
||
| 844 | |||
| 845 | if ($this->game->getOnInvitation()) { |
||
| 846 | $credential = trim( |
||
| 847 | $post[$this->getGameService()->getOptions()->getOnInvitationField()] |
||
| 848 | ); |
||
| 849 | if (!$credential) { |
||
| 850 | $credential = $this->params()->fromQuery( |
||
| 851 | $this->getGameService()->getOptions()->getOnInvitationField() |
||
| 852 | ); |
||
| 853 | } |
||
| 854 | $found = $this->getGameService()->getInvitationMapper()->findOneBy(array('requestKey'=>$credential)); |
||
| 855 | |||
| 856 | if (!$found || !empty($found->getUser())) { |
||
| 857 | $form->setData($post); |
||
| 858 | $viewModel = $this->buildView($this->game); |
||
| 859 | $viewModel->setVariables(array( |
||
| 860 | 'registerForm' => $form, |
||
| 861 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
| 862 | 'redirect' => $redirect, |
||
| 863 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
| 864 | 'flashErrors' => $this->flashMessenger()->getErrorMessages(), |
||
| 865 | )); |
||
| 866 | |||
| 867 | return $viewModel; |
||
| 868 | } |
||
| 869 | } |
||
| 870 | |||
| 871 | $user = $service->register($post, 'playgroundgame_register_form'); |
||
| 872 | |||
| 873 | if (! $user) { |
||
| 874 | $viewModel = $this->buildView($this->game); |
||
| 875 | $viewModel->setVariables(array( |
||
| 876 | 'registerForm' => $form, |
||
| 877 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
| 878 | 'redirect' => $redirect, |
||
| 879 | )); |
||
| 880 | |||
| 881 | return $viewModel; |
||
| 882 | } |
||
| 883 | |||
| 884 | if ($this->game->getOnInvitation()) { |
||
| 885 | // user has been created, associate the code with the userId |
||
| 886 | $found->setUser($user); |
||
| 887 | $this->getGameService()->getInvitationMapper()->update($found); |
||
| 888 | } |
||
| 889 | |||
| 890 | if ($service->getOptions()->getEmailVerification()) { |
||
| 891 | $vm = new ViewModel(array('userEmail' => $user->getEmail())); |
||
| 892 | $vm->setTemplate('playground-user/register/registermail'); |
||
| 893 | |||
| 894 | return $vm; |
||
| 895 | } elseif ($service->getOptions()->getLoginAfterRegistration()) { |
||
| 896 | $identityFields = $service->getOptions()->getAuthIdentityFields(); |
||
| 897 | if (in_array('email', $identityFields)) { |
||
| 898 | $post['identity'] = $user->getEmail(); |
||
| 899 | } elseif (in_array('username', $identityFields)) { |
||
| 900 | $post['identity'] = $user->getUsername(); |
||
| 901 | } |
||
| 902 | $post['credential'] = isset($post['password'])?$post['password']:''; |
||
| 903 | $request->setPost(new Parameters($post)); |
||
| 904 | |||
| 905 | // clear adapters |
||
| 906 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
| 907 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
| 908 | |||
| 909 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
| 910 | |||
| 911 | View Code Duplication | if ($logged) { |
|
| 912 | return $this->redirect()->toUrl( |
||
| 913 | $this->frontendUrl()->fromRoute( |
||
| 914 | $this->game->getClassType(), |
||
| 915 | array('id' => $this->game->getIdentifier()) |
||
| 916 | ) |
||
| 917 | ); |
||
| 918 | } else { |
||
| 919 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( |
||
| 920 | 'Authentication failed. Please try again.' |
||
| 921 | ); |
||
| 922 | return $this->redirect()->toUrl( |
||
| 923 | $this->frontendUrl()->fromRoute( |
||
| 924 | $this->game->getClassType() . '/login', |
||
| 925 | array('id' => $this->game->getIdentifier()) |
||
| 926 | ) |
||
| 927 | ); |
||
| 928 | } |
||
| 929 | } |
||
| 930 | |||
| 931 | $redirect = $this->frontendUrl()->fromRoute( |
||
| 932 | $this->game->getClassType().'/login', |
||
| 933 | array( |
||
| 934 | 'id' => $this->game->getIdentifier(), |
||
| 935 | |||
| 936 | ) |
||
| 937 | ) . ($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
| 938 | |||
| 939 | return $this->redirect()->toUrl($redirect); |
||
| 940 | } |
||
| 941 | |||
| 942 | /** |
||
| 943 | * |
||
| 944 | * @param \PlaygroundGame\Entity\Game $game |
||
| 945 | * @param \PlaygroundUser\Entity\User $user |
||
| 946 | */ |
||
| 947 | public function checkFbRegistration($user, $game) |
||
| 996 | |||
| 997 | /** |
||
| 998 | * This method create the basic Game view |
||
| 999 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1000 | */ |
||
| 1001 | public function buildView($game) |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1055 | */ |
||
| 1056 | public function addAdditionalView($game) |
||
| 1057 | { |
||
| 1058 | $view = false; |
||
| 1059 | |||
| 1060 | $actionName = $this->getEvent()->getRouteMatch()->getParam('action', 'not-found'); |
||
| 1061 | $stepsViews = json_decode($game->getStepsViews(), true); |
||
| 1062 | if ($stepsViews && isset($stepsViews[$actionName])) { |
||
| 1063 | $beforeLayout = $this->layout()->getTemplate(); |
||
| 1064 | $actionData = $stepsViews[$actionName]; |
||
| 1065 | if (is_string($actionData)) { |
||
| 1066 | $action = $actionData; |
||
| 1067 | $controller = $this->getEvent()->getRouteMatch()->getParam('controller', 'playgroundgame_game'); |
||
| 1068 | $view = $this->forward()->dispatch( |
||
| 1069 | $controller, |
||
| 1070 | array( |
||
| 1071 | 'action' => $action, |
||
| 1072 | 'id' => $game->getIdentifier() |
||
| 1073 | ) |
||
| 1074 | ); |
||
| 1075 | } elseif (is_array($actionData) && count($actionData)>0) { |
||
| 1076 | $action = key($actionData); |
||
| 1077 | $controller = $actionData[$action]; |
||
| 1078 | $view = $this->forward()->dispatch( |
||
| 1079 | $controller, |
||
| 1080 | array( |
||
| 1081 | 'action' => $action, |
||
| 1082 | 'id' => $game->getIdentifier() |
||
| 1083 | ) |
||
| 1084 | ); |
||
| 1085 | } |
||
| 1086 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
| 1087 | $this->layout()->setTemplate($beforeLayout); |
||
| 1088 | } |
||
| 1089 | |||
| 1090 | return $view; |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | public function addMetaBitly() |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1106 | */ |
||
| 1107 | public function addGaEvent($game) |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1118 | */ |
||
| 1119 | public function addMetaTitle($game) |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1142 | */ |
||
| 1143 | public function customizeGameDesign($game) |
||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1161 | */ |
||
| 1162 | public function getShareData($game) |
||
| 1248 | |||
| 1249 | /** |
||
| 1250 | * return ajax response in json format |
||
| 1251 | * |
||
| 1252 | * @param array $data |
||
| 1253 | * @return \Zend\View\Model\JsonModel |
||
| 1254 | */ |
||
| 1255 | View Code Duplication | protected function successJson($data = null) |
|
| 1263 | |||
| 1264 | /** |
||
| 1265 | * return ajax response in json format |
||
| 1266 | * |
||
| 1267 | * @param string $message |
||
| 1268 | * @return \Zend\View\Model\JsonModel |
||
| 1269 | */ |
||
| 1270 | View Code Duplication | protected function errorJson($message = null) |
|
| 1278 | |||
| 1279 | /** |
||
| 1280 | * @param string $helperName |
||
| 1281 | */ |
||
| 1282 | protected function getViewHelper($helperName) |
||
| 1286 | |||
| 1287 | public function getGameService() |
||
| 1295 | |||
| 1296 | public function setGameService(GameService $gameService) |
||
| 1302 | |||
| 1303 | public function getPrizeService() |
||
| 1311 | |||
| 1312 | public function setPrizeService(PrizeService $prizeService) |
||
| 1318 | |||
| 1319 | public function getOptions() |
||
| 1327 | |||
| 1328 | public function setOptions($options) |
||
| 1334 | } |
||
| 1335 |
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: