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

PostVoteController::formAction()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 38
Code Lines 25

Duplication

Lines 38
Ratio 100 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
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 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...
17
    {
18
        $service = $this->getAdminGameService();
19
        $viewModel = new ViewModel();
20
        $viewModel->setTemplate('playground-game/post-vote/postvote');
21
22
        $gameForm = new ViewModel();
23
        $gameForm->setTemplate('playground-game/game/game-form');
24
25
        $postVote = new PostVote();
26
27
        $form = $this->getServiceLocator()->get('playgroundgame_postvote_form');
28
        $form->bind($postVote);
29
        $form->get('submit')->setAttribute('label', 'Add');
30
        $form->setAttribute(
31
            'action',
32
            $this->url()->fromRoute(
33
                'admin/playgroundgame/create-postvote',
34
                array('gameId' => 0)
35
            )
36
        );
37
        $form->setAttribute('method', 'post');
38
39
        $request = $this->getRequest();
40
        if ($request->isPost()) {
41
            $data = array_replace_recursive(
42
                $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...
43
                $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...
44
            );
45
            if (empty($data['prizes'])) {
46
                $data['prizes'] = array();
47
            }
48
            $game = $service->create($data, $postVote, 'playgroundgame_postvote_form');
49
            if ($game) {
50
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created');
51
52
                return $this->redirect()->toRoute('admin/playgroundgame/list');
53
            }
54
        }
55
        $gameForm->setVariables(array('form' => $form, 'game' => $postVote));
56
        $viewModel->addChild($gameForm, 'game_form');
57
58
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Create Post & Vote'));
59
    }
60
61 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...
62
    {
63
        $service = $this->getAdminGameService();
64
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
65
66
        if (!$gameId) {
67
            return $this->redirect()->toRoute('admin/playgroundgame/create-postvote');
68
        }
69
70
        $game = $service->getGameMapper()->findById($gameId);
71
        $viewModel = new ViewModel();
72
        $viewModel->setTemplate('playground-game/post-vote/postvote');
73
74
        $gameForm = new ViewModel();
75
        $gameForm->setTemplate('playground-game/game/game-form');
76
77
        $form   = $this->getServiceLocator()->get('playgroundgame_postvote_form');
78
        $form->setAttribute(
79
            'action',
80
            $this->url()->fromRoute(
81
                'admin/playgroundgame/edit-postvote',
82
                array('gameId' => $gameId)
83
            )
84
        );
85
        $form->setAttribute('method', 'post');
86
        
87
        if ($game->getFbAppId()) {
88
            $appIds = $form->get('fbAppId')->getOption('value_options');
89
            $appIds[$game->getFbAppId()] = $game->getFbAppId();
90
            $form->get('fbAppId')->setAttribute('options', $appIds);
91
        }
92
93
        $gameOptions = $this->getAdminGameService()->getOptions();
94
        $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css';
95
        if (is_file($gameStylesheet)) {
96
            $values = $form->get('stylesheet')->getValueOptions();
97
            $values[$gameStylesheet] = 'Style personnalisé de ce jeu';
98
99
            $form->get('stylesheet')->setAttribute('options', $values);
100
        }
101
102
        $form->bind($game);
103
104
        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...
105
            $data = array_replace_recursive(
106
                $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...
107
                $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...
108
            );
109
            if (empty($data['prizes'])) {
110
                $data['prizes'] = array();
111
            }
112
            $result = $service->edit($data, $game, 'playgroundgame_postvote_form');
113
114
            if ($result) {
115
                return $this->redirect()->toRoute('admin/playgroundgame/list');
116
            }
117
        }
118
119
        $gameForm->setVariables(array('form' => $form, 'game' => $game));
120
        $viewModel->addChild($gameForm, 'game_form');
121
122
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit Post & Vote'));
123
    }
124
125 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...
126
    {
127
        $service = $this->getAdminGameService();
128
        $posts = $service->getPostVotePostMapper()->findBy(array('status' => 1));
129
130
        if (is_array($posts)) {
131
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($posts));
132
            $paginator->setItemCountPerPage(10);
133
            $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
134
        } else {
135
            $paginator = $posts;
136
        }
137
138
        return array('posts' => $paginator);
139
    }
140
141
    public function moderationEditAction()
142
    {
143
        $service = $this->getAdminGameService();
144
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
145
        $status = $this->getEvent()->getRouteMatch()->getParam('status');
146
147
        if (!$postId) {
148
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
149
        }
150
        $post = $service->getPostVotePostMapper()->findById($postId);
151
152
        if (! $post) {
153
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
154
        }
155
        $game = $post->getPostvote();
156
157
        if ($status && $status=='validation') {
158
            $post->setStatus(2);
159
            $service->getPostVotePostMapper()->update($post);
160
161
            return $this->redirect()->toUrl(
162
                $this->url()->fromRoute(
163
                    'admin/postvote/entry',
164
                    array('gameId' => $game->getId())
165
                )
166
            );
167
        } elseif ($status && $status=='rejection') {
168
            $post->setStatus(9);
169
            $service->getPostVotePostMapper()->update($post);
170
171
            return $this->redirect()->toUrl(
172
                $this->url()->fromRoute(
173
                    'admin/postvote/entry',
174
                    array('gameId' => $game->getId())
175
                )
176
            );
177
        }
178
179
        return array('game' => $game, 'post' => $post);
180
    }
181
    
182
    public function pushAction()
183
    {
184
        $service = $this->getAdminGameService();
185
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
186
        $pushed = $this->getEvent()->getRouteMatch()->getParam('pushed');
187
    
188
        if (!$postId) {
189
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
190
        }
191
        $post = $service->getPostVotePostMapper()->findById($postId);
192
    
193
        if (! $post) {
194
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
195
        }
196
        $game = $post->getPostvote();
197
    
198
        $post->setPushed($pushed);
199
        $service->getPostVotePostMapper()->update($post);
200
    
201
        return $this->redirect()->toUrl(
202
            $this->url()->fromRoute(
203
                'admin/postvote/entry',
204
                array('gameId' => $game->getId())
205
            )
206
        );
207
    }
208
209
    public function getAdminGameService()
210
    {
211
        if (!$this->adminGameService) {
212
            $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...
213
        }
214
215
        return $this->adminGameService;
216
    }
217
218
    public function setAdminGameService(AdminGameService $adminGameService)
219
    {
220
        $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...
221
222
        return $this;
223
    }
224
}
225