Completed
Push — develop ( 89108e...7bb4ed )
by greg
03:09
created

PostVoteController::moderationEditAction()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 40
rs 6.7273
cc 7
eloc 25
nc 5
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
    public function editPostVoteAction()
72
    {
73
        $this->checkGame();
74
75
        return $this->editGame(
76
            'playground-game/post-vote/postvote',
77
            'playgroundgame_postvote_form'
78
        );
79
    }
80
81 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...
82
    {
83
        $service = $this->getAdminGameService();
84
        $posts = $service->getPostVotePostMapper()->findBy(array('status' => 1));
85
86
        if (is_array($posts)) {
87
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($posts));
88
            $paginator->setItemCountPerPage(10);
89
            $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
90
        } else {
91
            $paginator = $posts;
92
        }
93
94
        return array('posts' => $paginator);
95
    }
96
97
    public function moderationEditAction()
98
    {
99
        $service = $this->getAdminGameService();
100
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
101
        $status = $this->getEvent()->getRouteMatch()->getParam('status');
102
103
        if (!$postId) {
104
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
105
        }
106
        $post = $service->getPostVotePostMapper()->findById($postId);
107
108
        if (! $post) {
109
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
110
        }
111
        $game = $post->getPostvote();
112
113
        if ($status && $status=='validation') {
114
            $post->setStatus(2);
115
            $service->getPostVotePostMapper()->update($post);
116
117
            return $this->redirect()->toUrl(
118
                $this->url()->fromRoute(
119
                    'admin/postvote/entry',
120
                    array('gameId' => $game->getId())
121
                )
122
            );
123
        } elseif ($status && $status=='rejection') {
124
            $post->setStatus(9);
125
            $service->getPostVotePostMapper()->update($post);
126
127
            return $this->redirect()->toUrl(
128
                $this->url()->fromRoute(
129
                    'admin/postvote/entry',
130
                    array('gameId' => $game->getId())
131
                )
132
            );
133
        }
134
135
        return array('game' => $game, 'post' => $post);
136
    }
137
    
138
    public function pushAction()
139
    {
140
        $service = $this->getAdminGameService();
141
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
142
        $pushed = $this->getEvent()->getRouteMatch()->getParam('pushed');
143
    
144
        if (!$postId) {
145
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
146
        }
147
        $post = $service->getPostVotePostMapper()->findById($postId);
148
    
149
        if (! $post) {
150
            return $this->redirect()->toUrl($this->url()->fromRoute('admin/postvote/entry', array('gameId' => 0)));
151
        }
152
        $game = $post->getPostvote();
153
    
154
        $post->setPushed($pushed);
155
        $service->getPostVotePostMapper()->update($post);
156
    
157
        return $this->redirect()->toUrl(
158
            $this->url()->fromRoute(
159
                'admin/postvote/entry',
160
                array('gameId' => $game->getId())
161
            )
162
        );
163
    }
164
165
    public function getAdminGameService()
166
    {
167
        if (!$this->adminGameService) {
168
            $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...
169
        }
170
171
        return $this->adminGameService;
172
    }
173
174
    public function setAdminGameService(AdminGameService $adminGameService)
175
    {
176
        $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...
177
178
        return $this;
179
    }
180
}
181