| Conditions | 17 |
| Paths | 22 |
| Total Lines | 156 |
| Code Lines | 100 |
| Lines | 68 |
| Ratio | 43.59 % |
| Changes | 5 | ||
| Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 12 | public function playAction() |
||
| 13 | { |
||
| 14 | $sg = $this->getGameService(); |
||
| 15 | |||
| 16 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 17 | $channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
||
| 18 | |||
| 19 | $game = $sg->checkGame($identifier); |
||
| 20 | if (!$game || $game->isClosed()) { |
||
| 21 | return $this->notFoundAction(); |
||
| 22 | } |
||
| 23 | |||
| 24 | $redirectFb = $this->checkFbRegistration($this->zfcUserAuthentication()->getIdentity(), $game, $channel); |
||
|
|
|||
| 25 | if ($redirectFb) { |
||
| 26 | return $redirectFb; |
||
| 27 | } |
||
| 28 | |||
| 29 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 30 | |||
| 31 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
| 32 | $redirect = urlencode( |
||
| 33 | $this->frontendUrl()->fromRoute( |
||
| 34 | $game->getClassType() . '/play', |
||
| 35 | array( |
||
| 36 | 'id' => $game->getIdentifier(), |
||
| 37 | 'channel' => $channel |
||
| 38 | ), |
||
| 39 | array('force_canonical' => true) |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | |||
| 43 | return $this->redirect()->toUrl( |
||
| 44 | $this->frontendUrl()->fromRoute( |
||
| 45 | 'zfcuser/register', |
||
| 46 | array('channel' => $channel) |
||
| 47 | ) . '?redirect='.$redirect |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($game->getOccurrenceType()=='datetime') { |
||
| 52 | if ($this->getRequest()->isPost()) { |
||
| 53 | // En post, je reçois la maj du form pour les gagnants. |
||
| 54 | // Je n'ai pas à créer une nouvelle participation mais vérifier la précédente |
||
| 55 | $lastEntry = $sg->findLastInactiveEntry($game, $user); |
||
| 56 | View Code Duplication | if (!$lastEntry) { |
|
| 57 | return $this->redirect()->toUrl( |
||
| 58 | $this->frontendUrl()->fromRoute( |
||
| 59 | 'instantwin', |
||
| 60 | array( |
||
| 61 | 'id' => $game->getIdentifier(), |
||
| 62 | 'channel' => $channel |
||
| 63 | ), |
||
| 64 | array('force_canonical' => true) |
||
| 65 | ) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | $winner = $lastEntry->getWinner(); |
||
| 69 | // if not winner, I'm not authorized to call this page in POST mode. |
||
| 70 | View Code Duplication | if (!$winner) { |
|
| 71 | return $this->redirect()->toUrl( |
||
| 72 | $this->frontendUrl()->fromRoute( |
||
| 73 | 'instantwin', |
||
| 74 | array( |
||
| 75 | 'id' => $game->getIdentifier(), |
||
| 76 | 'channel' => $channel |
||
| 77 | ), |
||
| 78 | array('force_canonical' => true) |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | } else { |
||
| 83 | // J'arrive sur le jeu, j'essaie donc de participer |
||
| 84 | $entry = $sg->play($game, $user); |
||
| 85 | View Code Duplication | if (!$entry) { |
|
| 86 | // the user has already taken part of this game and the participation limit has been reached |
||
| 87 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
| 88 | |||
| 89 | return $this->redirect()->toUrl( |
||
| 90 | $this->frontendUrl()->fromRoute( |
||
| 91 | 'instantwin/result', |
||
| 92 | array( |
||
| 93 | 'id' => $game->getIdentifier(), |
||
| 94 | 'channel' => $channel |
||
| 95 | ) |
||
| 96 | ) |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | // update the winner attribute in entry. |
||
| 101 | $winner = $sg->IsInstantWinner($game, $user); |
||
| 102 | } |
||
| 103 | $prize = null; |
||
| 104 | if ($winner) { |
||
| 105 | $prize = $winner->getPrize(); |
||
| 106 | } |
||
| 107 | $viewVariables = array( |
||
| 108 | 'winner' => $winner, |
||
| 109 | 'prize' => $prize, |
||
| 110 | 'over' => false, |
||
| 111 | ); |
||
| 112 | } elseif ($game->getOccurrenceType()=='code') { |
||
| 113 | $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrencecode_form'); |
||
| 114 | $form->setAttribute( |
||
| 115 | 'action', |
||
| 116 | $this->frontendUrl()->fromRoute( |
||
| 117 | 'instantwin/play', |
||
| 118 | array( |
||
| 119 | 'id' => $game->getIdentifier(), |
||
| 120 | 'channel' => $channel |
||
| 121 | ), |
||
| 122 | array('force_canonical' => true) |
||
| 123 | ) |
||
| 124 | ); |
||
| 125 | |||
| 126 | $occurrence = null; |
||
| 127 | if ($this->getRequest()->isPost()) { |
||
| 128 | $form->setData($this->getRequest()->getPost()); |
||
| 129 | if ($form->isValid()) { |
||
| 130 | $data = $form->getData('code-input'); |
||
| 131 | $code = filter_var($data['code-input'], FILTER_SANITIZE_STRING); |
||
| 132 | $occurrence = $this->getGameService()->isInstantWinner($game, $user, $code); |
||
| 133 | if (!$occurrence) { |
||
| 134 | $this->flashMessenger()->addMessage('Le code entré est invalide ou a déjà été utilisé !'); |
||
| 135 | return $this->redirect()->toUrl( |
||
| 136 | $this->frontendUrl()->fromRoute( |
||
| 137 | 'instantwin/play', |
||
| 138 | array( |
||
| 139 | 'id' => $game->getIdentifier(), |
||
| 140 | 'channel' => $channel |
||
| 141 | ), |
||
| 142 | array('force_canonical' => true) |
||
| 143 | ) |
||
| 144 | ); |
||
| 145 | View Code Duplication | } else { |
|
| 146 | return $this->redirect()->toUrl( |
||
| 147 | $this->frontendUrl()->fromRoute( |
||
| 148 | 'instantwin/result', |
||
| 149 | array( |
||
| 150 | 'id' => $game->getIdentifier(), |
||
| 151 | 'channel' => $channel |
||
| 152 | ) |
||
| 153 | ) |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | $viewVariables = array('form' => $form); |
||
| 159 | } |
||
| 160 | |||
| 161 | $viewModel = $this->buildView($game); |
||
| 162 | if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
||
| 163 | $viewModel->setVariables($viewVariables); |
||
| 164 | } |
||
| 165 | |||
| 166 | return $viewModel; |
||
| 167 | } |
||
| 168 | |||
| 275 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: