Completed
Push — develop ( 7ecade...971ef0 )
by greg
02:34
created

PostVoteController::moderationEditAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 4
nc 4
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 \PlaygroundGame\Service\Game
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->adminUrl()->fromRoute(
0 ignored issues
show
Documentation Bug introduced by
The method adminUrl does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
                '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, Zend\Psr7Bridge\Zend\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, Zend\Psr7Bridge\Zend\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->createOrUpdate($data, $postVote, 'playgroundgame_postvote_form');
59
            if ($game) {
60
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created');
0 ignored issues
show
Documentation Bug introduced by
The method flashMessenger does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
61
62
                return $this->redirect()->toUrl($this->adminUrl()->fromRoute('playgroundgame/list'));
0 ignored issues
show
Documentation Bug introduced by
The method adminUrl does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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->adminUrl()->fromRoute('postvote/entry', array('gameId' => 0)));
0 ignored issues
show
Documentation Bug introduced by
The method adminUrl does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
105
        }
106
        $post = $service->getPostVotePostMapper()->findById($postId);
107
108
        if (! $post) {
109
            return $this->redirect()->toUrl($this->adminUrl()->fromRoute('postvote/entry', array('gameId' => 0)));
0 ignored issues
show
Documentation Bug introduced by
The method adminUrl does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110
        }
111
        $game = $post->getPostvote();
112
113
        if ($status) {
114
            $service->moderatePost($post, $status);
115
116
            return $this->redirect()->toUrl(
117
                $this->adminUrl()->fromRoute(
0 ignored issues
show
Documentation Bug introduced by
The method adminUrl does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
118
                    'postvote/entry',
119
                    array('gameId' => $game->getId())
120
                )
121
            );
122
        }
123
124
        return array('game' => $game, 'post' => $post);
125
    }
126
    
127
    public function pushAction()
128
    {
129
        $service = $this->getAdminGameService();
130
        $postId = $this->getEvent()->getRouteMatch()->getParam('postId');
131
        $pushed = $this->getEvent()->getRouteMatch()->getParam('pushed');
132
    
133
        if (!$postId) {
134
            return $this->redirect()->toUrl($this->adminUrl()->fromRoute('postvote/entry', array('gameId' => 0)));
0 ignored issues
show
Documentation Bug introduced by
The method adminUrl does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
135
        }
136
        $post = $service->getPostVotePostMapper()->findById($postId);
137
    
138
        if (! $post) {
139
            return $this->redirect()->toUrl($this->adminUrl()->fromRoute('postvote/entry', array('gameId' => 0)));
0 ignored issues
show
Documentation Bug introduced by
The method adminUrl does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
140
        }
141
        $game = $post->getPostvote();
142
    
143
        $post->setPushed($pushed);
144
        $service->getPostVotePostMapper()->update($post);
145
    
146
        return $this->redirect()->toUrl(
147
            $this->adminUrl()->fromRoute(
0 ignored issues
show
Documentation Bug introduced by
The method adminUrl does not exist on object<PlaygroundGame\Co...min\PostVoteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
148
                'postvote/entry',
149
                array('gameId' => $game->getId())
150
            )
151
        );
152
    }
153
154
    public function getAdminGameService()
155
    {
156
        if (!$this->adminGameService) {
157
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_postvote_service');
158
        }
159
160
        return $this->adminGameService;
161
    }
162
163
    public function setAdminGameService(AdminGameService $adminGameService)
164
    {
165
        $this->adminGameService = $adminGameService;
166
167
        return $this;
168
    }
169
}
170