Completed
Push — develop ( 6e7672...4f4759 )
by greg
02:18
created

TradingCardController::cooAction()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 31
nc 6
nop 0
1
<?php
2
3
namespace PlaygroundGame\Controller\Admin;
4
5
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter;
6
use PlaygroundCore\ORM\Pagination\LargeTablePaginator;
7
use PlaygroundGame\Controller\Admin\GameController;
8
use PlaygroundGame\Entity\TradingCard;
9
use PlaygroundGame\Entity\TradingCardModel;
10
use PlaygroundGame\Service\Game as AdminGameService;
11
use Zend\Paginator\Paginator;
12
use Zend\View\Model\ViewModel;
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
    public function addModelAction()
99
    {
100
        $this->checkGame();
101
102
        $viewModel = new ViewModel();
103
        $viewModel->setTemplate('playground-game/trading-card/model');
104
105
        $form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form');
106
        $form->get('submit')->setAttribute('label', 'Add');
107
108
        // $form->get('availability')->setOptions(array(
109
        //     'format' => 'Y-m-d H:i:s'
110
        // ));
111
112
        $form->setAttribute(
113
            'action',
114
            $this->url()->fromRoute(
115
                'admin/playgroundgame/tradingcard-model-add',
116
                array('gameId' => $this->game->getId())
117
            )
118
        );
119
        $form->setAttribute('method', 'post');
120
        $form->get('trading_card_id')->setAttribute('value', $this->game->getId());
121
        $model = new TradingCardModel();
122
        $form->bind($model);
123
124 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...
125
            $data = array_merge(
126
                $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...
127
                $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...
128
            );
129
130
            $model = $this->getAdminGameService()->updateModel($data, $model);
131
            if ($model) {
132
                // Redirect to list of games
133
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been created');
134
                return $this->redirect()->toRoute(
135
                    'admin/playgroundgame/tradingcard-model-list',
136
                    array('gameId' => $this->game->getId())
137
                );
138
            }
139
        }
140
        return $viewModel->setVariables(
141
            array(
142
                'form'     => $form,
143
                'game'     => $this->game,
144
                'model_id' => 0,
145
                'title'    => 'Add model',
146
            )
147
        );
148
    }
149
150
    public function editModelAction()
151
    {
152
        $this->checkGame();
153
154
        $viewModel = new ViewModel();
155
        $viewModel->setTemplate('playground-game/trading-card/model');
156
        $service = $this->getAdminGameService();
157
158
        $modelId = $this->getEvent()->getRouteMatch()->getParam('modelId');
159
        $model   = $service->getTradingCardModelMapper()->findById($modelId);
160
161
        $form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form');
162
        $form->remove('models_file');
163
164
        $form->get('submit')->setAttribute('label', 'Edit');
165
        $form->setAttribute('action', '');
166
167
        $form->get('trading_card_id')->setAttribute('value', $this->game->getId());
168
169
        $form->bind($model);
170
171 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...
172
            $data = array_merge(
173
                $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...
174
                $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...
175
            );
176
            $model = $service->updateModel($data, $model);
177
178
            if ($model) {
179
                // Redirect to list of games
180
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been edited');
181
                return $this->redirect()->toRoute(
182
                    'admin/playgroundgame/tradingcard-model-list',
183
                    array('gameId' => $this->game->getId())
184
                );
185
            }
186
        }
187
        return $viewModel->setVariables(
188
            array(
189
                'form'     => $form,
190
                'game'     => $this->game,
191
                'model_id' => $modelId,
192
                'title'    => 'Edit model',
193
            )
194
        );
195
    }
196
197
    /**
198
     * This function helps the admin to position UGC image on a card model
199
     */
200
    public function cooAction()
201
    {
202
        // get models
203
        $sg = $this->getAdminGameService();
204
        $this->checkGame();
205
206
        $models = $sg->getTradingCardModelMapper()->findBy(array('game' => $this->game));
207
208
        if ($this->getRequest()->isXmlHttpRequest() && $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 isXmlHttpRequest() 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...
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...
209
            $position = json_decode($this->getRequest()->getPost()->get('position'));
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...
210
            $i        = 0;
211
            foreach ($models as $model) {
212
                $jsonData = json_decode($model->getJsonData());
213
                if (isset($jsonData->coo)) {
214
                    $jsonData->coo = $position[$i];
215
                } else {
216
                    $jsonData = array('coo' => $position[$i]);
217
                }
218
219
                $model->setJsonData(json_encode($jsonData));
220
                $models = $sg->getTradingCardModelMapper()->update($model);
221
                ++$i;
222
            }
223
224
            // update coo of Model
225
            $jsonModel = new \Zend\View\Model\JsonModel();
226
            $jsonModel->setVariables(array(
227
                    'success' => true,
228
                ));
229
230
            return $jsonModel;
231
        }
232
233
        // ajout des images au tableau
234
        $bgs = [];
235
        $coo = [];
236
        foreach ($models as $model) {
237
            $json     = json_decode($model->getJsonData());
238
            $modelCoo = (isset($json->coo))?$json->coo:'';
239
            $bgs[]    = '/'.$model->getImage();
240
            $coo[]    = $modelCoo;
241
        }
242
243
        return array(
244
            'backgrounds' => $bgs,
245
            'face'        => 'img-test/photo.png',
246
            'coo'         => $coo,
247
        );
248
    }
249
250 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...
251
    {
252
        $service = $this->getAdminGameService();
253
        $modelId = $this->getEvent()->getRouteMatch()->getParam('modelId');
254
        if (!$modelId) {
255
            return $this->redirect()->toRoute('admin/playgroundgame/list');
256
        }
257
        $model         = $service->getTradingCardModelMapper()->findById($modelId);
258
        $tradingcardId = $model->getTradingCard()->getId();
259
260
        if ($model->getActive()) {
261
            $service->getTradingCardModelMapper()->remove($model);
262
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been deleted');
263
        } else {
264
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
265
                'cards have already been created with this model'
266
            );
267
        }
268
269
        return $this->redirect()->toRoute(
270
            'admin/playgroundgame/tradingcard-model-list',
271
            array('gameId' => $tradingcardId)
272
        );
273
    }
274
275
    public function getAdminGameService()
276
    {
277
        if (!$this->adminGameService) {
278
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_tradingcard_service');
279
        }
280
281
        return $this->adminGameService;
282
    }
283
284
    public function setAdminGameService(AdminGameService $adminGameService)
285
    {
286
        $this->adminGameService = $adminGameService;
287
288
        return $this;
289
    }
290
}
291