Completed
Push — master ( be93f6...2f2834 )
by greg
06:57 queued 03:44
created

QuizController::sortQuestionAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

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 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace PlaygroundGame\Controller\Admin;
4
5
use Zend\View\Model\JsonModel;
6
use PlaygroundGame\Entity\Quiz;
7
use PlaygroundGame\Entity\QuizQuestion;
8
use PlaygroundGame\Controller\Admin\GameController;
9
use PlaygroundGame\Service\Game as AdminGameService;
10
use Zend\View\Model\ViewModel;
11
use Zend\Paginator\Paginator;
12
13
class QuizController extends GameController
14
{
15
    /**
16
     * @var \PlaygroundGame\Service\Game
17
     */
18
    protected $adminGameService;
19
    protected $quizReplyAnswerMapper;
20
    protected $quizReplyMapper;
21
22
    public function listQuestionAction()
23
    {
24
        $service = $this->getAdminGameService();
25
        $quizId = $this->getEvent()->getRouteMatch()->getParam('quizId');
26
        if (!$quizId) {
27
            return $this->redirect()->toRoute('admin/playgroundgame/list');
28
        }
29
        $quiz = $service->getGameMapper()->findById($quizId);
30
        $questions = $service->getQuizQuestionMapper()->findByGameId($quizId);
31
32
        if (is_array($questions)) {
33
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($questions));
34
        } else {
35
            $paginator = $questions;
36
        }
37
38
        $paginator->setItemCountPerPage(50);
39
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
40
41
        return array(
42
            'questions' => $paginator,
43
            'quiz_id' => $quizId,
44
            'quiz' => $quiz,
45
        );
46
    }
47
48
    public function addQuestionAction()
49
    {
50
        $viewModel = new ViewModel();
51
        $viewModel->setTemplate('playground-game/quiz/question');
52
        $service = $this->getAdminGameService();
53
        $quizId = $this->getEvent()->getRouteMatch()->getParam('quizId');
54
55
        if (!$quizId) {
56
            return $this->redirect()->toRoute('admin/playgroundgame/list');
57
        }
58
59
        $form = $this->getServiceLocator()->get('playgroundgame_quizquestion_form');
60
        $form->get('submit')->setAttribute('label', 'Ajouter');
61
        $form->get('quiz_id')->setAttribute('value', $quizId);
62
        $form->setAttribute(
63
            'action',
64
            $this->url()->fromRoute('admin/playgroundgame/quiz-question-add', array('quizId' => $quizId))
65
        );
66
        $form->setAttribute('method', 'post');
67
68
        $question = new QuizQuestion();
69
        $form->bind($question);
70
71 View Code Duplication
        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...
Duplication introduced by
This code seems to be duplicated across 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
            $data = array_replace_recursive(
73
                $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...
74
                $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...
75
            );
76
77
            $question = $service->createQuestion($data);
78
            if ($question) {
79
                // Redirect to list of games
80
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was created');
81
82
                return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId));
83
            } else { // Creation failed
84
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
85
                    'The question was not updated - create at least one good answer'
86
                );
87
            }
88
        }
89
90
        return $viewModel->setVariables(array('form' => $form, 'quiz_id' => $quizId, 'question_id' => 0));
91
    }
92
93
    public function editQuestionAction()
94
    {
95
        $service = $this->getAdminGameService();
96
        $viewModel = new ViewModel();
97
        $viewModel->setTemplate('playground-game/quiz/question');
98
99
        $questionId = $this->getEvent()->getRouteMatch()->getParam('questionId');
100
        if (!$questionId) {
101
            return $this->redirect()->toRoute('admin/playgroundgame/list');
102
        }
103
        $question   = $service->getQuizQuestionMapper()->findById($questionId);
104
        $quizId     = $question->getQuiz()->getId();
105
106
        $form = $this->getServiceLocator()->get('playgroundgame_quizquestion_form');
107
        $form->get('submit')->setAttribute('label', 'Mettre à jour');
108
        $form->get('quiz_id')->setAttribute('value', $quizId);
109
        $form->setAttribute(
110
            'action',
111
            $this->url()->fromRoute('admin/playgroundgame/quiz-question-edit', array('questionId' => $questionId))
112
        );
113
        $form->setAttribute('method', 'post');
114
115
        $form->bind($question);
116
117 View Code Duplication
        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...
Duplication introduced by
This code seems to be duplicated across 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...
118
            $data = array_replace_recursive(
119
                $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...
120
                $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...
121
            );
122
123
            $question = $service->updateQuestion($data, $question);
124
            if ($question) {
125
                // Redirect to list of games
126
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was updated');
127
128
                return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId));
129
            } else {
130
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
131
                    'The question was not updated - create at least one good answer'
132
                );
133
            }
134
        }
135
136
        return $viewModel->setVariables(array('form' => $form, 'quiz_id' => $quizId, 'question_id' => $questionId));
137
    }
138
139
    public function removeQuestionAction()
140
    {
141
        $service = $this->getAdminGameService();
142
        $questionId = $this->getEvent()->getRouteMatch()->getParam('questionId');
143
        if (!$questionId) {
144
            return $this->redirect()->toRoute('admin/playgroundgame/list');
145
        }
146
        $question   = $service->getQuizQuestionMapper()->findById($questionId);
147
        $quizId     = $question->getQuiz()->getId();
148
149
        $service->getQuizQuestionMapper()->remove($question);
150
        $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was created');
151
152
        return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId));
153
    }
154
155
    public function sortQuestionAction()
156
    {
157
        $result = $this->getAdminGameService()->sortQuestion($this->params()->fromQuery('order'));
158
        $model = new JsonModel(array(
159
            'success' => $result,
160
        ));
161
        return $model->setTerminal(true);
162
    }
163
164 View Code Duplication
    public function createQuizAction()
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
        $viewModel = new ViewModel();
168
        $viewModel->setTemplate('playground-game/quiz/quiz');
169
170
        $gameForm = new ViewModel();
171
        $gameForm->setTemplate('playground-game/game/game-form');
172
173
        $quiz = new Quiz();
174
175
        $form = $this->getServiceLocator()->get('playgroundgame_quiz_form');
176
        $form->bind($quiz);
177
        $form->get('submit')->setAttribute('label', 'Add');
178
        $form->setAttribute(
179
            'action',
180
            $this->url()->fromRoute(
181
                'admin/playgroundgame/create-quiz',
182
                array('gameId' => 0)
183
            )
184
        );
185
        $form->setAttribute('method', 'post');
186
187
        $request = $this->getRequest();
188
        if ($request->isPost()) {
189
            $data = array_replace_recursive(
190
                $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...
191
                $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...
192
            );
193
            if (empty($data['prizes'])) {
194
                $data['prizes'] = array();
195
            }
196
            $game = $service->createOrUpdate($data, $quiz, 'playgroundgame_quiz_form');
197
            if ($game) {
198
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created');
199
200
                return $this->redirect()->toRoute('admin/playgroundgame/list');
201
            }
202
        }
203
        $gameForm->setVariables(array('form' => $form, 'game' => $quiz));
204
        $viewModel->addChild($gameForm, 'game_form');
205
206
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Create quiz'));
207
    }
208
209
    public function editQuizAction()
210
    {
211
        $this->checkGame();
212
213
        return $this->editGame(
214
            'playground-game/quiz/quiz',
215
            'playgroundgame_quiz_form'
216
        );
217
    }
218
219
    public function getAdminGameService()
220
    {
221
        if (!$this->adminGameService) {
222
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_quiz_service');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceLocator...oundgame_quiz_service') can also be of type array. However, the property $adminGameService is declared as type object<PlaygroundGame\Service\Game>. 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...
223
        }
224
225
        return $this->adminGameService;
226
    }
227
228
    public function setAdminGameService(AdminGameService $adminGameService)
229
    {
230
        $this->adminGameService = $adminGameService;
231
232
        return $this;
233
    }
234
}
235