Completed
Push — master ( 905126...e876c0 )
by greg
08:24 queued 04:04
created

InstantWinController::exportOccurrencesAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3143
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace PlaygroundGame\Controller\Admin;
4
5
use PlaygroundGame\Entity\InstantWin;
6
use PlaygroundGame\Entity\InstantWinOccurrence;
7
use Zend\InputFilter;
8
use Zend\Validator;
9
use PlaygroundGame\Controller\Admin\GameController;
10
use Zend\View\Model\ViewModel;
11
use Zend\Paginator\Paginator;
12
use PlaygroundCore\ORM\Pagination\LargeTablePaginator;
13
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter;
14
15
class InstantWinController extends GameController
16
{
17
    /**
18
     * @var GameService
19
     */
20
    protected $adminGameService;
21
22
    public function removeAction()
23
    {
24
        $service = $this->getAdminGameService();
25
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
26
        if (!$gameId) {
27
            return $this->redirect()->toRoute('admin/playgroundgame/list');
28
        }
29
30
        $game = $service->getGameMapper()->findById($gameId);
31
        $service->getGameMapper()->remove($game);
32
        $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been removed');
33
34
        return $this->redirect()->toRoute('admin/playgroundgame/list');
35
    }
36
37
    public function createInstantWinAction()
38
    {
39
        $service = $this->getAdminGameService();
40
        $viewModel = new ViewModel();
41
        $viewModel->setTemplate('playground-game/instant-win/instantwin');
42
43
        $gameForm = new ViewModel();
44
        $gameForm->setTemplate('playground-game/game/game-form');
45
46
        $instantwin = new InstantWin();
47
48
        $form = $this->getServiceLocator()->get('playgroundgame_instantwin_form');
49
        $form->bind($instantwin);
50
        $form->setAttribute(
51
            'action',
52
            $this->url()->fromRoute('admin/playgroundgame/create-instantwin', array('gameId' => 0))
53
        );
54
        $form->setAttribute('method', 'post');
55
56
        $request = $this->getRequest();
57
        if ($request->isPost()) {
58
            $data = array_replace_recursive(
59
                $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...
60
                $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...
61
            );
62
            if (empty($data['prizes'])) {
63
                $data['prizes'] = array();
64
            }
65
            $game = $service->create($data, $instantwin, 'playgroundgame_instantwin_form');
66
            if ($game) {
67
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created');
68
69
                return $this->redirect()->toRoute('admin/playgroundgame/list');
70
            }
71
        }
72
        $gameForm->setVariables(array('form' => $form, 'game' => $instantwin));
73
        $viewModel->addChild($gameForm, 'game_form');
74
75
        return $viewModel->setVariables(
76
            array(
77
                'form' => $form,
78
                'title' => 'Create instant win',
79
            )
80
        );
81
    }
82
83 View Code Duplication
    public function editInstantWinAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
84
    {
85
        $service = $this->getAdminGameService();
86
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
87
88
        if (!$gameId) {
89
            return $this->redirect()->toRoute('admin/playgroundgame/create-instantwin');
90
        }
91
92
        $game = $service->getGameMapper()->findById($gameId);
93
        $viewModel = new ViewModel();
94
        $viewModel->setTemplate('playground-game/instant-win/instantwin');
95
96
        $gameForm = new ViewModel();
97
        $gameForm->setTemplate('playground-game/game/game-form');
98
99
        $form   = $this->getServiceLocator()->get('playgroundgame_instantwin_form');
100
        $form->setAttribute(
101
            'action',
102
            $this->url()->fromRoute(
103
                'admin/playgroundgame/edit-instantwin',
104
                array('gameId' => $gameId)
105
            )
106
        );
107
        $form->setAttribute('method', 'post');
108
109
        if ($game->getFbAppId()) {
110
            $appIds = $form->get('fbAppId')->getOption('value_options');
111
            $appIds[$game->getFbAppId()] = $game->getFbAppId();
112
            $form->get('fbAppId')->setAttribute('options', $appIds);
113
        }
114
115
        $gameOptions = $this->getAdminGameService()->getOptions();
116
        $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css';
117
        if (is_file($gameStylesheet)) {
118
            $values = $form->get('stylesheet')->getValueOptions();
119
            $values[$gameStylesheet] = 'Style personnalisé de ce jeu';
120
121
            $form->get('stylesheet')->setAttribute('options', $values);
122
        }
123
124
        $form->bind($game);
125
126
        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...
127
            $data = array_replace_recursive(
128
                $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...
129
                $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...
130
            );
131
            if (empty($data['prizes'])) {
132
                $data['prizes'] = array();
133
            }
134
            $result = $service->edit($data, $game, 'playgroundgame_instantwin_form');
135
136
            if ($result) {
137
                return $this->redirect()->toRoute('admin/playgroundgame/list');
138
            }
139
        }
140
141
        $gameForm->setVariables(array('form' => $form, 'game' => $game));
142
        $viewModel->addChild($gameForm, 'game_form');
143
144
        return $viewModel->setVariables(
145
            array(
146
                'form' => $form,
147
                'title' => 'Edit instant win',
148
            )
149
        );
150
    }
151
152
    public function listOccurrenceAction()
153
    {
154
        $service    = $this->getAdminGameService();
155
        $gameId     = $this->getEvent()->getRouteMatch()->getParam('gameId');
156
157
        if (!$gameId) {
158
            return $this->redirect()->toRoute('admin/playgroundgame/list');
159
        }
160
161
        $game = $service->getGameMapper()->findById($gameId);
162
163
        $adapter = new DoctrineAdapter(
164
            new LargeTablePaginator(
165
                $service->getInstantWinOccurrenceMapper()->queryByGame($game)
166
            )
167
        );
168
        $paginator = new Paginator($adapter);
169
        $paginator->setItemCountPerPage(25);
170
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
171
172
        return new ViewModel(
173
            array(
174
                'occurrences' => $paginator,
175
                'gameId'      => $gameId,
176
                'game'        => $game,
177
            )
178
        );
179
    }
180
181
    public function addOccurrenceAction()
182
    {
183
        $viewModel = new ViewModel();
184
        $viewModel->setTemplate('playground-game/instant-win/occurrence');
185
        $service = $this->getAdminGameService();
186
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
187
        if (!$gameId) {
188
            return $this->redirect()->toRoute('admin/playgroundgame/list');
189
        }
190
        $game = $service->getGameMapper()->findById($gameId);
191
192
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form');
193
        $form->get('submit')->setAttribute('label', 'Add');
194
195
        $form->setAttribute(
196
            'action',
197
            $this->url()->fromRoute(
198
                'admin/playgroundgame/instantwin-occurrence-add',
199
                array('gameId' => $gameId)
200
            )
201
        );
202
        $form->setAttribute('method', 'post');
203
        $form->get('instant_win_id')->setAttribute('value', $gameId);
204
        $occurrence = new InstantWinOccurrence();
205
        $form->bind($occurrence);
206
207
        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...
208
            $data = array_merge(
209
                $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...
210
                $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...
211
            );
212
213
            // Change the format of the date
214
            $value = \DateTime::createFromFormat('d/m/Y H:i', $data['value']);
215
            $data['value'] = $value->format('Y-m-d H:i:s');
216
            
217
            $occurrence = $service->updateOccurrence($data, $occurrence->getId());
218 View Code Duplication
            if ($occurrence) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
219
                // Redirect to list of games
220
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was created');
221
                return $this->redirect()->toRoute(
222
                    'admin/playgroundgame/instantwin-occurrence-list',
223
                    array('gameId'=>$gameId)
224
                );
225
            }
226
        }
227
        return $viewModel->setVariables(
228
            array(
229
                'form' => $form,
230
                'game' => $game,
231
                'occurrence_id' => 0,
232
                'title' => 'Add occurrence',
233
            )
234
        );
235
    }
236
237
    public function importOccurrencesAction()
238
    {
239
        $viewModel = new ViewModel();
240
        $viewModel->setTemplate('playground-game/instant-win/import-occurrences');
241
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
242
        if (!$gameId) {
243
            return $this->redirect()->toRoute('admin/playgroundgame/list');
244
        }
245
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrenceimport_form');
246
247
        $form->get('submit')->setAttribute('label', 'Import');
248
        $form->setAttribute(
249
            'action',
250
            $this->url()->fromRoute(
251
                'admin/playgroundgame/instantwin-occurrences-import',
252
                array('gameId' => $gameId)
253
            )
254
        );
255
        $form->get('instant_win_id')->setAttribute('value', $gameId);
256
257
        // File validator
258
        $inputFilter = new InputFilter\InputFilter();
259
        $fileFilter = new InputFilter\FileInput('file');
260
        $validatorChain = new Validator\ValidatorChain();
261
        $validatorChain->attach(new Validator\File\Exists());
262
        $validatorChain->attach(new Validator\File\Extension('csv'));
263
        $fileFilter->setValidatorChain($validatorChain);
264
        $fileFilter->setRequired(true);
265
266
        $prizeFilter = new InputFilter\Input('prize');
267
        $prizeFilter->setRequired(false);
268
269
        $inputFilter->add($fileFilter);
270
        $inputFilter->add($prizeFilter);
271
        $form->setInputFilter($inputFilter);
272
273
        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...
274
            $data = array_merge_recursive(
275
                $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...
276
                $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...
277
            );
278
            $form->setData($data);
279
            if ($form->isValid()) {
280
                $data = $form->getData();
281
                $created = $this->getAdminGameService()->importOccurrences($data);
282 View Code Duplication
                if ($created) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
283
                    $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
284
                        $created.' occurrences were created !'
285
                    );
286
                    return $this->redirect()->toRoute(
287
                        'admin/playgroundgame/instantwin-occurrence-list',
288
                        array('gameId'=>$gameId)
289
                    );
290
                }
291
            }
292
        }
293
294
        return $viewModel->setVariables(
295
            array(
296
                'form' => $form,
297
                'title' => 'Import occurrences',
298
            )
299
        );
300
    }
301
302
    public function editOccurrenceAction()
303
    {
304
        $viewModel = new ViewModel();
305
        $viewModel->setTemplate('playground-game/instant-win/occurrence');
306
        $service = $this->getAdminGameService();
307
308
        $occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId');
309
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
310
        if (!$gameId) {
311
            return $this->redirect()->toRoute('admin/playgroundgame/list');
312
        }
313
        $game = $service->getGameMapper()->findById($gameId);
314
        $occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId);
315
        // Si l'occurrence a été utilisée, on ne peut plus la modifier
316
        if ($occurrence->getUser()) {
317
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
318
                'This occurrence has a winner, you can not update it.'
319
            );
320
            return $this->redirect()->toRoute(
321
                'admin/playgroundgame/instantwin-occurrence-list',
322
                array('gameId'=>$gameId)
323
            );
324
        }
325
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form');
326
        $form->remove('occurrences_file');
327
328
        $form->get('submit')->setAttribute('label', 'Edit');
329
        $form->setAttribute('action', '');
330
331
        $form->get('instant_win_id')->setAttribute('value', $gameId);
332
333
        $form->bind($occurrence);
334
335
        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...
336
            $data = array_merge(
337
                $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...
338
                $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...
339
            );
340
            $occurrence = $service->updateOccurrence($data, $occurrence->getId());
341
342 View Code Duplication
            if ($occurrence) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
343
                // Redirect to list of games
344
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was edited');
345
                return $this->redirect()->toRoute(
346
                    'admin/playgroundgame/instantwin-occurrence-list',
347
                    array('gameId'=>$gameId)
348
                );
349
            }
350
        }
351
        return $viewModel->setVariables(
352
            array(
353
                'form' => $form,
354
                'game' => $game,
355
                'occurrence_id' => $occurrenceId,
356
                'title' => 'Edit occurrence',
357
            )
358
        );
359
    }
360
361
362
    public function removeOccurrenceAction()
363
    {
364
        $service = $this->getAdminGameService();
365
        $occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId');
366
        if (!$occurrenceId) {
367
            return $this->redirect()->toRoute('admin/playgroundgame/list');
368
        }
369
        $occurrence   = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId);
370
        $instantwinId = $occurrence->getInstantWin()->getId();
371
372
        if ($occurrence->getActive()) {
373
            $service->getInstantWinOccurrenceMapper()->remove($occurrence);
374
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was deleted');
375
        } else {
376
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
377
                'Il y a un participant à cet instant gagnant. Vous ne pouvez plus le supprimer'
378
            );
379
        }
380
381
        return $this->redirect()->toRoute(
382
            'admin/playgroundgame/instantwin-occurrence-list',
383
            array('gameId'=>$instantwinId)
384
        );
385
    }
386
387
    public function exportOccurrencesAction()
388
    {
389
        $gameId         = $this->getEvent()->getRouteMatch()->getParam('gameId');
390
        $game           = $this->getAdminGameService()->getGameMapper()->findById($gameId);
391
        $service = $this->getAdminGameService();
392
393
        $file = $service->setOccurencesToCSV($game);
394
395
        $response = new \Zend\Http\Response\Stream();
396
        $response->setStream(fopen($file, 'r'));
397
        $response->setStatusCode(200);
398
399
        $headers = new \Zend\Http\Headers();
400
        $headers->addHeaderLine('Content-Type', 'text/csv')
401
                ->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file . '"')
402
                ->addHeaderLine('Content-Length', filesize($file));
403
404
        $response->setHeaders($headers);
405
        unlink($file);
406
        return $response;
407
    }
408
409
    public function getAdminGameService()
410
    {
411
        if (!$this->adminGameService) {
412
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_instantwin_service');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceLocator...me_instantwin_service') can also be of type array. However, the property $adminGameService is declared as type object<PlaygroundGame\Co...ller\Admin\GameService>. 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...
413
        }
414
415
        return $this->adminGameService;
416
    }
417
418
    public function setAdminGameService(\PlaygroundGame\Service\Game $adminGameService)
419
    {
420
        $this->adminGameService = $adminGameService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $adminGameService of type object<PlaygroundGame\Service\Game> is incompatible with the declared type object<PlaygroundGame\Co...ller\Admin\GameService> of property $adminGameService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
421
422
        return $this;
423
    }
424
}
425