Completed
Push — master ( 8a50d1...4dc508 )
by greg
06:23 queued 02:59
created

GameController::editGame()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 65
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 65
rs 6.7652
cc 8
eloc 40
nc 36
nop 2

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace PlaygroundGame\Controller\Admin;
4
5
use PlaygroundGame\Service\Game as AdminGameService;
6
use Zend\Mvc\Controller\AbstractActionController;
7
use Zend\View\Model\ViewModel;
8
use PlaygroundGame\Options\ModuleOptions;
9
use Zend\Paginator\Paginator;
10
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter;
11
use PlaygroundCore\ORM\Pagination\LargeTablePaginator;
12
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
13
use Zend\Stdlib\ErrorHandler;
14
15
class GameController extends AbstractActionController
16
{
17
    protected $options;
18
19
    /**
20
     * @var \PlaygroundGame\Service\Game
21
     */
22
    protected $adminGameService;
23
24
    protected $game;
25
26
    public function checkGame()
27
    {
28
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
29
        if (!$gameId) {
30
            return $this->redirect()->toRoute('admin/playgroundgame/list');
31
        }
32
        
33
        $game = $this->getAdminGameService()->getGameMapper()->findById($gameId);
34
        if (!$game) {
35
            return $this->redirect()->toRoute('admin/playgroundgame/list');
36
        }
37
        $this->game = $game;
38
    }
39
40
    public function createForm($form)
41
    {
42
        // I use the wonderful Form Generator to create the Post & Vote form
43
        $this->forward()->dispatch(
44
            'PlaygroundCore\Controller\Formgen',
45
            array(
46
                'controller' => 'PlaygroundCore\Controller\Formgen',
47
                'action' => 'create'
48
            )
49
        );
50
51
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
52
            $data = $this->getRequest()->getPost()->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53
            $form = $this->getAdminGameService()->createForm($data, $this->game, $form);
54
            if ($form) {
55
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The form was created');
56
            }
57
        }
58
        $formTemplate='';
59
        if ($form) {
60
            $formTemplate = $form->getFormTemplate();
61
        }
62
63
        return array(
64
            'form' => $form,
65
            'formTemplate' => $formTemplate,
66
            'gameId' => $this->game->getId(),
67
            'game' => $this->game,
68
        );
69
    }
70
71
    /**
72
     * @param string $templatePath
73
     * @param string $formId
74
     */
75
    public function editGame($templatePath, $formId)
76
    {
77
78
        $viewModel = new ViewModel();
79
        $viewModel->setTemplate($templatePath);
80
81
        $gameForm = new ViewModel();
82
        $gameForm->setTemplate('playground-game/game/game-form');
83
84
        $form   = $this->getServiceLocator()->get($formId);
85
        $form->setAttribute(
86
            'action',
87
            $this->url()->fromRoute(
88
                'admin/playgroundgame/edit-' . $this->game->getClassType(),
89
                array('gameId' => $this->game->getId())
90
            )
91
        );
92
        $form->setAttribute('method', 'post');
93
94
        if ($this->game->getFbAppId()) {
95
            $appIds = $form->get('fbAppId')->getOption('value_options');
96
            $appIds[$this->game->getFbAppId()] = $this->game->getFbAppId();
97
            $form->get('fbAppId')->setAttribute('options', $appIds);
98
        }
99
100
        $gameOptions = $this->getAdminGameService()->getOptions();
101
        $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $this->game->getId(). '.css';
102
        if (is_file($gameStylesheet)) {
103
            $values = $form->get('stylesheet')->getValueOptions();
104
            $values[$gameStylesheet] = 'Style personnalisé de ce jeu';
105
106
            $form->get('stylesheet')->setAttribute('options', $values);
107
        }
108
109
        $form->bind($this->game);
110
111
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
112
            $data = array_replace_recursive(
113
                $this->getRequest()->getPost()->toArray(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
114
                $this->getRequest()->getFiles()->toArray()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getFiles() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
115
            );
116
            if (empty($data['prizes'])) {
117
                $data['prizes'] = array();
118
            }
119
            if (isset($data['drawDate']) && $data['drawDate']) {
120
                $data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']);
121
            }
122
            $result = $this->getAdminGameService()->edit($data, $this->game, $formId);
123
124
            if ($result) {
125
                return $this->redirect()->toRoute('admin/playgroundgame/list');
126
            }
127
        }
128
129
        $gameForm->setVariables(array('form' => $form, 'game' => $this->game));
130
        $viewModel->addChild($gameForm, 'game_form');
131
132
        return $viewModel->setVariables(
133
            array(
134
                'form' => $form,
135
                'title' => 'Edit this game',
136
            )
137
        );
138
    
139
    }
140
141
    public function listAction()
142
    {
143
        $filter    = $this->getEvent()->getRouteMatch()->getParam('filter');
144
        $type    = $this->getEvent()->getRouteMatch()->getParam('type');
145
146
        $service    = $this->getAdminGameService();
147
        $adapter = new DoctrineAdapter(new ORMPaginator($service->getQueryGamesOrderBy($type, $filter)));
148
        $paginator = new Paginator($adapter);
149
        $paginator->setItemCountPerPage(25);
150
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
151
152
        foreach ($paginator as $game) {
153
            $game->entry = $service->getEntryMapper()->countByGame($game);
154
        }
155
156
        return array(
157
            'games'    => $paginator,
158
            'type'        => $type,
159
            'filter'    => $filter,
160
        );
161
    }
162
163
    public function entryAction()
164
    {
165
        $this->checkGame();
166
167
        $adapter = new DoctrineAdapter(
168
            new LargeTablePaginator(
169
                $this->getAdminGameService()->getEntriesQuery($this->game)
170
            )
171
        );
172
        $paginator = new Paginator($adapter);
173
        $paginator->setItemCountPerPage(10);
174
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
175
176
        $header = $this->getAdminGameService()->getEntriesHeader($this->game);
177
        $entries = $this->getAdminGameService()->getGameEntries($header, $paginator, $this->game);
178
179
        return array(
180
            'paginator' => $paginator,
181
            'entries' => $entries,
182
            'header' => $header,
183
            'game' => $this->game,
184
            'gameId' => $this->game->getId()
185
        );
186
    }
187
188
    public function downloadAction()
189
    {
190
        $this->checkGame();
191
        $header = $this->getAdminGameService()->getEntriesHeader($this->game);
192
        $query = $this->getAdminGameService()->getEntriesQuery($this->game);
193
194
        $content = "\xEF\xBB\xBF"; // UTF-8 BOM
195
        $content .= $this->getAdminGameService()->getCSV(
196
            $this->getAdminGameService()->getGameEntries(
197
                $header,
198
                $query->getResult(),
199
                $this->game
200
            )
201
        );
202
203
        $response = $this->getResponse();
204
        $headers = $response->getHeaders();
205
        $headers->addHeaderLine('Content-Encoding: UTF-8');
206
        $headers->addHeaderLine('Content-Type', 'text/csv; charset=UTF-8');
207
        $headers->addHeaderLine('Content-Disposition', "attachment; filename=\"entry.csv\"");
208
        $headers->addHeaderLine('Accept-Ranges', 'bytes');
209
        $headers->addHeaderLine('Content-Length', strlen($content));
210
211
        $response->setContent($content);
212
213
        return $response;
214
    }
215
216
    // Only used for Quiz and Lottery
217
    public function drawAction()
218
    {
219
        $this->checkGame();
220
221
        $winningEntries = $this->getAdminGameService()->draw($this->game);
222
223
        $content = "\xEF\xBB\xBF"; // UTF-8 BOM
224
        $content .= "ID;Pseudo;Nom;Prenom;E-mail;Etat\n";
225
226
        foreach ($winningEntries as $e) {
227
            $etat = 'gagnant';
228
229
            $content   .= $e->getUser()->getId()
230
            . ";" . $e->getUser()->getUsername()
231
            . ";" . $e->getUser()->getLastname()
232
            . ";" . $e->getUser()->getFirstname()
233
            . ";" . $e->getUser()->getEmail()
234
            . ";" . $etat
235
            ."\n";
236
        }
237
238
        $response = $this->getResponse();
239
        $headers = $response->getHeaders();
240
        $headers->addHeaderLine('Content-Encoding: UTF-8');
241
        $headers->addHeaderLine('Content-Type', 'text/csv; charset=UTF-8');
242
        $headers->addHeaderLine('Content-Disposition', "attachment; filename=\"gagnants.csv\"");
243
        $headers->addHeaderLine('Accept-Ranges', 'bytes');
244
        $headers->addHeaderLine('Content-Length', strlen($content));
245
246
        $response->setContent($content);
247
248
        return $response;
249
    }
250
    
251
    /**
252
     * This method serialize a game an export it as a txt file
253
     * @return \Zend\Stdlib\ResponseInterface
254
     */
255
    public function exportAction()
256
    {
257
        $this->checkGame();
258
        $content = serialize($this->game);
259
260
        $response = $this->getResponse();
261
        $headers = $response->getHeaders();
262
        $headers->addHeaderLine('Content-Encoding: UTF-8');
263
        $headers->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8');
264
        $headers->addHeaderLine(
265
            'Content-Disposition',
266
            "attachment; filename=\"". $this->game->getIdentifier() .".txt\""
267
        );
268
        $headers->addHeaderLine('Accept-Ranges', 'bytes');
269
        $headers->addHeaderLine('Content-Length', strlen($content));
270
    
271
        $response->setContent($content);
272
    
273
        return $response;
274
    }
275
    
276
    /**
277
     * This method take an uploaded txt file containing a serialized game
278
     * and persist it in the database
279
     * @return unknown
280
     */
281
    public function importAction()
282
    {
283
        $form = $this->getServiceLocator()->get('playgroundgame_import_form');
284
        $form->setAttribute('action', $this->url()->fromRoute('admin/playgroundgame/import'));
285
        $form->setAttribute('method', 'post');
286
        
287
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
288
            $data = array_replace_recursive(
289
                $this->getRequest()->getPost()->toArray(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
290
                $this->getRequest()->getFiles()->toArray()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getFiles() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
291
            );
292
            
293
            if (! empty($data['import_file']['tmp_name'])) {
294
                ErrorHandler::start();
295
                $game = unserialize(file_get_contents($data['import_file']['tmp_name']));
296
                $game->setId(null);
297
                if ($data['slug']) {
298
                    $game->setIdentifier($data['slug']);
299
                }
300
                $duplicate = $this->getAdminGameService()->getGameMapper()->findByIdentifier($game->getIdentifier());
301
                if (!$duplicate) {
302
                    $this->getAdminGameService()->getGameMapper()->insert($game);
303
                }
304
305
                ErrorHandler::stop(true);
306
            }
307
            
308
            return $this->redirect()->toRoute('admin/playgroundgame/list');
309
        }
310
        
311
        return array(
312
            'form' => $form,
313
        );
314
    }
315
316
    public function removeAction()
317
    {
318
        $this->checkGame();
319
320
        try {
321
            $this->getAdminGameService()->getGameMapper()->remove($this->game);
322
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been edited');
323
        } catch (\Doctrine\DBAL\DBALException $e) {
324
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
325
                'Il y a déjà eu des participants à ce jeu. Vous ne pouvez plus le supprimer'
326
            );
327
        }
328
329
        return $this->redirect()->toRoute('admin/playgroundgame/list');
330
    }
331
332
    public function setActiveAction()
333
    {
334
        $this->checkGame();
335
336
        $this->game->setActive(!$this->game->getActive());
337
        $this->getAdminGameService()->getGameMapper()->update($game);
0 ignored issues
show
Bug introduced by
The variable $game does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
338
339
        return $this->redirect()->toRoute('admin/playgroundgame/list');
340
    }
341
342
    public function formAction()
343
    {
344
        $this->checkGame();
345
        
346
        $form = $this->game->getPlayerForm();
347
348
        return $this->createForm($form);
349
    }
350
351
    public function setOptions(ModuleOptions $options)
352
    {
353
        $this->options = $options;
354
355
        return $this;
356
    }
357
358
    public function getOptions()
359
    {
360
        if (!$this->options instanceof ModuleOptions) {
361
            $this->setOptions($this->getServiceLocator()->get('playgroundgame_module_options'));
0 ignored issues
show
Documentation introduced by
$this->getServiceLocator...ndgame_module_options') is of type object|array, but the function expects a object<PlaygroundGame\Options\ModuleOptions>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
362
        }
363
364
        return $this->options;
365
    }
366
367
    public function getAdminGameService()
368
    {
369
        if (!$this->adminGameService) {
370
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_game_service');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceLocator...oundgame_game_service') can also be of type array. However, the property $adminGameService is declared as type object<PlaygroundGame\Service\Game>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
371
        }
372
373
        return $this->adminGameService;
374
    }
375
376
    public function setAdminGameService(AdminGameService $adminGameService)
377
    {
378
        $this->adminGameService = $adminGameService;
379
380
        return $this;
381
    }
382
}
383