Completed
Push — master ( a5446b...0a03f7 )
by greg
06:15 queued 03:04
created

TradingCardController::setAdminGameService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace PlaygroundGame\Controller\Admin;
4
5
use PlaygroundGame\Service\Game as AdminGameService;
6
use PlaygroundGame\Entity\TradingCard;
7
use PlaygroundGame\Entity\TradingCardModel;
8
use PlaygroundGame\Controller\Admin\GameController;
9
use Zend\View\Model\ViewModel;
10
use Zend\Paginator\Paginator;
11
use PlaygroundCore\ORM\Pagination\LargeTablePaginator;
12
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter;
13
14
class TradingCardController extends GameController
15
{
16
    /**
17
     * @var \PlaygroundGame\Service\Game
18
     */
19
    protected $adminGameService;
20
21 View Code Duplication
    public function createTradingcardAction()
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...
22
    {
23
        $service = $this->getAdminGameService();
24
        $viewModel = new ViewModel();
25
        $viewModel->setTemplate('playground-game/trading-card/tradingcard');
26
27
        $gameForm = new ViewModel();
28
        $gameForm->setTemplate('playground-game/game/game-form');
29
30
        $tradingcard = new TradingCard();
31
32
        $form = $this->getServiceLocator()->get('playgroundgame_tradingcard_form');
33
        $form->bind($tradingcard);
34
        $form->get('submit')->setAttribute('label', 'Add');
35
        $form->setAttribute(
36
            'action',
37
            $this->url()->fromRoute(
38
                'admin/playgroundgame/create-tradingcard',
39
                array('gameId' => 0)
40
            )
41
        );
42
        $form->setAttribute('method', 'post');
43
44
        $request = $this->getRequest();
45
        if ($request->isPost()) {
46
            $data = array_replace_recursive(
47
                $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...
48
                $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...
49
            );
50
            if (empty($data['prizes'])) {
51
                $data['prizes'] = array();
52
            }
53
            $game = $service->createOrUpdate($data, $tradingcard, 'playgroundgame_tradingcard_form');
54
            if ($game) {
55
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been created');
56
57
                return $this->redirect()->toRoute('admin/playgroundgame/list');
58
            }
59
        }
60
        $gameForm->setVariables(array('form' => $form, 'game' => $tradingcard));
61
        $viewModel->addChild($gameForm, 'game_form');
62
63
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Create trading card'));
64
    }
65
66
    public function editTradingcardAction()
67
    {
68
        $this->checkGame();
69
70
        return $this->editGame(
71
            'playground-game/trading-card/tradingcard',
72
            'playgroundgame_tradingcard_form'
73
        );
74
    }
75
76 View Code Duplication
    public function listModelAction()
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...
77
    {
78
        $this->checkGame();
79
80
        $adapter = new DoctrineAdapter(
81
            new LargeTablePaginator(
82
                $this->getAdminGameService()->getTradingCardModelMapper()->queryByGame($this->game)
83
            )
84
        );
85
        $paginator = new Paginator($adapter);
86
        $paginator->setItemCountPerPage(25);
87
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
88
89
        return new ViewModel(
90
            array(
91
                'models' => $paginator,
92
                'gameId'      => $this->game->getId(),
93
                'game'        => $this->game,
94
            )
95
        );
96
    }
97
98
99
    public function addModelAction()
100
    {
101
        $this->checkGame();
102
103
        $viewModel = new ViewModel();
104
        $viewModel->setTemplate('playground-game/trading-card/model');
105
106
        $form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form');
107
        $form->get('submit')->setAttribute('label', 'Add');
108
109
        // $form->get('availability')->setOptions(array(
110
        //     'format' => 'Y-m-d H:i:s'
111
        // ));
112
113
        $form->setAttribute(
114
            'action',
115
            $this->url()->fromRoute(
116
                'admin/playgroundgame/tradingcard-model-add',
117
                array('gameId' => $this->game->getId())
118
            )
119
        );
120
        $form->setAttribute('method', 'post');
121
        $form->get('trading_card_id')->setAttribute('value', $this->game->getId());
122
        $model = new TradingCardModel();
123
        $form->bind($model);
124
125 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...
126
            $data = array_merge(
127
                $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...
128
                $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...
129
            );
130
            
131
            $model = $this->getAdminGameService()->updateModel($data, $model);
132
            if ($model) {
133
                // Redirect to list of games
134
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been created');
135
                return $this->redirect()->toRoute(
136
                    'admin/playgroundgame/tradingcard-model-list',
137
                    array('gameId'=>$this->game->getId())
138
                );
139
            }
140
        }
141
        return $viewModel->setVariables(
142
            array(
143
                'form' => $form,
144
                'game' => $this->game,
145
                'model_id' => 0,
146
                'title' => 'Add model',
147
            )
148
        );
149
    }
150
151
    public function editModelAction()
152
    {
153
        $this->checkGame();
154
155
        $viewModel = new ViewModel();
156
        $viewModel->setTemplate('playground-game/trading-card/model');
157
        $service = $this->getAdminGameService();
158
159
        $modelId = $this->getEvent()->getRouteMatch()->getParam('modelId');
160
        $model = $service->getTradingCardModelMapper()->findById($modelId);
161
162
        $form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form');
163
        $form->remove('models_file');
164
165
        $form->get('submit')->setAttribute('label', 'Edit');
166
        $form->setAttribute('action', '');
167
168
        $form->get('trading_card_id')->setAttribute('value', $this->game->getId());
169
170
        $form->bind($model);
171
172 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...
173
            $data = array_merge(
174
                $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...
175
                $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...
176
            );
177
            $model = $service->updateModel($data, $model);
178
179
            if ($model) {
180
                // Redirect to list of games
181
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been edited');
182
                return $this->redirect()->toRoute(
183
                    'admin/playgroundgame/tradingcard-model-list',
184
                    array('gameId'=>$this->game->getId())
185
                );
186
            }
187
        }
188
        return $viewModel->setVariables(
189
            array(
190
                'form' => $form,
191
                'game' => $this->game,
192
                'model_id' => $modelId,
193
                'title' => 'Edit model',
194
            )
195
        );
196
    }
197
198
199 View Code Duplication
    public function removeOccurrenceAction()
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...
200
    {
201
        $service = $this->getAdminGameService();
202
        $modelId = $this->getEvent()->getRouteMatch()->getParam('modelId');
203
        if (!$modelId) {
204
            return $this->redirect()->toRoute('admin/playgroundgame/list');
205
        }
206
        $model   = $service->getTradingCardModelMapper()->findById($modelId);
207
        $tradingcardId = $model->getTradingCard()->getId();
208
209
        if ($model->getActive()) {
210
            $service->getTradingCardModelMapper()->remove($model);
211
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been deleted');
212
        } else {
213
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
214
                'cards have already been created with this model'
215
            );
216
        }
217
218
        return $this->redirect()->toRoute(
219
            'admin/playgroundgame/tradingcard-model-list',
220
            array('gameId'=>$tradingcardId)
221
        );
222
    }
223
224
    public function getAdminGameService()
225
    {
226
        if (!$this->adminGameService) {
227
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_tradingcard_service');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceLocator...e_tradingcard_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...
228
        }
229
230
        return $this->adminGameService;
231
    }
232
233
    public function setAdminGameService(AdminGameService $adminGameService)
234
    {
235
        $this->adminGameService = $adminGameService;
236
237
        return $this;
238
    }
239
}
240