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 | 'ajaxforgot', |
||
44 | 'play', |
||
45 | 'result', |
||
46 | 'preview', |
||
47 | 'list' |
||
48 | ); |
||
49 | |||
50 | protected $withOnlineGame = array( |
||
51 | 'leaderboard', |
||
52 | 'register', |
||
53 | 'bounce', |
||
54 | 'play', |
||
55 | 'result' |
||
56 | ); |
||
57 | |||
58 | protected $withAnyUser = array( |
||
59 | 'share', |
||
60 | 'result', |
||
61 | 'play' |
||
62 | ); |
||
63 | |||
64 | public function setEventManager(\Zend\EventManager\EventManagerInterface $events) |
||
65 | { |
||
66 | parent::setEventManager($events); |
||
67 | |||
68 | $controller = $this; |
||
69 | $events->attach('dispatch', function (\Zend\Mvc\MvcEvent $e) use ($controller) { |
||
70 | |||
71 | $identifier = $e->getRouteMatch()->getParam('id'); |
||
72 | $controller->game = $controller->getGameService()->checkGame($identifier, false); |
||
73 | if (!$controller->game && |
||
74 | in_array($controller->params('action'), $controller->withGame) |
||
75 | ) { |
||
76 | return $controller->notFoundAction(); |
||
77 | } |
||
78 | |||
79 | if ($controller->game && |
||
80 | $controller->game->isClosed() && |
||
81 | in_array($controller->params('action'), $controller->withOnlineGame) |
||
82 | ) { |
||
83 | return $controller->notFoundAction(); |
||
84 | } |
||
85 | |||
86 | if ($controller->game) { |
||
87 | // this is possible to create a specific game design in /design/frontend/default/custom. |
||
88 | //It will precede all others templates. |
||
89 | $templatePathResolver = $controller->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
90 | $l = $templatePathResolver->getPaths(); |
||
91 | $templatePathResolver->addPath($l[0].'custom/'.$controller->game->getIdentifier()); |
||
92 | } |
||
93 | |||
94 | $controller->user = $controller->zfcUserAuthentication()->getIdentity(); |
||
|
|||
95 | if ($controller->game && |
||
96 | !$controller->user && |
||
97 | !$controller->game->getAnonymousAllowed() && |
||
98 | in_array($controller->params('action'), $controller->withAnyUser) |
||
99 | ) { |
||
100 | $redirect = urlencode( |
||
101 | $controller->url()->fromRoute( |
||
102 | 'frontend/'.$controller->game->getClassType() . '/' . $controller->params('action'), |
||
103 | array('id' => $controller->game->getIdentifier()), |
||
104 | array('force_canonical' => true) |
||
105 | ) |
||
106 | ); |
||
107 | |||
108 | $urlRegister = $controller->url()->fromRoute( |
||
109 | 'frontend/zfcuser/register', |
||
110 | array(), |
||
111 | array('force_canonical' => true) |
||
112 | ) . '?redirect='.$redirect; |
||
113 | |||
114 | // code permettant d'identifier un custom game |
||
115 | // ligne $config = $controller->getGameService()->getServiceManager()->get('config'); |
||
116 | // ligne $customUrl = str_replace('frontend.', '', $e->getRouteMatch()->getParam('area', '')); |
||
117 | // ligne if ($config['custom_games'][$controller->game->getIdentifier()] && |
||
118 | // ligne $controller->getRequest()->getUri()->getHost() === $customUrl |
||
119 | // ligne ) { |
||
120 | return $controller->redirect()->toUrl($urlRegister); |
||
121 | } |
||
122 | |||
123 | return; |
||
124 | }, 100); // execute before executing action logic |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Action called if matched action does not exist |
||
129 | * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy |
||
130 | * it has to be rendered in the controller. Hence the code below. |
||
131 | * |
||
132 | * This action is injected as a catchall action for each custom_games definition |
||
133 | * This way, when a custom_game is created, the 404 is it's responsability and the |
||
134 | * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml |
||
135 | * |
||
136 | * |
||
137 | * @return \Zend\Stdlib\ResponseInterface |
||
138 | */ |
||
139 | public function notFoundAction() |
||
140 | { |
||
141 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
142 | |||
143 | // I create a template path in which I can find a custom template |
||
144 | $controller = explode('\\', get_class($this)); |
||
145 | $controllerPath = str_replace('Controller', '', end($controller)); |
||
146 | $controllerPath = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '-\\1', $controllerPath)); |
||
147 | $template = 'playground-game/'.$controllerPath . '/custom' .$this->getRequest()->getUri()->getPath(); |
||
148 | |||
149 | if (false === $templatePathResolver->resolve($template)) { |
||
150 | $viewRender = $this->getServiceLocator()->get('ViewRenderer'); |
||
151 | |||
152 | $this->getEvent()->getRouteMatch()->setParam('action', 'not-found'); |
||
153 | $this->response->setStatusCode(404); |
||
154 | |||
155 | $res = 'error/404'; |
||
156 | |||
157 | $viewModel = $this->buildView($this->game); |
||
158 | $viewModel->setTemplate($res); |
||
159 | |||
160 | $this->layout()->setVariable("content", $viewRender->render($viewModel)); |
||
161 | $this->response->setContent($viewRender->render($this->layout())); |
||
162 | |||
163 | return $this->response; |
||
164 | } |
||
165 | |||
166 | $viewModel = $this->buildView($this->game); |
||
167 | $viewModel->setTemplate($template); |
||
168 | |||
169 | return $viewModel; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * This action acts as a hub : Depending on the first step of the game, it will forward the action to this step |
||
174 | */ |
||
175 | public function homeAction() |
||
176 | { |
||
177 | // This fix exists only for safari in FB on Windows : we need to redirect the user to the page |
||
178 | // outside of iframe for the cookie to be accepted. PlaygroundCore redirects to the FB Iframed page when |
||
179 | // it discovers that the user arrives for the first time on the game in FB. |
||
180 | // When core redirects, it adds a 'redir_fb_page_id' var in the querystring |
||
181 | // Here, we test if this var exist, and then send the user back to the game in FB. |
||
182 | // Now the cookie will be accepted by Safari... |
||
183 | $pageId = $this->params()->fromQuery('redir_fb_page_id'); |
||
184 | if (!empty($pageId)) { |
||
185 | $appId = 'app_'.$this->game->getFbAppId(); |
||
186 | $url = '//www.facebook.com/pages/game/'.$pageId.'?sk='.$appId; |
||
187 | |||
188 | return $this->redirect()->toUrl($url); |
||
189 | } |
||
190 | |||
191 | // If an entry has already been done during this session, I reset the anonymous_identifier cookie |
||
192 | // so that another person can play the same game (if game conditions are fullfilled) |
||
193 | $session = new Container('anonymous_identifier'); |
||
194 | if ($session->offsetExists('anonymous_identifier')) { |
||
195 | $session->offsetUnset('anonymous_identifier'); |
||
196 | } |
||
197 | |||
198 | return $this->forward()->dispatch( |
||
199 | 'playgroundgame_'.$this->game->getClassType(), |
||
200 | array( |
||
201 | 'controller' => 'playgroundgame_'.$this->game->getClassType(), |
||
202 | 'action' => $this->game->firstStep(), |
||
203 | 'id' => $this->game->getIdentifier() |
||
204 | ) |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Homepage of the game |
||
210 | */ |
||
211 | public function indexAction() |
||
212 | { |
||
213 | $isSubscribed = false; |
||
214 | |||
215 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user); |
||
216 | if ($entry) { |
||
217 | $isSubscribed = true; |
||
218 | } |
||
219 | |||
220 | $viewModel = $this->buildView($this->game); |
||
221 | $viewModel->setVariables(array( |
||
222 | 'isSubscribed' => $isSubscribed |
||
223 | )); |
||
224 | |||
225 | return $viewModel; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * leaderboardAction |
||
230 | * |
||
231 | * @return ViewModel $viewModel |
||
232 | */ |
||
233 | public function leaderboardAction() |
||
255 | |||
256 | /** |
||
257 | * This action has been designed to be called by other controllers |
||
258 | * It gives the ability to display an information form and persist it in the game entry |
||
259 | * |
||
260 | * @return \Zend\View\Model\ViewModel |
||
261 | */ |
||
262 | public function registerAction() |
||
263 | { |
||
264 | $form = $this->getGameService()->createFormFromJson($this->game->getPlayerForm()->getForm(), 'playerForm'); |
||
265 | |||
266 | if ($this->getRequest()->isPost()) { |
||
267 | // POST Request: Process form |
||
268 | $data = array_merge_recursive( |
||
269 | $this->getRequest()->getPost()->toArray(), |
||
270 | $this->getRequest()->getFiles()->toArray() |
||
271 | ); |
||
272 | |||
273 | $form->setData($data); |
||
274 | |||
275 | if ($form->isValid()) { |
||
276 | // steps of the game |
||
277 | $steps = $this->game->getStepsArray(); |
||
278 | // sub steps of the game |
||
279 | $viewSteps = $this->game->getStepsViewsArray(); |
||
280 | |||
281 | // register position |
||
282 | $key = array_search($this->params('action'), $viewSteps); |
||
283 | View Code Duplication | if (!$key) { |
|
284 | // register is not a substep of the game so it's a step |
||
285 | $key = array_search($this->params('action'), $steps); |
||
286 | $keyStep = true; |
||
287 | } else { |
||
288 | // register was a substep, i search the index of its parent |
||
289 | $key = array_search($key, $steps); |
||
290 | $keyStep = false; |
||
291 | } |
||
292 | |||
293 | // play position |
||
294 | $keyplay = array_search('play', $viewSteps); |
||
295 | |||
296 | View Code Duplication | if (!$keyplay) { |
|
297 | // play is not a substep, so it's a step |
||
298 | $keyplay = array_search('play', $steps); |
||
299 | $keyplayStep = true; |
||
300 | } else { |
||
301 | // play is a substep so I search the index of its parent |
||
302 | $keyplay = array_search($keyplay, $steps); |
||
303 | $keyplayStep = false; |
||
304 | } |
||
305 | |||
306 | // If register step before play, I don't have no entry yet. I have to create one |
||
307 | // If register after play step, I search for the last entry created by play step. |
||
308 | |||
309 | if ($key < $keyplay || ($keyStep && !$keyplayStep && $key <= $keyplay)) { |
||
310 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
311 | if (!$entry) { |
||
312 | // the user has already taken part of this game and the participation limit has been reached |
||
313 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
314 | |||
315 | return $this->redirect()->toUrl( |
||
316 | $this->frontendUrl()->fromRoute( |
||
317 | $this->game->getClassType().'/result', |
||
318 | array( |
||
319 | 'id' => $this->game->getIdentifier(), |
||
320 | |||
321 | ) |
||
322 | ) |
||
323 | ); |
||
324 | } |
||
325 | } else { |
||
326 | // I'm looking for an entry without anonymousIdentifier (the active entry in fact). |
||
327 | $entry = $this->getGameService()->findLastEntry($this->game, $this->user); |
||
328 | if ($this->getGameService()->hasReachedPlayLimit($this->game, $this->user)) { |
||
329 | // the user has already taken part of this game and the participation limit has been reached |
||
330 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
331 | |||
332 | return $this->redirect()->toUrl( |
||
333 | $this->frontendUrl()->fromRoute( |
||
334 | $this->game->getClassType().'/result', |
||
335 | array( |
||
336 | 'id' => $this->game->getIdentifier(), |
||
337 | |||
338 | ) |
||
339 | ) |
||
340 | ); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | $this->getGameService()->updateEntryPlayerForm($form->getData(), $this->game, $this->user, $entry); |
||
345 | |||
346 | if (!empty($this->game->nextStep($this->params('action')))) { |
||
347 | return $this->redirect()->toUrl( |
||
348 | $this->frontendUrl()->fromRoute( |
||
349 | $this->game->getClassType() .'/' . $this->game->nextStep($this->params('action')), |
||
350 | array('id' => $this->game->getIdentifier()), |
||
351 | array('force_canonical' => true) |
||
352 | ) |
||
353 | ); |
||
354 | } |
||
355 | } |
||
356 | } |
||
357 | |||
358 | $viewModel = $this->buildView($this->game); |
||
359 | $viewModel->setVariables(array( |
||
360 | 'form' => $form |
||
361 | )); |
||
362 | |||
363 | return $viewModel; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * This action takes care of the terms of the game |
||
368 | */ |
||
369 | public function termsAction() |
||
370 | { |
||
371 | $viewModel = $this->buildView($this->game); |
||
372 | |||
373 | return $viewModel; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * This action takes care of the conditions of the game |
||
378 | */ |
||
379 | public function conditionsAction() |
||
380 | { |
||
381 | $viewModel = $this->buildView($this->game); |
||
382 | |||
383 | return $viewModel; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * This action takes care of bounce page of the game |
||
388 | */ |
||
389 | public function bounceAction() |
||
390 | { |
||
391 | $availableGames = $this->getGameService()->getAvailableGames($this->user); |
||
392 | |||
393 | $rssUrl = ''; |
||
394 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
395 | if (isset($config['rss']['url'])) { |
||
396 | $rssUrl = $config['rss']['url']; |
||
397 | } |
||
398 | |||
399 | $viewModel = $this->buildView($this->game); |
||
400 | $viewModel->setVariables(array( |
||
401 | 'rssUrl' => $rssUrl, |
||
402 | 'user' => $this->user, |
||
403 | 'availableGames' => $availableGames, |
||
404 | )); |
||
405 | |||
406 | return $viewModel; |
||
407 | } |
||
408 | |||
409 | |||
410 | /** |
||
411 | * This action displays the Prizes page associated to the game |
||
412 | */ |
||
413 | public function prizesAction() |
||
414 | { |
||
415 | if (count($this->game->getPrizes()) == 0) { |
||
416 | return $this->notFoundAction(); |
||
417 | } |
||
418 | |||
419 | $viewModel = $this->buildView($this->game); |
||
420 | |||
421 | return $viewModel; |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * This action displays a specific Prize page among those associated to the game |
||
426 | */ |
||
427 | public function prizeAction() |
||
428 | { |
||
429 | $prizeIdentifier = $this->getEvent()->getRouteMatch()->getParam('prize'); |
||
430 | $prize = $this->getPrizeService()->getPrizeMapper()->findByIdentifier($prizeIdentifier); |
||
431 | |||
432 | if (!$prize) { |
||
433 | return $this->notFoundAction(); |
||
434 | } |
||
435 | |||
436 | $viewModel = $this->buildView($this->game); |
||
437 | $viewModel->setVariables(array('prize'=> $prize)); |
||
438 | |||
439 | return $viewModel; |
||
440 | } |
||
441 | |||
442 | public function gameslistAction() |
||
443 | { |
||
444 | $layoutViewModel = $this->layout(); |
||
445 | |||
446 | $slider = new ViewModel(); |
||
447 | $slider->setTemplate('playground-game/common/top_promo'); |
||
448 | |||
449 | $sliderItems = $this->getGameService()->getActiveSliderGames(); |
||
450 | |||
451 | $slider->setVariables(array('sliderItems' => $sliderItems)); |
||
452 | |||
453 | $layoutViewModel->addChild($slider, 'slider'); |
||
454 | |||
455 | $games = $this->getGameService()->getActiveGames(false, '', 'endDate'); |
||
456 | if (is_array($games)) { |
||
457 | $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($games)); |
||
458 | } else { |
||
459 | $paginator = $games; |
||
460 | } |
||
461 | |||
462 | $paginator->setItemCountPerPage(7); |
||
463 | $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
||
464 | |||
465 | $bitlyclient = $this->getOptions()->getBitlyUrl(); |
||
466 | $bitlyuser = $this->getOptions()->getBitlyUsername(); |
||
467 | $bitlykey = $this->getOptions()->getBitlyApiKey(); |
||
468 | |||
469 | $this->getViewHelper('HeadMeta')->setProperty('bt:client', $bitlyclient); |
||
470 | $this->getViewHelper('HeadMeta')->setProperty('bt:user', $bitlyuser); |
||
471 | $this->getViewHelper('HeadMeta')->setProperty('bt:key', $bitlykey); |
||
472 | |||
473 | $this->layout()->setVariables( |
||
474 | array( |
||
475 | 'sliderItems' => $sliderItems, |
||
476 | 'currentPage' => array( |
||
477 | 'pageGames' => 'games', |
||
478 | 'pageWinners' => '' |
||
479 | ), |
||
480 | ) |
||
481 | ); |
||
482 | |||
483 | return new ViewModel( |
||
484 | array( |
||
485 | 'games' => $paginator |
||
486 | ) |
||
487 | ); |
||
488 | } |
||
489 | |||
490 | public function fangateAction() |
||
491 | { |
||
492 | $viewModel = $this->buildView($this->game); |
||
493 | |||
494 | return $viewModel; |
||
495 | } |
||
496 | |||
497 | public function shareAction() |
||
498 | { |
||
499 | $statusMail = null; |
||
500 | $lastEntry = null; |
||
501 | |||
502 | // steps of the game |
||
503 | $steps = $this->game->getStepsArray(); |
||
504 | // sub steps of the game |
||
505 | $viewSteps = $this->game->getStepsViewsArray(); |
||
506 | |||
507 | // share position |
||
508 | $key = array_search($this->params('action'), $viewSteps); |
||
509 | View Code Duplication | if (!$key) { |
|
510 | // share is not a substep of the game so it's a step |
||
511 | $key = array_search($this->params('action'), $steps); |
||
512 | $keyStep = true; |
||
513 | } else { |
||
514 | // share was a substep, I search the index of its parent |
||
515 | $key = array_search($key, $steps); |
||
516 | $keyStep = false; |
||
517 | } |
||
518 | |||
519 | // play position |
||
520 | $keyplay = array_search('play', $viewSteps); |
||
521 | |||
522 | View Code Duplication | if (!$keyplay) { |
|
523 | // play is not a substep, so it's a step |
||
524 | $keyplay = array_search('play', $steps); |
||
525 | $keyplayStep = true; |
||
526 | } else { |
||
527 | // play is a substep so I search the index of its parent |
||
528 | $keyplay = array_search($keyplay, $steps); |
||
529 | $keyplayStep = false; |
||
530 | } |
||
531 | |||
532 | if ($key && $keyplay && $keyplay <= $key) { |
||
533 | // Has the user finished the game ? |
||
534 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); |
||
535 | |||
536 | View Code Duplication | if ($lastEntry === null) { |
|
537 | return $this->redirect()->toUrl( |
||
538 | $this->frontendUrl()->fromRoute( |
||
539 | $this->game->getClassType(), |
||
540 | array('id' => $this->game->getIdentifier()) |
||
541 | ) |
||
542 | ); |
||
543 | } |
||
544 | } |
||
545 | |||
546 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
547 | $form->setAttribute('method', 'post'); |
||
548 | |||
549 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
550 | $viewModel = $this->buildView($this->game); |
||
551 | |||
552 | if ($this->getRequest()->isPost()) { |
||
553 | $data = $this->getRequest()->getPost()->toArray(); |
||
554 | $form->setData($data); |
||
555 | View Code Duplication | if ($form->isValid()) { |
|
556 | $result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); |
||
557 | if ($result) { |
||
558 | $statusMail = true; |
||
559 | } |
||
560 | } |
||
561 | } |
||
562 | |||
563 | $viewModel->setVariables(array( |
||
564 | 'statusMail' => $statusMail, |
||
565 | 'form' => $form, |
||
566 | )); |
||
567 | |||
568 | return $viewModel; |
||
569 | } |
||
570 | |||
571 | View Code Duplication | public function fbshareAction() |
|
591 | |||
592 | public function fbrequestAction() |
||
593 | { |
||
594 | $viewModel = new ViewModel(); |
||
595 | $viewModel->setTerminal(true); |
||
596 | $fbId = $this->params()->fromQuery('fbId'); |
||
597 | $to = $this->params()->fromQuery('to'); |
||
598 | |||
599 | if (!$this->game) { |
||
600 | return $this->errorJson(); |
||
601 | } |
||
602 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user); |
||
603 | if (! $entry) { |
||
604 | return $this->errorJson(); |
||
605 | } |
||
606 | if (!$fbId) { |
||
607 | return $this->errorJson(); |
||
608 | } |
||
609 | |||
610 | $this->getGameService()->postFbRequest($fbId, $this->game, $this->user, $entry, $to); |
||
611 | |||
612 | return $this->successJson(); |
||
613 | } |
||
614 | |||
615 | public function tweetAction() |
||
634 | |||
635 | View Code Duplication | public function googleAction() |
|
636 | { |
||
637 | $viewModel = new ViewModel(); |
||
638 | $viewModel->setTerminal(true); |
||
639 | $googleId = $this->params()->fromQuery('googleId'); |
||
640 | |||
641 | if (!$this->game) { |
||
642 | return $this->errorJson(); |
||
643 | } |
||
644 | $entry = $this->getGameService()->checkExistingEntry($this->game, $this->user); |
||
645 | if (! $entry) { |
||
646 | return $this->errorJson(); |
||
647 | } |
||
648 | if (!$googleId) { |
||
649 | return $this->errorJson(); |
||
650 | } |
||
651 | |||
652 | $this->getGameService()->postGoogle($googleId, $this->game, $this->user, $entry); |
||
653 | |||
654 | return $this->successJson(); |
||
655 | } |
||
656 | |||
657 | public function optinAction() |
||
675 | |||
676 | public function loginAction() |
||
677 | { |
||
678 | $request = $this->getRequest(); |
||
679 | $form = $this->getServiceLocator()->get('zfcuser_login_form'); |
||
680 | |||
681 | if ($request->isPost()) { |
||
682 | $form->setData($request->getPost()); |
||
683 | |||
684 | if (!$form->isValid()) { |
||
685 | $this->flashMessenger()->addMessage( |
||
686 | 'Authentication failed. Please try again.' |
||
687 | ); |
||
688 | |||
689 | $viewModel = $this->buildView($this->game); |
||
690 | $viewModel->setVariables(array( |
||
691 | 'form' => $form, |
||
692 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
693 | )); |
||
694 | |||
695 | return $viewModel; |
||
696 | } |
||
697 | |||
698 | // clear adapters |
||
699 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
700 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
701 | |||
702 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
703 | |||
704 | if (!$logged) { |
||
705 | $this->flashMessenger()->addMessage( |
||
706 | 'Authentication failed. Please try again.' |
||
707 | ); |
||
708 | |||
709 | return $this->redirect()->toUrl( |
||
710 | $this->frontendUrl()->fromRoute( |
||
711 | $this->game->getClassType() . '/login', |
||
712 | array('id' => $this->game->getIdentifier()) |
||
713 | ) |
||
714 | ); |
||
715 | } else { |
||
716 | return $this->redirect()->toUrl( |
||
717 | $this->frontendUrl()->fromRoute( |
||
718 | $this->game->getClassType() . '/index', |
||
719 | array('id' => $this->game->getIdentifier()) |
||
720 | ) |
||
721 | ); |
||
722 | } |
||
723 | } |
||
724 | |||
725 | $form->setAttribute( |
||
726 | 'action', |
||
727 | $this->frontendUrl()->fromRoute( |
||
728 | $this->game->getClassType().'/login', |
||
729 | array('id' => $this->game->getIdentifier()) |
||
730 | ) |
||
731 | ); |
||
732 | $viewModel = $this->buildView($this->game); |
||
733 | $viewModel->setVariables(array( |
||
734 | 'form' => $form, |
||
735 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
736 | )); |
||
737 | return $viewModel; |
||
738 | } |
||
739 | |||
740 | public function userregisterAction() |
||
741 | { |
||
742 | $userOptions = $this->getServiceLocator()->get('zfcuser_module_options'); |
||
743 | |||
744 | if ($this->zfcUserAuthentication()->hasIdentity()) { |
||
745 | return $this->redirect()->toUrl( |
||
746 | $this->frontendUrl()->fromRoute( |
||
747 | $this->game->getClassType().'/'.$this->game->nextStep('index'), |
||
748 | array('id' => $this->game->getIdentifier()) |
||
749 | ) |
||
750 | ); |
||
751 | } |
||
752 | $request = $this->getRequest(); |
||
753 | $service = $this->getServiceLocator()->get('zfcuser_user_service'); |
||
754 | $form = $this->getServiceLocator()->get('playgroundgame_register_form'); |
||
755 | $socialnetwork = $this->params()->fromRoute('socialnetwork', false); |
||
756 | $form->setAttribute( |
||
757 | 'action', |
||
758 | $this->frontendUrl()->fromRoute( |
||
759 | $this->game->getClassType().'/user-register', |
||
760 | array('id' => $this->game->getIdentifier()) |
||
761 | ) |
||
762 | ); |
||
763 | $params = array(); |
||
764 | $socialCredentials = array(); |
||
765 | |||
766 | if ($userOptions->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) { |
||
767 | $redirect = $request->getQuery()->get('redirect'); |
||
768 | } else { |
||
769 | $redirect = false; |
||
770 | } |
||
771 | |||
772 | if ($socialnetwork) { |
||
773 | $infoMe = $this->getProviderService()->getInfoMe($socialnetwork); |
||
774 | |||
775 | if (!empty($infoMe)) { |
||
776 | $user = $this->getProviderService()->getUserProviderMapper()->findUserByProviderId( |
||
777 | $infoMe->identifier, |
||
778 | $socialnetwork |
||
779 | ); |
||
780 | |||
781 | if ($user || $service->getOptions()->getCreateUserAutoSocial() === true) { |
||
782 | //on le dirige vers l'action d'authentification |
||
783 | if (! $redirect && $userOptions->getLoginRedirectRoute() != '') { |
||
784 | $redirect = $this->frontendUrl()->fromRoute( |
||
785 | $this->game->getClassType().'/login', |
||
786 | array('id' => $this->game->getIdentifier()) |
||
787 | ); |
||
788 | } |
||
789 | $redir = $this->frontendUrl()->fromRoute( |
||
790 | $this->game->getClassType().'/login', |
||
791 | array('id' => $this->game->getIdentifier()) |
||
792 | ) .'/' . $socialnetwork . ($redirect ? '?redirect=' . $redirect : ''); |
||
793 | |||
794 | return $this->redirect()->toUrl($redir); |
||
795 | } |
||
796 | |||
797 | // Je retire la saisie du login/mdp |
||
798 | $form->setAttribute( |
||
799 | 'action', |
||
800 | $this->frontendUrl()->fromRoute( |
||
801 | $this->game->getClassType().'/user-register', |
||
802 | array( |
||
803 | 'id' => $this->game->getIdentifier(), |
||
804 | 'socialnetwork' => $socialnetwork, |
||
805 | |||
806 | ) |
||
807 | ) |
||
808 | ); |
||
809 | $form->remove('password'); |
||
810 | $form->remove('passwordVerify'); |
||
811 | |||
812 | $birthMonth = $infoMe->birthMonth; |
||
813 | if (strlen($birthMonth) <= 1) { |
||
814 | $birthMonth = '0'.$birthMonth; |
||
815 | } |
||
816 | $birthDay = $infoMe->birthDay; |
||
817 | if (strlen($birthDay) <= 1) { |
||
818 | $birthDay = '0'.$birthDay; |
||
819 | } |
||
820 | |||
821 | $gender = $infoMe->gender; |
||
822 | if ($gender == 'female') { |
||
823 | $title = 'Me'; |
||
824 | } else { |
||
825 | $title = 'M'; |
||
826 | } |
||
827 | |||
828 | $params = array( |
||
829 | //'birth_year' => $infoMe->birthYear, |
||
830 | 'title' => $title, |
||
831 | 'dob' => $birthDay.'/'.$birthMonth.'/'.$infoMe->birthYear, |
||
832 | 'firstname' => $infoMe->firstName, |
||
833 | 'lastname' => $infoMe->lastName, |
||
834 | 'email' => $infoMe->email, |
||
835 | 'postalCode' => $infoMe->zip, |
||
836 | ); |
||
837 | $socialCredentials = array( |
||
838 | 'socialNetwork' => strtolower($socialnetwork), |
||
839 | 'socialId' => $infoMe->identifier, |
||
840 | ); |
||
841 | } |
||
842 | } |
||
843 | |||
844 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
845 | $this->game->getClassType().'/user-register', |
||
846 | array('id' => $this->game->getIdentifier()) |
||
847 | ) .($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
848 | $prg = $this->prg($redirectUrl, true); |
||
849 | |||
850 | if ($prg instanceof Response) { |
||
851 | return $prg; |
||
852 | } elseif ($prg === false) { |
||
853 | $form->setData($params); |
||
854 | $viewModel = $this->buildView($this->game); |
||
855 | $viewModel->setVariables(array( |
||
856 | 'registerForm' => $form, |
||
857 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
858 | 'redirect' => $redirect, |
||
859 | )); |
||
860 | return $viewModel; |
||
861 | } |
||
862 | |||
863 | $post = $prg; |
||
864 | $post = array_merge( |
||
865 | $post, |
||
866 | $socialCredentials |
||
867 | ); |
||
868 | |||
869 | if ($this->game->getOnInvitation()) { |
||
870 | $credential = trim( |
||
871 | $post[$this->getGameService()->getOptions()->getOnInvitationField()] |
||
872 | ); |
||
873 | if (!$credential) { |
||
874 | $credential = $this->params()->fromQuery( |
||
875 | $this->getGameService()->getOptions()->getOnInvitationField() |
||
876 | ); |
||
877 | } |
||
878 | $found = $this->getGameService()->getInvitationMapper()->findOneBy(array('requestKey'=>$credential)); |
||
879 | |||
880 | if (!$found || !empty($found->getUser())) { |
||
881 | $this->flashMessenger()->addMessage( |
||
882 | 'Authentication failed. Please try again.' |
||
883 | ); |
||
884 | $form->setData($post); |
||
885 | $viewModel = $this->buildView($this->game); |
||
886 | $viewModel->setVariables(array( |
||
887 | 'registerForm' => $form, |
||
888 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
889 | 'redirect' => $redirect, |
||
890 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
891 | 'flashErrors' => $this->flashMessenger()->getErrorMessages(), |
||
892 | )); |
||
893 | |||
894 | return $viewModel; |
||
895 | } |
||
896 | } |
||
897 | |||
898 | $user = $service->register($post, 'playgroundgame_register_form'); |
||
899 | |||
900 | if (! $user) { |
||
901 | $viewModel = $this->buildView($this->game); |
||
902 | $viewModel->setVariables(array( |
||
903 | 'registerForm' => $form, |
||
904 | 'enableRegistration' => $userOptions->getEnableRegistration(), |
||
905 | 'redirect' => $redirect, |
||
906 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
907 | 'flashErrors' => $this->flashMessenger()->getErrorMessages(), |
||
908 | )); |
||
909 | |||
910 | return $viewModel; |
||
911 | } |
||
912 | |||
913 | if ($this->game->getOnInvitation()) { |
||
914 | // user has been created, associate the code with the userId |
||
915 | $found->setUser($user); |
||
916 | $this->getGameService()->getInvitationMapper()->update($found); |
||
917 | } |
||
918 | |||
919 | if ($service->getOptions()->getEmailVerification()) { |
||
920 | $vm = new ViewModel(array('userEmail' => $user->getEmail())); |
||
921 | $vm->setTemplate('playground-user/register/registermail'); |
||
922 | |||
923 | return $vm; |
||
924 | } elseif ($service->getOptions()->getLoginAfterRegistration()) { |
||
925 | $identityFields = $service->getOptions()->getAuthIdentityFields(); |
||
926 | if (in_array('email', $identityFields)) { |
||
927 | $post['identity'] = $user->getEmail(); |
||
928 | } elseif (in_array('username', $identityFields)) { |
||
929 | $post['identity'] = $user->getUsername(); |
||
930 | } |
||
931 | $post['credential'] = isset($post['password'])?$post['password']:''; |
||
932 | $request->setPost(new Parameters($post)); |
||
933 | |||
934 | // clear adapters |
||
935 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
936 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
937 | |||
938 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
939 | |||
940 | if ($logged) { |
||
941 | return $this->redirect()->toUrl( |
||
942 | $this->frontendUrl()->fromRoute( |
||
943 | $this->game->getClassType(), |
||
944 | array('id' => $this->game->getIdentifier()) |
||
945 | ) |
||
946 | ); |
||
947 | } else { |
||
948 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage( |
||
949 | 'Authentication failed. Please try again.' |
||
950 | ); |
||
951 | return $this->redirect()->toUrl( |
||
952 | $this->frontendUrl()->fromRoute( |
||
953 | $this->game->getClassType() . '/login', |
||
954 | array('id' => $this->game->getIdentifier()) |
||
955 | ) |
||
956 | ); |
||
957 | } |
||
958 | } |
||
959 | |||
960 | $redirect = $this->frontendUrl()->fromRoute( |
||
961 | $this->game->getClassType().'/login', |
||
962 | array('id' => $this->game->getIdentifier()) |
||
963 | ) . ($socialnetwork ? '/' . $socialnetwork : ''). ($redirect ? '?redirect=' . $redirect : ''); |
||
964 | |||
965 | return $this->redirect()->toUrl($redirect); |
||
966 | } |
||
967 | |||
968 | View Code Duplication | public function userProfileAction() |
|
986 | |||
987 | public function userresetAction() |
||
1007 | |||
1008 | View Code Duplication | public function ajaxforgotAction() |
|
1026 | |||
1027 | View Code Duplication | public function cmsPageAction() |
|
1046 | |||
1047 | View Code Duplication | public function cmsListAction() |
|
1066 | |||
1067 | /** |
||
1068 | * |
||
1069 | * @param \PlaygroundGame\Entity\Game $game |
||
1070 | * @param \PlaygroundUser\Entity\User $user |
||
1071 | */ |
||
1072 | public function checkFbRegistration($user, $game) |
||
1121 | |||
1122 | /** |
||
1123 | * This method create the basic Game view |
||
1124 | * @param \PlaygroundGame\Entity\Game $game |
||
1125 | */ |
||
1126 | public function buildView($game) |
||
1127 | { |
||
1128 | if ($this->getRequest()->isXmlHttpRequest()) { |
||
1129 | $viewModel = new JsonModel(); |
||
1130 | if ($game) { |
||
1131 | $view = $this->addAdditionalView($game); |
||
1132 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
1133 | $viewModel->setVariables($view->getVariables()); |
||
1134 | } |
||
1135 | } |
||
1136 | } else { |
||
1137 | $viewModel = new ViewModel(); |
||
1138 | |||
1139 | if ($game) { |
||
1140 | $this->addMetaTitle($game); |
||
1141 | $this->addMetaBitly(); |
||
1142 | $this->addGaEvent($game); |
||
1143 | |||
1144 | $this->customizeGameDesign($game); |
||
1145 | |||
1146 | $view = $this->addAdditionalView($game); |
||
1147 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
1148 | $viewModel->addChild($view, 'additional'); |
||
1149 | } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
1150 | return $view; |
||
1151 | } |
||
1152 | |||
1153 | $this->layout()->setVariables( |
||
1154 | array( |
||
1155 | 'action' => $this->params('action'), |
||
1156 | 'game' => $game, |
||
1157 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
1158 | |||
1159 | ) |
||
1160 | ); |
||
1161 | } |
||
1162 | } |
||
1163 | |||
1164 | if ($game) { |
||
1165 | $viewModel->setVariables($this->getShareData($game)); |
||
1166 | $viewModel->setVariables(array('game' => $game, 'user' => $this->user)); |
||
1167 | } |
||
1168 | |||
1169 | return $viewModel; |
||
1170 | } |
||
1171 | |||
1172 | /** |
||
1173 | * @param \PlaygroundGame\Entity\Game $game |
||
1174 | */ |
||
1175 | public function addAdditionalView($game) |
||
1176 | { |
||
1177 | $view = false; |
||
1178 | |||
1179 | $actionName = $this->getEvent()->getRouteMatch()->getParam('action', 'not-found'); |
||
1180 | $stepsViews = json_decode($game->getStepsViews(), true); |
||
1181 | if ($stepsViews && isset($stepsViews[$actionName])) { |
||
1182 | $beforeLayout = $this->layout()->getTemplate(); |
||
1183 | $actionData = $stepsViews[$actionName]; |
||
1184 | if (is_string($actionData)) { |
||
1185 | $action = $actionData; |
||
1186 | $controller = $this->getEvent()->getRouteMatch()->getParam('controller', 'playgroundgame_game'); |
||
1187 | $view = $this->forward()->dispatch( |
||
1188 | $controller, |
||
1189 | array( |
||
1190 | 'action' => $action, |
||
1191 | 'id' => $game->getIdentifier() |
||
1192 | ) |
||
1193 | ); |
||
1194 | } elseif (is_array($actionData) && count($actionData)>0) { |
||
1195 | $action = key($actionData); |
||
1196 | $controller = $actionData[$action]; |
||
1197 | $view = $this->forward()->dispatch( |
||
1198 | $controller, |
||
1199 | array( |
||
1200 | 'action' => $action, |
||
1201 | 'id' => $game->getIdentifier() |
||
1202 | ) |
||
1203 | ); |
||
1204 | } |
||
1205 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
1206 | $this->layout()->setTemplate($beforeLayout); |
||
1207 | } |
||
1208 | |||
1209 | return $view; |
||
1210 | } |
||
1211 | |||
1212 | public function addMetaBitly() |
||
1222 | |||
1223 | /** |
||
1224 | * @param \PlaygroundGame\Entity\Game $game |
||
1225 | */ |
||
1226 | public function addGaEvent($game) |
||
1234 | |||
1235 | /** |
||
1236 | * @param \PlaygroundGame\Entity\Game $game |
||
1237 | */ |
||
1238 | public function addMetaTitle($game) |
||
1239 | { |
||
1240 | $title = $this->translate($game->getTitle()); |
||
1241 | $this->getGameService()->getServiceManager()->get('ViewHelperManager')->get('HeadTitle')->set($title); |
||
1242 | // Meta set in the layout |
||
1243 | $this->layout()->setVariables( |
||
1244 | array( |
||
1245 | 'breadcrumbTitle' => $title, |
||
1246 | 'currentPage' => array( |
||
1247 | 'pageGames' => 'games', |
||
1248 | 'pageWinners' => '' |
||
1249 | ), |
||
1250 | 'headParams' => array( |
||
1251 | 'headTitle' => $title, |
||
1252 | 'headDescription' => $title, |
||
1253 | ), |
||
1254 | 'bodyCss' => $game->getIdentifier() |
||
1255 | ) |
||
1256 | ); |
||
1257 | } |
||
1258 | |||
1259 | /** |
||
1260 | * @param \PlaygroundGame\Entity\Game $game |
||
1261 | */ |
||
1262 | public function customizeGameDesign($game) |
||
1277 | |||
1278 | /** |
||
1279 | * @param \PlaygroundGame\Entity\Game $game |
||
1280 | */ |
||
1281 | public function getShareData($game) |
||
1367 | |||
1368 | /** |
||
1369 | * return ajax response in json format |
||
1370 | * |
||
1371 | * @param array $data |
||
1372 | * @return \Zend\View\Model\JsonModel |
||
1373 | */ |
||
1374 | View Code Duplication | protected function successJson($data = null) |
|
1382 | |||
1383 | /** |
||
1384 | * return ajax response in json format |
||
1385 | * |
||
1386 | * @param string $message |
||
1387 | * @return \Zend\View\Model\JsonModel |
||
1388 | */ |
||
1389 | View Code Duplication | protected function errorJson($message = null) |
|
1397 | |||
1398 | /** |
||
1399 | * @param string $helperName |
||
1400 | */ |
||
1401 | protected function getViewHelper($helperName) |
||
1405 | |||
1406 | public function getGameService() |
||
1414 | |||
1415 | public function setGameService(GameService $gameService) |
||
1421 | |||
1422 | public function getPrizeService() |
||
1430 | |||
1431 | public function setPrizeService(PrizeService $prizeService) |
||
1437 | |||
1438 | public function getOptions() |
||
1446 | |||
1447 | public function setOptions($options) |
||
1453 | } |
||
1454 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: