Completed
Push — develop ( 0bc71f...f6430f )
by greg
41:42
created

PostVoteController::getAdminGameService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
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
    
17
    public function formAction()
18
    {
19
        $this->checkGame();
20
        
21
        $form = $this->game->getForm();
22
23
        return $this->createForm($form);
24
    }
25
    
26 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...
27
    {
28
        $service = $this->getAdminGameService();
29
        $viewModel = new ViewModel();
30
        $viewModel->setTemplate('playground-game/post-vote/postvote');
31
32
        $gameForm = new ViewModel();
33
        $gameForm->setTemplate('playground-game/game/game-form');
34
35
        $postVote = new PostVote();
36
37
        $form = $this->getServiceLocator()->get('playgroundgame_postvote_form');
38
        $form->bind($postVote);
39
        $form->get('submit')->setAttribute('label', 'Add');
40
        $form->setAttribute(
41
            'action',
42
            $this->url()->fromRoute(
43
                'admin/playgroundgame/create-postvote',
44
                array('gameId' => 0)
45
            )
46
        );
47
        $form->setAttribute('method', 'post');
48
49
        $request = $this->getRequest();
50
        if ($request->isPost()) {
51
            $data = array_replace_recursive(
52
                $this->getRequest()->getPost()->toArray(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

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

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

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

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

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

Available Fixes

  1. Change the type-hint for the parameter:

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

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

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53
                $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...
54
            );
55
            if (empty($data['prizes'])) {
56
                $data['prizes'] = array();
57
            }
58
            $game = $service->create($data, $postVote, 'playgroundgame_postvote_form');
59
            if ($game) {
60
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created');
61
62
                return $this->redirect()->toRoute('admin/playgroundgame/list');
63
            }
64
        }
65
        $gameForm->setVariables(array('form' => $form, 'game' => $postVote));
66
        $viewModel->addChild($gameForm, 'game_form');
67
68
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Create Post & Vote'));
69
    }
70
71 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...
72
    {
73
        $this->checkGame();
74
75
        $viewModel = new ViewModel();
76
        $viewModel->setTemplate('playground-game/post-vote/postvote');
77
78
        $gameForm = new ViewModel();
79
        $gameForm->setTemplate('playground-game/game/game-form');
80
81
        $form   = $this->getServiceLocator()->get('playgroundgame_postvote_form');
82
        $form->setAttribute(
83
            'action',
84
            $this->url()->fromRoute(
85
                'admin/playgroundgame/edit-postvote',
86
                array('gameId' => $this->game->getId())
87
            )
88
        );
89
        $form->setAttribute('method', 'post');
90
        
91
        if ($this->game->getFbAppId()) {
92
            $appIds = $form->get('fbAppId')->getOption('value_options');
93
            $appIds[$this->game->getFbAppId()] = $this->game->getFbAppId();
94
            $form->get('fbAppId')->setAttribute('options', $appIds);
95
        }
96
97
        $gameOptions = $this->getAdminGameService()->getOptions();
98
        $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css';
0 ignored issues
show
Bug introduced by
The variable $game does not exist. Did you forget to declare it?

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

Loading history...
99
        if (is_file($gameStylesheet)) {
100
            $values = $form->get('stylesheet')->getValueOptions();
101
            $values[$gameStylesheet] = 'Style personnalisé de ce jeu';
102
103
            $form->get('stylesheet')->setAttribute('options', $values);
104
        }
105
106
        $form->bind($this->game);
107
108
        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...
109
            $data = array_replace_recursive(
110
                $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...
111
                $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...
112
            );
113
            if (empty($data['prizes'])) {
114
                $data['prizes'] = array();
115
            }
116
            $result = $service->edit($data, $this->game, 'playgroundgame_postvote_form');
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you forget to declare it?

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

Loading history...
117
118
            if ($result) {
119
                return $this->redirect()->toRoute('admin/playgroundgame/list');
120
            }
121
        }
122
123
        $gameForm->setVariables(array('form' => $form, 'game' => $this->game));
124
        $viewModel->addChild($gameForm, 'game_form');
125
126
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit Post & Vote'));
127
    }
128
129 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...
130
    {
131
        $service = $this->getAdminGameService();
132
        $posts = $service->getPostVotePostMapper()->findBy(array('status' => 1));
133
134
        if (is_array($posts)) {
135
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($posts));
136
            $paginator->setItemCountPerPage(10);
137
            $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
138
        } else {
139
            $paginator = $posts;
140
        }
141
142
        return array('posts' => $paginator);
143
    }
144
145
    public function moderationEditAction()
146
    {
147
        $service = $this->getAdminGameService();
148
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
149
        $status = $this->getEvent()->getRouteMatch()->getParam('status');
150
151
        if (!$postId) {
152
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
153
        }
154
        $post = $service->getPostVotePostMapper()->findById($postId);
155
156
        if (! $post) {
157
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
158
        }
159
        $game = $post->getPostvote();
160
161
        if ($status && $status=='validation') {
162
            $post->setStatus(2);
163
            $service->getPostVotePostMapper()->update($post);
164
165
            return $this->redirect()->toUrl(
166
                $this->url()->fromRoute(
167
                    'admin/postvote/entry',
168
                    array('gameId' => $game->getId())
169
                )
170
            );
171
        } elseif ($status && $status=='rejection') {
172
            $post->setStatus(9);
173
            $service->getPostVotePostMapper()->update($post);
174
175
            return $this->redirect()->toUrl(
176
                $this->url()->fromRoute(
177
                    'admin/postvote/entry',
178
                    array('gameId' => $game->getId())
179
                )
180
            );
181
        }
182
183
        return array('game' => $game, 'post' => $post);
184
    }
185
    
186
    public function pushAction()
187
    {
188
        $service = $this->getAdminGameService();
189
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
190
        $pushed = $this->getEvent()->getRouteMatch()->getParam('pushed');
191
    
192
        if (!$postId) {
193
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
194
        }
195
        $post = $service->getPostVotePostMapper()->findById($postId);
196
    
197
        if (! $post) {
198
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
199
        }
200
        $game = $post->getPostvote();
201
    
202
        $post->setPushed($pushed);
203
        $service->getPostVotePostMapper()->update($post);
204
    
205
        return $this->redirect()->toUrl(
206
            $this->url()->fromRoute(
207
                'admin/postvote/entry',
208
                array('gameId' => $game->getId())
209
            )
210
        );
211
    }
212
213
    public function getAdminGameService()
214
    {
215
        if (!$this->adminGameService) {
216
            $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...
217
        }
218
219
        return $this->adminGameService;
220
    }
221
222
    public function setAdminGameService(AdminGameService $adminGameService)
223
    {
224
        $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...
225
226
        return $this;
227
    }
228
}
229