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)  | 
            ||
| 64 |     { | 
            ||
| 65 | parent::setEventManager($events);  | 
            ||
| 66 | |||
| 67 | $controller = $this;  | 
            ||
| 68 |         $events->attach('dispatch', function (\Zend\Mvc\MvcEvent $e) use ($controller) { | 
            ||
| 69 | |||
| 70 |             $identifier = $e->getRouteMatch()->getParam('id'); | 
            ||
| 71 | $controller->game = $controller->getGameService()->checkGame($identifier, false);  | 
            ||
| 72 | if (!$controller->game &&  | 
            ||
| 73 |                 in_array($controller->params('action'), $controller->withGame) | 
            ||
| 74 |             ) { | 
            ||
| 75 | return $controller->notFoundAction();  | 
            ||
| 76 | }  | 
            ||
| 77 | |||
| 78 | if ($controller->game &&  | 
            ||
| 79 | $controller->game->isClosed() &&  | 
            ||
| 80 |                 in_array($controller->params('action'), $controller->withOnlineGame) | 
            ||
| 81 |             ) { | 
            ||
| 82 | return $controller->notFoundAction();  | 
            ||
| 83 | }  | 
            ||
| 84 | |||
| 85 | $controller->user = $controller->zfcUserAuthentication()->getIdentity();  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 86 | if ($controller->game &&  | 
            ||
| 87 | !$controller->user &&  | 
            ||
| 88 | !$controller->game->getAnonymousAllowed() &&  | 
            ||
| 89 |                 in_array($controller->params('action'), $controller->withAnyUser) | 
            ||
| 90 |             ) { | 
            ||
| 91 | $redirect = urlencode(  | 
            ||
| 92 | $controller->url()->fromRoute(  | 
            ||
| 93 |                         'frontend/'.$controller->game->getClassType() . '/' . $controller->params('action'), | 
            ||
| 94 |                         array('id' => $controller->game->getIdentifier()), | 
            ||
| 95 |                         array('force_canonical' => true) | 
            ||
| 96 | )  | 
            ||
| 97 | );  | 
            ||
| 98 | |||
| 99 | $urlRegister = $controller->url()->fromRoute(  | 
            ||
| 100 | 'frontend/zfcuser/register',  | 
            ||
| 101 | array(),  | 
            ||
| 102 |                     array('force_canonical' => true) | 
            ||
| 103 | ) . '?redirect='.$redirect;  | 
            ||
| 104 | |||
| 105 | // code permettant d'identifier un custom game  | 
            ||
| 106 |                 // ligne $config = $controller->getGameService()->getServiceManager()->get('config'); | 
            ||
| 107 |                 // ligne $customUrl = str_replace('frontend.', '', $e->getRouteMatch()->getParam('area', '')); | 
            ||
| 108 | // ligne if ($config['custom_games'][$controller->game->getIdentifier()] &&  | 
            ||
| 109 | // ligne $controller->getRequest()->getUri()->getHost() === $customUrl  | 
            ||
| 110 |                 // ligne ) { | 
            ||
| 111 | return $controller->redirect()->toUrl($urlRegister);  | 
            ||
| 112 | |||
| 113 | }  | 
            ||
| 114 | |||
| 115 | return;  | 
            ||
| 116 | }, 100); // execute before executing action logic  | 
            ||
| 117 | }  | 
            ||
| 118 | |||
| 119 | /**  | 
            ||
| 120 | * Action called if matched action does not exist  | 
            ||
| 121 | * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy  | 
            ||
| 122 | * it has to be rendered in the controller. Hence the code below.  | 
            ||
| 123 | *  | 
            ||
| 124 | * This action is injected as a catchall action for each custom_games definition  | 
            ||
| 125 | * This way, when a custom_game is created, the 404 is it's responsability and the  | 
            ||
| 126 | * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml  | 
            ||
| 127 | *  | 
            ||
| 128 | *  | 
            ||
| 129 | * @return \Zend\Stdlib\ResponseInterface  | 
            ||
| 130 | */  | 
            ||
| 131 | public function notFoundAction()  | 
            ||
| 132 |     { | 
            ||
| 133 |         $viewRender     = $this->getServiceLocator()->get('ViewRenderer'); | 
            ||
| 134 | |||
| 135 |         $this->getEvent()->getRouteMatch()->setParam('action', 'not-found'); | 
            ||
| 136 | $this->response->setStatusCode(404);  | 
            ||
| 137 | |||
| 138 | $res = 'error/404';  | 
            ||
| 139 | |||
| 140 | $viewModel = $this->buildView($this->game);  | 
            ||
| 141 | $viewModel->setTemplate($res);  | 
            ||
| 142 | |||
| 143 |         $this->layout()->setVariable("content", $viewRender->render($viewModel)); | 
            ||
| 144 | $this->response->setContent($viewRender->render($this->layout()));  | 
            ||
| 145 | |||
| 146 | return $this->response;  | 
            ||
| 147 | }  | 
            ||
| 148 | |||
| 149 | /**  | 
            ||
| 150 | * This action acts as a hub : Depending on the first step of the game, it will forward the action to this step  | 
            ||
| 151 | */  | 
            ||
| 152 | public function homeAction()  | 
            ||
| 153 |     { | 
            ||
| 154 | // This fix exists only for safari in FB on Windows : we need to redirect the user to the page  | 
            ||
| 155 | // outside of iframe for the cookie to be accepted. PlaygroundCore redirects to the FB Iframed page when  | 
            ||
| 156 | // it discovers that the user arrives for the first time on the game in FB.  | 
            ||
| 157 | // When core redirects, it adds a 'redir_fb_page_id' var in the querystring  | 
            ||
| 158 | // Here, we test if this var exist, and then send the user back to the game in FB.  | 
            ||
| 159 | // Now the cookie will be accepted by Safari...  | 
            ||
| 160 |         $pageId = $this->params()->fromQuery('redir_fb_page_id'); | 
            ||
| 161 |         if (!empty($pageId)) { | 
            ||
| 162 | $appId = 'app_'.$this->game->getFbAppId();  | 
            ||
| 163 | $url = '//www.facebook.com/pages/game/'.$pageId.'?sk='.$appId;  | 
            ||
| 164 | |||
| 165 | return $this->redirect()->toUrl($url);  | 
            ||
| 166 | }  | 
            ||
| 167 | |||
| 168 | // If an entry has already been done during this session, I reset the anonymous_identifier cookie  | 
            ||
| 169 | // so that another person can play the same game (if game conditions are fullfilled)  | 
            ||
| 170 |         $session = new Container('anonymous_identifier'); | 
            ||
| 171 |         if ($session->offsetExists('anonymous_identifier')) { | 
            ||
| 172 |             $session->offsetUnset('anonymous_identifier'); | 
            ||
| 173 | }  | 
            ||
| 174 | |||
| 175 | return $this->forward()->dispatch(  | 
            ||
| 176 | 'playgroundgame_'.$this->game->getClassType(),  | 
            ||
| 177 | array(  | 
            ||
| 178 | 'controller' => 'playgroundgame_'.$this->game->getClassType(),  | 
            ||
| 179 | 'action' => $this->game->firstStep(),  | 
            ||
| 180 | 'id' => $this->game->getIdentifier()  | 
            ||
| 181 | )  | 
            ||
| 182 | );  | 
            ||
| 183 | }  | 
            ||
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * Homepage of the game  | 
            ||
| 187 | */  | 
            ||
| 188 | public function indexAction()  | 
            ||
| 189 |     { | 
            ||
| 190 | $isSubscribed = false;  | 
            ||
| 191 | |||
| 192 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user);  | 
            ||
| 193 |         if ($entry) { | 
            ||
| 194 | $isSubscribed = true;  | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | $viewModel = $this->buildView($this->game);  | 
            ||
| 198 | $viewModel->setVariables(array(  | 
            ||
| 199 | 'isSubscribed' => $isSubscribed  | 
            ||
| 200 | ));  | 
            ||
| 201 | |||
| 202 | return $viewModel;  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | /**  | 
            ||
| 206 | * leaderboardAction  | 
            ||
| 207 | *  | 
            ||
| 208 | * @return ViewModel $viewModel  | 
            ||
| 209 | */  | 
            ||
| 210 | public function leaderboardAction()  | 
            ||
| 211 |     { | 
            ||
| 212 |         $filter = $this->getEvent()->getRouteMatch()->getParam('filter'); | 
            ||
| 213 |         $p = $this->getEvent()->getRouteMatch()->getParam('p'); | 
            ||
| 214 | |||
| 215 | $beforeLayout = $this->layout()->getTemplate();  | 
            ||
| 216 | $subViewModel = $this->forward()->dispatch(  | 
            ||
| 217 | 'playgroundreward',  | 
            ||
| 218 |             array('action' => 'leaderboard', 'filter' => $filter, 'p' => $p) | 
            ||
| 219 | );  | 
            ||
| 220 | |||
| 221 | // suite au forward, le template de layout a changé, je dois le rétablir...  | 
            ||
| 222 | $this->layout()->setTemplate($beforeLayout);  | 
            ||
| 223 | |||
| 224 | // give the ability to the game to have its customized look and feel.  | 
            ||
| 225 |         $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); | 
            ||
| 226 | $l = $templatePathResolver->getPaths();  | 
            ||
| 227 | |||
| 228 | $templatePathResolver->addPath($l[0].'custom/'.$this->game->getIdentifier());  | 
            ||
| 229 | |||
| 230 | return $subViewModel;  | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 | /**  | 
            ||
| 234 | * This action has been designed to be called by other controllers  | 
            ||
| 235 | * It gives the ability to display an information form and persist it in the game entry  | 
            ||
| 236 | *  | 
            ||
| 237 | * @return \Zend\View\Model\ViewModel  | 
            ||
| 238 | */  | 
            ||
| 239 | public function registerAction()  | 
            ||
| 240 |     { | 
            ||
| 241 | $form = $this->getGameService()->createFormFromJson($this->game->getPlayerForm()->getForm(), 'playerForm');  | 
            ||
| 242 | |||
| 243 |         if ($this->getRequest()->isPost()) { | 
            ||
| 244 | // POST Request: Process form  | 
            ||
| 245 | $data = array_merge_recursive(  | 
            ||
| 246 | $this->getRequest()->getPost()->toArray(),  | 
            ||
| 247 | $this->getRequest()->getFiles()->toArray()  | 
            ||
| 248 | );  | 
            ||
| 249 | |||
| 250 | $form->setData($data);  | 
            ||
| 251 | |||
| 252 |             if ($form->isValid()) { | 
            ||
| 253 | // steps of the game  | 
            ||
| 254 | $steps = $this->game->getStepsArray();  | 
            ||
| 255 | // sub steps of the game  | 
            ||
| 256 | $viewSteps = $this->game->getStepsViewsArray();  | 
            ||
| 257 | |||
| 258 | // register position  | 
            ||
| 259 |                 $key = array_search($this->params('action'), $viewSteps); | 
            ||
| 260 |                 if (!$key) { | 
            ||
| 261 | // register is not a substep of the game so it's a step  | 
            ||
| 262 |                     $key = array_search($this->params('action'), $steps); | 
            ||
| 263 | $keyStep = true;  | 
            ||
| 264 |                 } else { | 
            ||
| 265 | // register was a substep, i search the index of its parent  | 
            ||
| 266 | $key = array_search($key, $steps);  | 
            ||
| 267 | $keyStep = false;  | 
            ||
| 268 | }  | 
            ||
| 269 | |||
| 270 | // play position  | 
            ||
| 271 |                 $keyplay = array_search('play', $viewSteps); | 
            ||
| 272 | |||
| 273 |                 if (!$keyplay) { | 
            ||
| 274 | // play is not a substep, so it's a step  | 
            ||
| 275 |                     $keyplay = array_search('play', $steps); | 
            ||
| 276 | $keyplayStep = true;  | 
            ||
| 277 |                 } else { | 
            ||
| 278 | // play is a substep so I search the index of its parent  | 
            ||
| 279 | $keyplay = array_search($keyplay, $steps);  | 
            ||
| 280 | $keyplayStep = false;  | 
            ||
| 281 | }  | 
            ||
| 282 | |||
| 283 | // If register step before play, I don't have no entry yet. I have to create one  | 
            ||
| 284 | // If register after play step, I search for the last entry created by play step.  | 
            ||
| 285 | |||
| 286 |                 if ($key < $keyplay || ($keyStep && !$keyplayStep && $key <= $keyplay)) { | 
            ||
| 287 | $entry = $this->getGameService()->play($this->game, $this->user);  | 
            ||
| 288 |                     if (!$entry) { | 
            ||
| 289 | // the user has already taken part of this game and the participation limit has been reached  | 
            ||
| 290 |                         $this->flashMessenger()->addMessage('Vous avez déjà participé'); | 
            ||
| 291 | |||
| 292 | return $this->redirect()->toUrl(  | 
            ||
| 293 | $this->frontendUrl()->fromRoute(  | 
            ||
| 294 | $this->game->getClassType().'/result',  | 
            ||
| 295 | array(  | 
            ||
| 296 | 'id' => $this->game->getIdentifier(),  | 
            ||
| 297 | |||
| 298 | )  | 
            ||
| 299 | )  | 
            ||
| 300 | );  | 
            ||
| 301 | }  | 
            ||
| 302 |                 } else { | 
            ||
| 303 | // I'm looking for an entry without anonymousIdentifier (the active entry in fact).  | 
            ||
| 304 | $entry = $this->getGameService()->findLastEntry($this->game, $this->user);  | 
            ||
| 305 |                     if ($this->getGameService()->hasReachedPlayLimit($this->game, $this->user)) { | 
            ||
| 306 | // the user has already taken part of this game and the participation limit has been reached  | 
            ||
| 307 |                         $this->flashMessenger()->addMessage('Vous avez déjà participé'); | 
            ||
| 308 | |||
| 309 | return $this->redirect()->toUrl(  | 
            ||
| 310 | $this->frontendUrl()->fromRoute(  | 
            ||
| 311 | $this->game->getClassType().'/result',  | 
            ||
| 312 | array(  | 
            ||
| 313 | 'id' => $this->game->getIdentifier(),  | 
            ||
| 314 | |||
| 315 | )  | 
            ||
| 316 | )  | 
            ||
| 317 | );  | 
            ||
| 318 | }  | 
            ||
| 319 | }  | 
            ||
| 320 | |||
| 321 | $this->getGameService()->updateEntryPlayerForm($form->getData(), $this->game, $this->user, $entry);  | 
            ||
| 322 | |||
| 323 |                 if (!empty($this->game->nextStep($this->params('action')))) { | 
            ||
| 324 | return $this->redirect()->toUrl(  | 
            ||
| 325 | $this->frontendUrl()->fromRoute(  | 
            ||
| 326 |                             $this->game->getClassType() .'/' . $this->game->nextStep($this->params('action')), | 
            ||
| 327 |                             array('id' => $this->game->getIdentifier()), | 
            ||
| 328 |                             array('force_canonical' => true) | 
            ||
| 329 | )  | 
            ||
| 330 | );  | 
            ||
| 331 | }  | 
            ||
| 332 | }  | 
            ||
| 333 | }  | 
            ||
| 334 | |||
| 335 | $viewModel = $this->buildView($this->game);  | 
            ||
| 336 | $viewModel->setVariables(array(  | 
            ||
| 337 | 'form' => $form  | 
            ||
| 338 | ));  | 
            ||
| 339 | |||
| 340 | return $viewModel;  | 
            ||
| 341 | }  | 
            ||
| 342 | |||
| 343 | /**  | 
            ||
| 344 | * This action takes care of the terms of the game  | 
            ||
| 345 | */  | 
            ||
| 346 | public function termsAction()  | 
            ||
| 347 |     { | 
            ||
| 348 | $viewModel = $this->buildView($this->game);  | 
            ||
| 349 | |||
| 350 | return $viewModel;  | 
            ||
| 351 | }  | 
            ||
| 352 | |||
| 353 | /**  | 
            ||
| 354 | * This action takes care of the conditions of the game  | 
            ||
| 355 | */  | 
            ||
| 356 | public function conditionsAction()  | 
            ||
| 357 |     { | 
            ||
| 358 | $viewModel = $this->buildView($this->game);  | 
            ||
| 359 | |||
| 360 | return $viewModel;  | 
            ||
| 361 | }  | 
            ||
| 362 | |||
| 363 | /**  | 
            ||
| 364 | * This action takes care of bounce page of the game  | 
            ||
| 365 | */  | 
            ||
| 366 | public function bounceAction()  | 
            ||
| 367 |     { | 
            ||
| 368 | $availableGames = $this->getGameService()->getAvailableGames($this->user);  | 
            ||
| 369 | |||
| 370 | $rssUrl = '';  | 
            ||
| 371 |         $config = $this->getGameService()->getServiceManager()->get('config'); | 
            ||
| 372 |         if (isset($config['rss']['url'])) { | 
            ||
| 373 | $rssUrl = $config['rss']['url'];  | 
            ||
| 374 | }  | 
            ||
| 375 | |||
| 376 | $viewModel = $this->buildView($this->game);  | 
            ||
| 377 | $viewModel->setVariables(array(  | 
            ||
| 378 | 'rssUrl' => $rssUrl,  | 
            ||
| 379 | 'user' => $this->user,  | 
            ||
| 380 | 'availableGames' => $availableGames,  | 
            ||
| 381 | ));  | 
            ||
| 382 | |||
| 383 | return $viewModel;  | 
            ||
| 384 | }  | 
            ||
| 385 | |||
| 386 | |||
| 387 | /**  | 
            ||
| 388 | * This action displays the Prizes page associated to the game  | 
            ||
| 389 | */  | 
            ||
| 390 | public function prizesAction()  | 
            ||
| 391 |     { | 
            ||
| 392 |         if (count($this->game->getPrizes()) == 0) { | 
            ||
| 393 | return $this->notFoundAction();  | 
            ||
| 394 | }  | 
            ||
| 395 | |||
| 396 | $viewModel = $this->buildView($this->game);  | 
            ||
| 397 | |||
| 398 | return $viewModel;  | 
            ||
| 399 | }  | 
            ||
| 400 | |||
| 401 | /**  | 
            ||
| 402 | * This action displays a specific Prize page among those associated to the game  | 
            ||
| 403 | */  | 
            ||
| 404 | public function prizeAction()  | 
            ||
| 405 |     { | 
            ||
| 406 |         $prizeIdentifier = $this->getEvent()->getRouteMatch()->getParam('prize'); | 
            ||
| 407 | $prize = $this->getPrizeService()->getPrizeMapper()->findByIdentifier($prizeIdentifier);  | 
            ||
| 408 | |||
| 409 |         if (!$prize) { | 
            ||
| 410 | return $this->notFoundAction();  | 
            ||
| 411 | }  | 
            ||
| 412 | |||
| 413 | $viewModel = $this->buildView($this->game);  | 
            ||
| 414 |         $viewModel->setVariables(array('prize'=> $prize)); | 
            ||
| 415 | |||
| 416 | return $viewModel;  | 
            ||
| 417 | }  | 
            ||
| 418 | |||
| 419 | public function gameslistAction()  | 
            ||
| 420 |     { | 
            ||
| 421 | $layoutViewModel = $this->layout();  | 
            ||
| 422 | |||
| 423 | $slider = new ViewModel();  | 
            ||
| 424 |         $slider->setTemplate('playground-game/common/top_promo'); | 
            ||
| 425 | |||
| 426 | $sliderItems = $this->getGameService()->getActiveSliderGames();  | 
            ||
| 427 | |||
| 428 |         $slider->setVariables(array('sliderItems' => $sliderItems)); | 
            ||
| 429 | |||
| 430 | $layoutViewModel->addChild($slider, 'slider');  | 
            ||
| 431 | |||
| 432 | $games = $this->getGameService()->getActiveGames(false, '', 'endDate');  | 
            ||
| 433 |         if (is_array($games)) { | 
            ||
| 434 | $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($games));  | 
            ||
| 435 |         } else { | 
            ||
| 436 | $paginator = $games;  | 
            ||
| 437 | }  | 
            ||
| 438 | |||
| 439 | $paginator->setItemCountPerPage(7);  | 
            ||
| 440 |         $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); | 
            ||
| 441 | |||
| 442 | $bitlyclient = $this->getOptions()->getBitlyUrl();  | 
            ||
| 443 | $bitlyuser = $this->getOptions()->getBitlyUsername();  | 
            ||
| 444 | $bitlykey = $this->getOptions()->getBitlyApiKey();  | 
            ||
| 445 | |||
| 446 |         $this->getViewHelper('HeadMeta')->setProperty('bt:client', $bitlyclient); | 
            ||
| 447 |         $this->getViewHelper('HeadMeta')->setProperty('bt:user', $bitlyuser); | 
            ||
| 448 |         $this->getViewHelper('HeadMeta')->setProperty('bt:key', $bitlykey); | 
            ||
| 449 | |||
| 450 | $this->layout()->setVariables(  | 
            ||
| 451 | array(  | 
            ||
| 452 | 'sliderItems' => $sliderItems,  | 
            ||
| 453 | 'currentPage' => array(  | 
            ||
| 454 | 'pageGames' => 'games',  | 
            ||
| 455 | 'pageWinners' => ''  | 
            ||
| 456 | ),  | 
            ||
| 457 | )  | 
            ||
| 458 | );  | 
            ||
| 459 | |||
| 460 | return new ViewModel(  | 
            ||
| 461 | array(  | 
            ||
| 462 | 'games' => $paginator  | 
            ||
| 463 | )  | 
            ||
| 464 | );  | 
            ||
| 465 | }  | 
            ||
| 466 | |||
| 467 | public function fangateAction()  | 
            ||
| 468 |     { | 
            ||
| 469 | $viewModel = $this->buildView($this->game);  | 
            ||
| 470 | |||
| 471 | return $viewModel;  | 
            ||
| 472 | }  | 
            ||
| 473 | |||
| 474 | public function shareAction()  | 
            ||
| 475 |     { | 
            ||
| 476 | $statusMail = null;  | 
            ||
| 477 | |||
| 478 | // Has the user finished the game ?  | 
            ||
| 479 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user);  | 
            ||
| 480 | |||
| 481 |         if ($lastEntry === null) { | 
            ||
| 482 | return $this->redirect()->toUrl(  | 
            ||
| 483 | $this->frontendUrl()->fromRoute(  | 
            ||
| 484 | 'postvote',  | 
            ||
| 485 |                     array('id' => $this->game->getIdentifier()) | 
            ||
| 486 | )  | 
            ||
| 487 | );  | 
            ||
| 488 | }  | 
            ||
| 489 | |||
| 490 |         $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); | 
            ||
| 491 |         $form->setAttribute('method', 'post'); | 
            ||
| 492 | |||
| 493 |         if ($this->getRequest()->isPost()) { | 
            ||
| 494 | $data = $this->getRequest()->getPost()->toArray();  | 
            ||
| 495 | $form->setData($data);  | 
            ||
| 496 | View Code Duplication |             if ($form->isValid()) { | 
            |
| 497 | $result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry);  | 
            ||
| 498 |                 if ($result) { | 
            ||
| 499 | $statusMail = true;  | 
            ||
| 500 | }  | 
            ||
| 501 | }  | 
            ||
| 502 | }  | 
            ||
| 503 | |||
| 504 | // buildView must be before sendMail because it adds the game template path to the templateStack  | 
            ||
| 505 | $viewModel = $this->buildView($this->game);  | 
            ||
| 506 | |||
| 507 | $this->getGameService()->sendMail($this->game, $this->user, $lastEntry);  | 
            ||
| 508 | |||
| 509 | $viewModel->setVariables(array(  | 
            ||
| 510 | 'statusMail' => $statusMail,  | 
            ||
| 511 | 'form' => $form,  | 
            ||
| 512 | ));  | 
            ||
| 513 | |||
| 514 | return $viewModel;  | 
            ||
| 515 | }  | 
            ||
| 516 | |||
| 517 | View Code Duplication | public function fbshareAction()  | 
            |
| 518 |     { | 
            ||
| 519 | $viewModel = new JsonModel();  | 
            ||
| 520 | $viewModel->setTerminal(true);  | 
            ||
| 521 |         $fbId = $this->params()->fromQuery('fbId'); | 
            ||
| 522 |         if (!$this->game) { | 
            ||
| 523 | return $this->errorJson();  | 
            ||
| 524 | }  | 
            ||
| 525 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user);  | 
            ||
| 526 |         if (! $entry) { | 
            ||
| 527 | return $this->errorJson();  | 
            ||
| 528 | }  | 
            ||
| 529 |         if (!$fbId) { | 
            ||
| 530 | return $this->errorJson();  | 
            ||
| 531 | }  | 
            ||
| 532 | |||
| 533 | $this->getGameService()->postFbWall($fbId, $this->game, $this->user, $entry);  | 
            ||
| 534 | |||
| 535 | return $this->successJson();  | 
            ||
| 536 | }  | 
            ||
| 537 | |||
| 538 | public function fbrequestAction()  | 
            ||
| 539 |     { | 
            ||
| 540 | $viewModel = new ViewModel();  | 
            ||
| 541 | $viewModel->setTerminal(true);  | 
            ||
| 542 |         $fbId = $this->params()->fromQuery('fbId'); | 
            ||
| 543 |         $to = $this->params()->fromQuery('to'); | 
            ||
| 544 | |||
| 545 |         if (!$this->game) { | 
            ||
| 546 | return $this->errorJson();  | 
            ||
| 547 | }  | 
            ||
| 548 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user);  | 
            ||
| 549 |         if (! $entry) { | 
            ||
| 550 | return $this->errorJson();  | 
            ||
| 551 | }  | 
            ||
| 552 |         if (!$fbId) { | 
            ||
| 553 | return $this->errorJson();  | 
            ||
| 554 | }  | 
            ||
| 555 | |||
| 556 | $this->getGameService()->postFbRequest($fbId, $this->game, $this->user, $entry, $to);  | 
            ||
| 557 | |||
| 558 | return $this->successJson();  | 
            ||
| 559 | }  | 
            ||
| 560 | |||
| 561 | public function tweetAction()  | 
            ||
| 562 |     { | 
            ||
| 563 |         $tweetId = $this->params()->fromQuery('tweetId'); | 
            ||
| 564 | |||
| 565 |         if (!$this->game) { | 
            ||
| 566 | return $this->errorJson();  | 
            ||
| 567 | }  | 
            ||
| 568 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user);  | 
            ||
| 569 |         if (! $entry) { | 
            ||
| 570 | return $this->errorJson();  | 
            ||
| 571 | }  | 
            ||
| 572 |         if (!$tweetId) { | 
            ||
| 573 | return $this->errorJson();  | 
            ||
| 574 | }  | 
            ||
| 575 | |||
| 576 | $this->getGameService()->postTwitter($tweetId, $this->game, $this->user, $entry);  | 
            ||
| 577 | |||
| 578 | return $this->successJson();  | 
            ||
| 579 | }  | 
            ||
| 580 | |||
| 581 | View Code Duplication | public function googleAction()  | 
            |
| 582 |     { | 
            ||
| 583 | $viewModel = new ViewModel();  | 
            ||
| 584 | $viewModel->setTerminal(true);  | 
            ||
| 585 |         $googleId = $this->params()->fromQuery('googleId'); | 
            ||
| 586 | |||
| 587 |         if (!$this->game) { | 
            ||
| 588 | return $this->errorJson();  | 
            ||
| 589 | }  | 
            ||
| 590 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user);  | 
            ||
| 591 |         if (! $entry) { | 
            ||
| 592 | return $this->errorJson();  | 
            ||
| 593 | }  | 
            ||
| 594 |         if (!$googleId) { | 
            ||
| 595 | return $this->errorJson();  | 
            ||
| 596 | }  | 
            ||
| 597 | |||
| 598 | $this->getGameService()->postGoogle($googleId, $this->game, $this->user, $entry);  | 
            ||
| 599 | |||
| 600 | return $this->successJson();  | 
            ||
| 601 | }  | 
            ||
| 602 | |||
| 603 | public function optinAction()  | 
            ||
| 604 |     { | 
            ||
| 605 |         $userService = $this->getServiceLocator()->get('zfcuser_user_service'); | 
            ||
| 606 | |||
| 607 |         if ($this->getRequest()->isPost()) { | 
            ||
| 608 |             $data['optin'] = ($this->params()->fromPost('optin'))? 1:0; | 
            ||
| 609 |             $data['optinPartner'] = ($this->params()->fromPost('optinPartner'))? 1:0; | 
            ||
| 610 | |||
| 611 | $userService->updateNewsletter($data);  | 
            ||
| 612 | }  | 
            ||
| 613 | |||
| 614 | return $this->redirect()->toUrl(  | 
            ||
| 615 | $this->frontendUrl()->fromRoute(  | 
            ||
| 616 | 'frontend/' . $this->game->getClassType() . '/index',  | 
            ||
| 617 |                 array('id' => $this->game->getIdentifier()) | 
            ||
| 618 | )  | 
            ||
| 619 | );  | 
            ||
| 620 | }  | 
            ||
| 621 | |||
| 622 | public function loginAction()  | 
            ||
| 623 |     { | 
            ||
| 624 | $request = $this->getRequest();  | 
            ||
| 625 |         $form = $this->getServiceLocator()->get('zfcuser_login_form'); | 
            ||
| 626 | |||
| 627 |         if ($request->isPost()) { | 
            ||
| 628 | $form->setData($request->getPost());  | 
            ||
| 629 | |||
| 630 |             if (!$form->isValid()) { | 
            ||
| 631 |                 $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( | 
            ||
| 632 | 'Authentication failed. Please try again.'  | 
            ||
| 633 | );  | 
            ||
| 634 | |||
| 635 | |||
| 636 | return $this->redirect()->toUrl(  | 
            ||
| 637 | $this->frontendUrl()->fromRoute(  | 
            ||
| 638 | $this->game->getClassType() . '/login',  | 
            ||
| 639 |                         array('id' => $this->game->getIdentifier()) | 
            ||
| 640 | )  | 
            ||
| 641 | );  | 
            ||
| 642 | }  | 
            ||
| 643 | |||
| 644 | // clear adapters  | 
            ||
| 645 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();  | 
            ||
| 646 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity();  | 
            ||
| 647 | |||
| 648 |             $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); | 
            ||
| 649 | |||
| 650 | View Code Duplication |             if (!$logged) { | 
            |
| 651 |                 $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( | 
            ||
| 652 | 'Authentication failed. Please try again.'  | 
            ||
| 653 | );  | 
            ||
| 654 | |||
| 655 | return $this->redirect()->toUrl(  | 
            ||
| 656 | $this->frontendUrl()->fromRoute(  | 
            ||
| 657 | $this->game->getClassType() . '/login',  | 
            ||
| 658 |                         array('id' => $this->game->getIdentifier()) | 
            ||
| 659 | )  | 
            ||
| 660 | );  | 
            ||
| 661 |             } else { | 
            ||
| 662 | return $this->redirect()->toUrl(  | 
            ||
| 663 | $this->frontendUrl()->fromRoute(  | 
            ||
| 664 |                         $this->game->getClassType() . '/' . $this->game->nextStep('index'), | 
            ||
| 665 |                         array('id' => $this->game->getIdentifier()) | 
            ||
| 666 | )  | 
            ||
| 667 | );  | 
            ||
| 668 | }  | 
            ||
| 669 | }  | 
            ||
| 670 | |||
| 671 | $form->setAttribute(  | 
            ||
| 672 | 'action',  | 
            ||
| 673 | $this->frontendUrl()->fromRoute(  | 
            ||
| 674 | $this->game->getClassType().'/login',  | 
            ||
| 675 |                 array('id' => $this->game->getIdentifier()) | 
            ||
| 676 | )  | 
            ||
| 677 | );  | 
            ||
| 678 | $viewModel = $this->buildView($this->game);  | 
            ||
| 679 | $viewModel->setVariables(array(  | 
            ||
| 680 | 'form' => $form,  | 
            ||
| 681 | ));  | 
            ||
| 682 | return $viewModel;  | 
            ||
| 683 | }  | 
            ||
| 684 | |||
| 685 | public function userregisterAction()  | 
            ||
| 686 |     { | 
            ||
| 687 |         $userOptions = $this->getServiceLocator()->get('zfcuser_module_options'); | 
            ||
| 688 | |||
| 689 |         if ($this->zfcUserAuthentication()->hasIdentity()) { | 
            ||
| 690 | return $this->redirect()->toUrl(  | 
            ||
| 691 | $this->frontendUrl()->fromRoute(  | 
            ||
| 692 |                     $this->game->getClassType().'/'.$this->game->nextStep('index'), | 
            ||
| 693 |                     array('id' => $this->game->getIdentifier()) | 
            ||
| 694 | )  | 
            ||
| 695 | );  | 
            ||
| 696 | }  | 
            ||
| 697 | $request = $this->getRequest();  | 
            ||
| 698 |         $service = $this->getServiceLocator()->get('zfcuser_user_service'); | 
            ||
| 699 |         $form = $this->getServiceLocator()->get('playgroundgame_register_form'); | 
            ||
| 700 |         $socialnetwork = $this->params()->fromRoute('socialnetwork', false); | 
            ||
| 701 | $form->setAttribute(  | 
            ||
| 702 | 'action',  | 
            ||
| 703 | $this->frontendUrl()->fromRoute(  | 
            ||
| 704 | $this->game->getClassType().'/user-register',  | 
            ||
| 705 |                 array('id' => $this->game->getIdentifier()) | 
            ||
| 706 | )  | 
            ||
| 707 | );  | 
            ||
| 708 | $params = array();  | 
            ||
| 709 | $socialCredentials = array();  | 
            ||
| 710 | |||
| 711 |         if ($userOptions->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) { | 
            ||
| 712 |             $redirect = $request->getQuery()->get('redirect'); | 
            ||
| 713 |         } else { | 
            ||
| 714 | $redirect = false;  | 
            ||
| 715 | }  | 
            ||
| 716 | |||
| 717 |         if ($socialnetwork) { | 
            ||
| 718 | $infoMe = $this->getProviderService()->getInfoMe($socialnetwork);  | 
            ||
| 719 | |||
| 720 |             if (!empty($infoMe)) { | 
            ||
| 721 | $user = $this->getProviderService()->getUserProviderMapper()->findUserByProviderId(  | 
            ||
| 722 | $infoMe->identifier,  | 
            ||
| 723 | $socialnetwork  | 
            ||
| 724 | );  | 
            ||
| 725 | |||
| 726 |                 if ($user || $service->getOptions()->getCreateUserAutoSocial() === true) { | 
            ||
| 727 | //on le dirige vers l'action d'authentification  | 
            ||
| 728 |                     if (! $redirect && $userOptions->getLoginRedirectRoute() != '') { | 
            ||
| 729 | $redirect = $this->frontendUrl()->fromRoute(  | 
            ||
| 730 | $this->game->getClassType().'/login',  | 
            ||
| 731 |                             array('id' => $this->game->getIdentifier()) | 
            ||
| 732 | );  | 
            ||
| 733 | }  | 
            ||
| 734 | $redir = $this->frontendUrl()->fromRoute(  | 
            ||
| 735 | $this->game->getClassType().'/login',  | 
            ||
| 736 |                         array('id' => $this->game->getIdentifier()) | 
            ||
| 737 | ) .'/' . $socialnetwork . ($redirect ? '?redirect=' . $redirect : '');  | 
            ||
| 738 | |||
| 739 | return $this->redirect()->toUrl($redir);  | 
            ||
| 740 | }  | 
            ||
| 741 | |||
| 742 | // Je retire la saisie du login/mdp  | 
            ||
| 743 | $form->setAttribute(  | 
            ||
| 744 | 'action',  | 
            ||
| 745 | $this->frontendUrl()->fromRoute(  | 
            ||
| 746 | $this->game->getClassType().'/user-register',  | 
            ||
| 747 | array(  | 
            ||
| 748 | 'id' => $this->game->getIdentifier(),  | 
            ||
| 749 | 'socialnetwork' => $socialnetwork,  | 
            ||
| 750 | |||
| 751 | )  | 
            ||
| 752 | )  | 
            ||
| 753 | );  | 
            ||
| 754 |                 $form->remove('password'); | 
            ||
| 755 |                 $form->remove('passwordVerify'); | 
            ||
| 756 | |||
| 757 | $birthMonth = $infoMe->birthMonth;  | 
            ||
| 758 |                 if (strlen($birthMonth) <= 1) { | 
            ||
| 759 | $birthMonth = '0'.$birthMonth;  | 
            ||
| 760 | }  | 
            ||
| 761 | $birthDay = $infoMe->birthDay;  | 
            ||
| 762 |                 if (strlen($birthDay) <= 1) { | 
            ||
| 763 | $birthDay = '0'.$birthDay;  | 
            ||
| 764 | }  | 
            ||
| 765 | |||
| 766 | $gender = $infoMe->gender;  | 
            ||
| 767 |                 if ($gender == 'female') { | 
            ||
| 768 | $title = 'Me';  | 
            ||
| 769 |                 } else { | 
            ||
| 770 | $title = 'M';  | 
            ||
| 771 | }  | 
            ||
| 772 | |||
| 773 | $params = array(  | 
            ||
| 774 | //'birth_year' => $infoMe->birthYear,  | 
            ||
| 775 | 'title' => $title,  | 
            ||
| 776 | 'dob' => $birthDay.'/'.$birthMonth.'/'.$infoMe->birthYear,  | 
            ||
| 777 | 'firstname' => $infoMe->firstName,  | 
            ||
| 778 | 'lastname' => $infoMe->lastName,  | 
            ||
| 779 | 'email' => $infoMe->email,  | 
            ||
| 780 | 'postalCode' => $infoMe->zip,  | 
            ||
| 781 | );  | 
            ||
| 782 | $socialCredentials = array(  | 
            ||
| 783 | 'socialNetwork' => strtolower($socialnetwork),  | 
            ||
| 784 | 'socialId' => $infoMe->identifier,  | 
            ||
| 785 | );  | 
            ||
| 786 | }  | 
            ||
| 787 | }  | 
            ||
| 788 | |||
| 789 | $redirectUrl = $this->frontendUrl()->fromRoute(  | 
            ||
| 790 | $this->game->getClassType().'/user-register',  | 
            ||
| 791 |             array('id' => $this->game->getIdentifier()) | 
            ||
| 792 | ) .($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : '');  | 
            ||
| 793 | $prg = $this->prg($redirectUrl, true);  | 
            ||
| 794 | |||
| 795 |         if ($prg instanceof Response) { | 
            ||
| 796 | return $prg;  | 
            ||
| 797 |         } elseif ($prg === false) { | 
            ||
| 798 | $form->setData($params);  | 
            ||
| 799 | $viewModel = $this->buildView($this->game);  | 
            ||
| 800 | $viewModel->setVariables(array(  | 
            ||
| 801 | 'registerForm' => $form,  | 
            ||
| 802 | 'enableRegistration' => $userOptions->getEnableRegistration(),  | 
            ||
| 803 | 'redirect' => $redirect,  | 
            ||
| 804 | ));  | 
            ||
| 805 | return $viewModel;  | 
            ||
| 806 | }  | 
            ||
| 807 | |||
| 808 | $post = $prg;  | 
            ||
| 809 | $post = array_merge(  | 
            ||
| 810 | $post,  | 
            ||
| 811 | $socialCredentials  | 
            ||
| 812 | );  | 
            ||
| 813 | |||
| 814 | $user = $service->register($post, 'playgroundgame_register_form');  | 
            ||
| 815 | |||
| 816 |         if (! $user) { | 
            ||
| 817 | $viewModel = $this->buildView($this->game);  | 
            ||
| 818 | $viewModel->setVariables(array(  | 
            ||
| 819 | 'registerForm' => $form,  | 
            ||
| 820 | 'enableRegistration' => $userOptions->getEnableRegistration(),  | 
            ||
| 821 | 'redirect' => $redirect,  | 
            ||
| 822 | ));  | 
            ||
| 823 | |||
| 824 | return $viewModel;  | 
            ||
| 825 | }  | 
            ||
| 826 | |||
| 827 |         if ($service->getOptions()->getEmailVerification()) { | 
            ||
| 828 |             $vm = new ViewModel(array('userEmail' => $user->getEmail())); | 
            ||
| 829 |             $vm->setTemplate('playground-user/register/registermail'); | 
            ||
| 830 | |||
| 831 | return $vm;  | 
            ||
| 832 |         } elseif ($service->getOptions()->getLoginAfterRegistration()) { | 
            ||
| 833 | $identityFields = $service->getOptions()->getAuthIdentityFields();  | 
            ||
| 834 |             if (in_array('email', $identityFields)) { | 
            ||
| 835 | $post['identity'] = $user->getEmail();  | 
            ||
| 836 |             } elseif (in_array('username', $identityFields)) { | 
            ||
| 837 | $post['identity'] = $user->getUsername();  | 
            ||
| 838 | }  | 
            ||
| 839 | $post['credential'] = isset($post['password'])?$post['password']:'';  | 
            ||
| 840 | $request->setPost(new Parameters($post));  | 
            ||
| 841 | |||
| 842 | // clear adapters  | 
            ||
| 843 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();  | 
            ||
| 844 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity();  | 
            ||
| 845 | |||
| 846 |             $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); | 
            ||
| 847 | |||
| 848 | View Code Duplication |             if ($logged) { | 
            |
| 849 | return $this->redirect()->toUrl(  | 
            ||
| 850 | $this->frontendUrl()->fromRoute(  | 
            ||
| 851 |                         $this->game->getClassType() . '/' . $this->game->nextStep('index'), | 
            ||
| 852 |                         array('id' => $this->game->getIdentifier()) | 
            ||
| 853 | )  | 
            ||
| 854 | );  | 
            ||
| 855 |             } else { | 
            ||
| 856 |                 $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( | 
            ||
| 857 | 'Authentication failed. Please try again.'  | 
            ||
| 858 | );  | 
            ||
| 859 | return $this->redirect()->toUrl(  | 
            ||
| 860 | $this->frontendUrl()->fromRoute(  | 
            ||
| 861 | $this->game->getClassType() . '/login',  | 
            ||
| 862 |                         array('id' => $this->game->getIdentifier()) | 
            ||
| 863 | )  | 
            ||
| 864 | );  | 
            ||
| 865 | }  | 
            ||
| 866 | }  | 
            ||
| 867 | |||
| 868 | $redirect = $this->frontendUrl()->fromRoute(  | 
            ||
| 869 | $this->game->getClassType().'/login',  | 
            ||
| 870 | array(  | 
            ||
| 871 | 'id' => $this->game->getIdentifier(),  | 
            ||
| 872 | |||
| 873 | )  | 
            ||
| 874 | ) . ($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : '');  | 
            ||
| 875 | |||
| 876 | return $this->redirect()->toUrl($redirect);  | 
            ||
| 877 | }  | 
            ||
| 878 | |||
| 879 | /**  | 
            ||
| 880 | *  | 
            ||
| 881 | * @param \PlaygroundGame\Entity\Game $game  | 
            ||
| 882 | * @param \PlaygroundUser\Entity\User $user  | 
            ||
| 883 | */  | 
            ||
| 884 | public function checkFbRegistration($user, $game)  | 
            ||
| 885 |     { | 
            ||
| 886 | $redirect = false;  | 
            ||
| 887 |         $session = new Container('facebook'); | 
            ||
| 888 |         if ($session->offsetExists('signed_request')) { | 
            ||
| 889 |             if (!$user) { | 
            ||
| 890 | // Get Playground user from Facebook info  | 
            ||
| 891 | $beforeLayout = $this->layout()->getTemplate();  | 
            ||
| 892 | $view = $this->forward()->dispatch(  | 
            ||
| 893 | 'playgrounduser_user',  | 
            ||
| 894 | array(  | 
            ||
| 895 | 'controller' => 'playgrounduser_user',  | 
            ||
| 896 | 'action' => 'registerFacebookUser',  | 
            ||
| 897 | 'provider' => 'facebook'  | 
            ||
| 898 | )  | 
            ||
| 899 | );  | 
            ||
| 900 | |||
| 901 | $this->layout()->setTemplate($beforeLayout);  | 
            ||
| 902 | $user = $view->user;  | 
            ||
| 903 | |||
| 904 | // If the user can not be created/retrieved from Facebook info, redirect to login/register form  | 
            ||
| 905 |                 if (!$user) { | 
            ||
| 906 | $redirectUrl = urlencode(  | 
            ||
| 907 | $this->frontendUrl()->fromRoute(  | 
            ||
| 908 | $game->getClassType() .'/play',  | 
            ||
| 909 |                             array('id' => $game->getIdentifier()), | 
            ||
| 910 |                             array('force_canonical' => true) | 
            ||
| 911 | )  | 
            ||
| 912 | );  | 
            ||
| 913 | $redirect = $this->redirect()->toUrl(  | 
            ||
| 914 | $this->frontendUrl()->fromRoute(  | 
            ||
| 915 | 'zfcuser/register'  | 
            ||
| 916 | ) . '?redirect='.$redirectUrl  | 
            ||
| 917 | );  | 
            ||
| 918 | }  | 
            ||
| 919 | }  | 
            ||
| 920 | |||
| 921 |             if ($game->getFbFan()) { | 
            ||
| 922 |                 if ($this->getGameService()->checkIsFan($game) === false) { | 
            ||
| 923 | $redirect = $this->redirect()->toRoute(  | 
            ||
| 924 | $game->getClassType().'/fangate',  | 
            ||
| 925 |                         array('id' => $game->getIdentifier()) | 
            ||
| 926 | );  | 
            ||
| 927 | }  | 
            ||
| 928 | }  | 
            ||
| 929 | }  | 
            ||
| 930 | |||
| 931 | return $redirect;  | 
            ||
| 932 | }  | 
            ||
| 933 | |||
| 934 | /**  | 
            ||
| 935 | * This method create the basic Game view  | 
            ||
| 936 | * @param \PlaygroundGame\Entity\Game $game  | 
            ||
| 937 | */  | 
            ||
| 938 | public function buildView($game)  | 
            ||
| 939 |     { | 
            ||
| 940 |         if ($this->getRequest()->isXmlHttpRequest()) { | 
            ||
| 941 | $viewModel = new JsonModel();  | 
            ||
| 942 |             if ($game) { | 
            ||
| 943 | $view = $this->addAdditionalView($game);  | 
            ||
| 944 |                 if ($view && $view instanceof \Zend\View\Model\ViewModel) { | 
            ||
| 945 | $viewModel->setVariables($view->getVariables());  | 
            ||
| 946 | }  | 
            ||
| 947 | }  | 
            ||
| 948 |         } else { | 
            ||
| 949 | $viewModel = new ViewModel();  | 
            ||
| 950 | |||
| 951 |             if ($game) { | 
            ||
| 952 | $this->addMetaTitle($game);  | 
            ||
| 953 | $this->addMetaBitly();  | 
            ||
| 954 | $this->addGaEvent($game);  | 
            ||
| 955 | |||
| 956 | $this->customizeGameDesign($game);  | 
            ||
| 957 | |||
| 958 | // this is possible to create a specific game design in /design/frontend/default/custom.  | 
            ||
| 959 | //It will precede all others templates.  | 
            ||
| 960 |                 $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); | 
            ||
| 961 | $l = $templatePathResolver->getPaths();  | 
            ||
| 962 | $templatePathResolver->addPath($l[0].'custom/'.$game->getIdentifier());  | 
            ||
| 963 | |||
| 964 | $view = $this->addAdditionalView($game);  | 
            ||
| 965 |                 if ($view && $view instanceof \Zend\View\Model\ViewModel) { | 
            ||
| 966 | $viewModel->addChild($view, 'additional');  | 
            ||
| 967 |                 } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { | 
            ||
| 968 | return $view;  | 
            ||
| 969 | }  | 
            ||
| 970 | |||
| 971 | $this->layout()->setVariables(  | 
            ||
| 972 | array(  | 
            ||
| 973 |                         'action' => $this->params('action'), | 
            ||
| 974 | 'game' => $game,  | 
            ||
| 975 | 'flashMessages' => $this->flashMessenger()->getMessages(),  | 
            ||
| 976 | |||
| 977 | )  | 
            ||
| 978 | );  | 
            ||
| 979 | }  | 
            ||
| 980 | }  | 
            ||
| 981 | |||
| 982 |         if ($game) { | 
            ||
| 983 | $viewModel->setVariables($this->getShareData($game));  | 
            ||
| 984 |             $viewModel->setVariables(array('game' => $game)); | 
            ||
| 985 | }  | 
            ||
| 986 | |||
| 987 | return $viewModel;  | 
            ||
| 988 | }  | 
            ||
| 989 | |||
| 990 | /**  | 
            ||
| 991 | * @param \PlaygroundGame\Entity\Game $game  | 
            ||
| 992 | */  | 
            ||
| 993 | public function addAdditionalView($game)  | 
            ||
| 994 |     { | 
            ||
| 995 | $view = false;  | 
            ||
| 996 | |||
| 997 |         $actionName = $this->getEvent()->getRouteMatch()->getParam('action', 'not-found'); | 
            ||
| 998 | $stepsViews = json_decode($game->getStepsViews(), true);  | 
            ||
| 999 |         if ($stepsViews && isset($stepsViews[$actionName])) { | 
            ||
| 1000 | $beforeLayout = $this->layout()->getTemplate();  | 
            ||
| 1001 | $actionData = $stepsViews[$actionName];  | 
            ||
| 1002 |             if (is_string($actionData)) { | 
            ||
| 1003 | $action = $actionData;  | 
            ||
| 1004 |                 $controller = $this->getEvent()->getRouteMatch()->getParam('controller', 'playgroundgame_game'); | 
            ||
| 1005 | $view = $this->forward()->dispatch(  | 
            ||
| 1006 | $controller,  | 
            ||
| 1007 | array(  | 
            ||
| 1008 | 'action' => $action,  | 
            ||
| 1009 | 'id' => $game->getIdentifier()  | 
            ||
| 1010 | )  | 
            ||
| 1011 | );  | 
            ||
| 1012 |             } elseif (is_array($actionData) && count($actionData)>0) { | 
            ||
| 1013 | $action = key($actionData);  | 
            ||
| 1014 | $controller = $actionData[$action];  | 
            ||
| 1015 | $view = $this->forward()->dispatch(  | 
            ||
| 1016 | $controller,  | 
            ||
| 1017 | array(  | 
            ||
| 1018 | 'action' => $action,  | 
            ||
| 1019 | 'id' => $game->getIdentifier()  | 
            ||
| 1020 | )  | 
            ||
| 1021 | );  | 
            ||
| 1022 | }  | 
            ||
| 1023 | // suite au forward, le template de layout a changé, je dois le rétablir...  | 
            ||
| 1024 | $this->layout()->setTemplate($beforeLayout);  | 
            ||
| 1025 | }  | 
            ||
| 1026 | |||
| 1027 | return $view;  | 
            ||
| 1028 | }  | 
            ||
| 1029 | |||
| 1030 | public function addMetaBitly()  | 
            ||
| 1031 |     { | 
            ||
| 1032 | $bitlyclient = $this->getOptions()->getBitlyUrl();  | 
            ||
| 1033 | $bitlyuser = $this->getOptions()->getBitlyUsername();  | 
            ||
| 1034 | $bitlykey = $this->getOptions()->getBitlyApiKey();  | 
            ||
| 1035 | |||
| 1036 |         $this->getViewHelper('HeadMeta')->setProperty('bt:client', $bitlyclient); | 
            ||
| 1037 |         $this->getViewHelper('HeadMeta')->setProperty('bt:user', $bitlyuser); | 
            ||
| 1038 |         $this->getViewHelper('HeadMeta')->setProperty('bt:key', $bitlykey); | 
            ||
| 1039 | }  | 
            ||
| 1040 | |||
| 1041 | /**  | 
            ||
| 1042 | * @param \PlaygroundGame\Entity\Game $game  | 
            ||
| 1043 | */  | 
            ||
| 1044 | public function addGaEvent($game)  | 
            ||
| 1045 |     { | 
            ||
| 1046 | // Google Analytics event  | 
            ||
| 1047 |         $ga = $this->getServiceLocator()->get('google-analytics'); | 
            ||
| 1048 |         $event = new \PlaygroundCore\Analytics\Event($game->getClassType(), $this->params('action')); | 
            ||
| 1049 | $event->setLabel($game->getTitle());  | 
            ||
| 1050 | $ga->addEvent($event);  | 
            ||
| 1051 | }  | 
            ||
| 1052 | |||
| 1053 | /**  | 
            ||
| 1054 | * @param \PlaygroundGame\Entity\Game $game  | 
            ||
| 1055 | */  | 
            ||
| 1056 | public function addMetaTitle($game)  | 
            ||
| 1076 | |||
| 1077 | /**  | 
            ||
| 1078 | * @param \PlaygroundGame\Entity\Game $game  | 
            ||
| 1079 | */  | 
            ||
| 1080 | public function customizeGameDesign($game)  | 
            ||
| 1095 | |||
| 1096 | /**  | 
            ||
| 1097 | * @param \PlaygroundGame\Entity\Game $game  | 
            ||
| 1098 | */  | 
            ||
| 1099 | public function getShareData($game)  | 
            ||
| 1185 | |||
| 1186 | /**  | 
            ||
| 1187 | * return ajax response in json format  | 
            ||
| 1188 | *  | 
            ||
| 1189 | * @param array $data  | 
            ||
| 1190 | * @return \Zend\View\Model\JsonModel  | 
            ||
| 1191 | */  | 
            ||
| 1192 | View Code Duplication | protected function successJson($data = null)  | 
            |
| 1200 | |||
| 1201 | /**  | 
            ||
| 1202 | * return ajax response in json format  | 
            ||
| 1203 | *  | 
            ||
| 1204 | * @param string $message  | 
            ||
| 1205 | * @return \Zend\View\Model\JsonModel  | 
            ||
| 1206 | */  | 
            ||
| 1207 | View Code Duplication | protected function errorJson($message = null)  | 
            |
| 1215 | |||
| 1216 | /**  | 
            ||
| 1217 | * @param string $helperName  | 
            ||
| 1218 | */  | 
            ||
| 1219 | protected function getViewHelper($helperName)  | 
            ||
| 1223 | |||
| 1224 | public function getGameService()  | 
            ||
| 1232 | |||
| 1233 | public function setGameService(GameService $gameService)  | 
            ||
| 1234 |     { | 
            ||
| 1235 | $this->gameService = $gameService;  | 
            ||
| 1236 | |||
| 1237 | return $this;  | 
            ||
| 1238 | }  | 
            ||
| 1239 | |||
| 1240 | public function getPrizeService()  | 
            ||
| 1241 |     { | 
            ||
| 1242 |         if (!$this->prizeService) { | 
            ||
| 1243 |             $this->prizeService = $this->getServiceLocator()->get('playgroundgame_prize_service'); | 
            ||
| 1244 | }  | 
            ||
| 1245 | |||
| 1246 | return $this->prizeService;  | 
            ||
| 1247 | }  | 
            ||
| 1248 | |||
| 1249 | public function setPrizeService(PrizeService $prizeService)  | 
            ||
| 1250 |     { | 
            ||
| 1251 | $this->prizeService = $prizeService;  | 
            ||
| 1252 | |||
| 1253 | return $this;  | 
            ||
| 1254 | }  | 
            ||
| 1255 | |||
| 1256 | public function getOptions()  | 
            ||
| 1264 | |||
| 1265 | public function setOptions($options)  | 
            ||
| 1271 | }  | 
            ||
| 1272 | 
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: