Completed
Push — master ( e876c0...8a8de6 )
by greg
13:55 queued 08:45
created

PostVoteController::formAction()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 38
Code Lines 25

Duplication

Lines 38
Ratio 100 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 38
loc 38
rs 8.439
cc 5
eloc 25
nc 7
nop 0
1
<?php
2
3
namespace PlaygroundGame\Controller\Admin;
4
5
use PlaygroundGame\Service\Game as AdminGameService;
6
use PlaygroundGame\Entity\PostVote;
7
use Zend\View\Model\ViewModel;
8
9
class PostVoteController extends GameController
10
{
11
    /**
12
     * @var GameService
13
     */
14
    protected $adminGameService;
15
16 View Code Duplication
    public function formAction()
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...
17
    {
18
        $service = $this->getAdminGameService();
19
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
20
        if (!$gameId) {
21
            return $this->redirect()->toRoute('admin/playgroundgame/list');
22
        }
23
        $game = $service->getGameMapper()->findById($gameId);
24
        $form = $service->getPostVoteFormMapper()->findByGame($game);
25
26
        // I use the wonderful Form Generator to create the Post & Vote form
27
        $this->forward()->dispatch(
28
            'PlaygroundCore\Controller\Formgen',
29
            array(
30
                'controller' => 'PlaygroundCore\Controller\Formgen',
31
                'action' => 'create'
32
            )
33
        );
34
35
        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...
36
            $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...
37
            $form = $service->createForm($data, $game, $form);
38
            if ($form) {
39
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The form was created');
40
            }
41
        }
42
        $formTemplate='';
43
        if ($form) {
44
            $formTemplate = $form->getFormTemplate();
45
        }
46
47
        return array(
48
            'form' => $form,
49
            'formTemplate' => $formTemplate,
50
            'gameId' => $gameId,
51
            'game' => $game,
52
        );
53
    }
54
    
55 View Code Duplication
    public function createPostVoteAction()
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...
56
    {
57
        $service = $this->getAdminGameService();
58
        $viewModel = new ViewModel();
59
        $viewModel->setTemplate('playground-game/post-vote/postvote');
60
61
        $gameForm = new ViewModel();
62
        $gameForm->setTemplate('playground-game/game/game-form');
63
64
        $postVote = new PostVote();
65
66
        $form = $this->getServiceLocator()->get('playgroundgame_postvote_form');
67
        $form->bind($postVote);
68
        $form->get('submit')->setAttribute('label', 'Add');
69
        $form->setAttribute(
70
            'action',
71
            $this->url()->fromRoute(
72
                'admin/playgroundgame/create-postvote',
73
                array('gameId' => 0)
74
            )
75
        );
76
        $form->setAttribute('method', 'post');
77
78
        $request = $this->getRequest();
79
        if ($request->isPost()) {
80
            $data = array_replace_recursive(
81
                $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...
82
                $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...
83
            );
84
            if (empty($data['prizes'])) {
85
                $data['prizes'] = array();
86
            }
87
            $game = $service->create($data, $postVote, 'playgroundgame_postvote_form');
88
            if ($game) {
89
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created');
90
91
                return $this->redirect()->toRoute('admin/playgroundgame/list');
92
            }
93
        }
94
        $gameForm->setVariables(array('form' => $form, 'game' => $postVote));
95
        $viewModel->addChild($gameForm, 'game_form');
96
97
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Create Post & Vote'));
98
    }
99
100 View Code Duplication
    public function editPostVoteAction()
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...
101
    {
102
        $service = $this->getAdminGameService();
103
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
104
105
        if (!$gameId) {
106
            return $this->redirect()->toRoute('admin/playgroundgame/create-postvote');
107
        }
108
109
        $game = $service->getGameMapper()->findById($gameId);
110
        $viewModel = new ViewModel();
111
        $viewModel->setTemplate('playground-game/post-vote/postvote');
112
113
        $gameForm = new ViewModel();
114
        $gameForm->setTemplate('playground-game/game/game-form');
115
116
        $form   = $this->getServiceLocator()->get('playgroundgame_postvote_form');
117
        $form->setAttribute(
118
            'action',
119
            $this->url()->fromRoute(
120
                'admin/playgroundgame/edit-postvote',
121
                array('gameId' => $gameId)
122
            )
123
        );
124
        $form->setAttribute('method', 'post');
125
        
126
        if ($game->getFbAppId()) {
127
            $appIds = $form->get('fbAppId')->getOption('value_options');
128
            $appIds[$game->getFbAppId()] = $game->getFbAppId();
129
            $form->get('fbAppId')->setAttribute('options', $appIds);
130
        }
131
132
        $gameOptions = $this->getAdminGameService()->getOptions();
133
        $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css';
134
        if (is_file($gameStylesheet)) {
135
            $values = $form->get('stylesheet')->getValueOptions();
136
            $values[$gameStylesheet] = 'Style personnalisé de ce jeu';
137
138
            $form->get('stylesheet')->setAttribute('options', $values);
139
        }
140
141
        $form->bind($game);
142
143
        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...
144
            $data = array_replace_recursive(
145
                $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...
146
                $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...
147
            );
148
            if (empty($data['prizes'])) {
149
                $data['prizes'] = array();
150
            }
151
            $result = $service->edit($data, $game, 'playgroundgame_postvote_form');
152
153
            if ($result) {
154
                return $this->redirect()->toRoute('admin/playgroundgame/list');
155
            }
156
        }
157
158
        $gameForm->setVariables(array('form' => $form, 'game' => $game));
159
        $viewModel->addChild($gameForm, 'game_form');
160
161
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit Post & Vote'));
162
    }
163
164 View Code Duplication
    public function modListAction()
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...
165
    {
166
        $service = $this->getAdminGameService();
167
        $posts = $service->getPostVotePostMapper()->findBy(array('status' => 1));
168
169
        if (is_array($posts)) {
170
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($posts));
171
            $paginator->setItemCountPerPage(10);
172
            $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
173
        } else {
174
            $paginator = $posts;
175
        }
176
177
        return array('posts' => $paginator);
178
    }
179
180
    public function moderationEditAction()
181
    {
182
        $service = $this->getAdminGameService();
183
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
184
        $status = $this->getEvent()->getRouteMatch()->getParam('status');
185
186
        if (!$postId) {
187
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
188
        }
189
        $post = $service->getPostVotePostMapper()->findById($postId);
190
191
        if (! $post) {
192
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
193
        }
194
        $game = $post->getPostvote();
195
196
        if ($status && $status=='validation') {
197
            $post->setStatus(2);
198
            $service->getPostVotePostMapper()->update($post);
199
200
            return $this->redirect()->toUrl(
201
                $this->url()->fromRoute(
202
                    'admin/postvote/entry',
203
                    array('gameId' => $game->getId())
204
                )
205
            );
206
        } elseif ($status && $status=='rejection') {
207
            $post->setStatus(9);
208
            $service->getPostVotePostMapper()->update($post);
209
210
            return $this->redirect()->toUrl(
211
                $this->url()->fromRoute(
212
                    'admin/postvote/entry',
213
                    array('gameId' => $game->getId())
214
                )
215
            );
216
        }
217
218
        return array('game' => $game, 'post' => $post);
219
    }
220
    
221
    public function pushAction()
222
    {
223
        $service = $this->getAdminGameService();
224
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
225
        $pushed = $this->getEvent()->getRouteMatch()->getParam('pushed');
226
    
227
        if (!$postId) {
228
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
229
        }
230
        $post = $service->getPostVotePostMapper()->findById($postId);
231
    
232
        if (! $post) {
233
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
234
        }
235
        $game = $post->getPostvote();
236
    
237
        $post->setPushed($pushed);
238
        $service->getPostVotePostMapper()->update($post);
239
    
240
        return $this->redirect()->toUrl(
241
            $this->url()->fromRoute(
242
                'admin/postvote/entry',
243
                array('gameId' => $game->getId())
244
            )
245
        );
246
    }
247
248
    public function getAdminGameService()
249
    {
250
        if (!$this->adminGameService) {
251
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_postvote_service');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceLocator...game_postvote_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...
252
        }
253
254
        return $this->adminGameService;
255
    }
256
257
    public function setAdminGameService(AdminGameService $adminGameService)
258
    {
259
        $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...
260
261
        return $this;
262
    }
263
}
264