| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace PlaygroundGame\Controller\Frontend; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Zend\ServiceManager\ServiceLocatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class LotteryController extends GameController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * @var gameService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected $gameService; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     public function __construct(ServiceLocatorInterface $locator) | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         parent::__construct($locator); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function playAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $redirectFb = $this->checkFbRegistration($this->user, $this->game); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         if ($redirectFb) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             return $redirectFb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $entry = $this->getGameService()->play($this->game, $this->user); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 26 |  | View Code Duplication |         if (!$entry) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             // the user has already taken part of this game and the participation limit has been reached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $this->flashMessenger()->addMessage('Vous avez déjà participé'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             return $this->redirect()->toUrl( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 $this->frontendUrl()->fromRoute( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                     'lottery/result', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     array('id' => $this->game->getIdentifier()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         // Every entry is eligible to draw | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $entry->setDrawable(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $entry->setActive(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->getGameService()->getEntryMapper()->update($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return $this->redirect()->toUrl( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $this->frontendUrl()->fromRoute( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $this->game->getClassType() . '/'. $this->game->nextStep($this->params('action')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 array('id' => $this->game->getIdentifier()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function resultAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $statusMail = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $socialLinkUrl = $this->frontendUrl()->fromRoute( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             'lottery', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             array('id' => $this->game->getIdentifier()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             array('force_canonical' => true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ).'?key='.$secretKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         // With core shortener helper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $this->user); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 |  | View Code Duplication |         if (!$lastEntry) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             return $this->redirect()->toUrl( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $this->frontendUrl()->fromRoute( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     'lottery', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     array('id' => $this->game->getIdentifier()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     array('force_canonical' => true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $form->setAttribute('method', 'post'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if ($this->getRequest()->isPost()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $data = $this->getRequest()->getPost()->toArray(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $form->setData($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             if ($form->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $result = $this->getGameService()->sendShareMail($data, $this->game, $this->user, $lastEntry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 if ($result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     $statusMail = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     $this->getGameService()->addAnotherChance($this->game, $this->user, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // buildView must be before sendMail because it adds the game template path to the templateStack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $viewModel = $this->buildView($this->game); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->getGameService()->sendMail($this->game, $this->user, $lastEntry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $viewModel->setVariables(array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 'statusMail'    => $statusMail, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'form'          => $form, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 'socialLinkUrl' => $socialLinkUrl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 'secretKey'     => $secretKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return $viewModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 104 |  | View Code Duplication |     public function fbshareAction() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $result = parent::fbshareAction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $bonusEntry = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if ($result->getVariable('success')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $response = $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $response->setContent(\Zend\Json\Json::encode(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'success' => $result, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             'playBonus' => $bonusEntry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |          ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $response; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 122 |  | View Code Duplication |     public function fbrequestAction() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $result = parent::fbrequestAction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $bonusEntry = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if ($result->getVariable('success')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $response = $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $response->setContent(\Zend\Json\Json::encode(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             'success' => $result, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             'playBonus' => $bonusEntry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $response; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 140 |  | View Code Duplication |     public function tweetAction() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $result = parent::tweetAction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $bonusEntry = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         if ($result->getVariable('success')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             $bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $response = $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $response->setContent(\Zend\Json\Json::encode(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             'success' => $result, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             'playBonus' => $bonusEntry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return $response; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 158 |  | View Code Duplication |     public function googleAction() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $result = parent::googleAction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $bonusEntry = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         if ($result->getVariable('success')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             $bonusEntry = $this->getGameService()->addAnotherChance($this->game, $this->user, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $response = $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $response->setContent(\Zend\Json\Json::encode(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             'success' => $result, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             'playBonus' => $bonusEntry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         return $response; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     public function getGameService() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         if (!$this->gameService) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             $this->gameService = $this->getServiceLocator()->get('playgroundgame_lottery_service'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         return $this->gameService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 184 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.