Completed
Push — develop ( 02d169...4593a9 )
by greg
02:27
created

HomeController::setPageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PlaygroundGame\Controller\Frontend;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use Zend\View\Model\ViewModel;
7
use Zend\View\Model\JsonModel;
8
use Zend\ServiceManager\ServiceLocatorInterface;
9
10
class HomeController extends AbstractActionController
11
{
12
    /**
13
     * @var \PlaygroundGame\Service\GameService
14
     */
15
    protected $gameService;
16
    
17
    /**
18
     * @var \PlaygroundCms\Service\Page
19
     */
20
    protected $pageService;
21
22
    protected $options;
23
    
24
    /**
25
     *
26
     * @var ServiceManager
27
     */
28
    protected $serviceLocator;
29
30
    public function __construct(ServiceLocatorInterface $locator)
31
    {
32
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $locator of type object<Zend\ServiceManag...erviceLocatorInterface> is incompatible with the declared type object<PlaygroundGame\Co...rontend\ServiceManager> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    public function getServiceLocator()
36
    {
37
        return $this->serviceLocator;
38
    }
39
    
40
    public function indexAction()
41
    {
42
        $layoutViewModel = $this->layout();
43
44
        $slider = new ViewModel();
45
        $slider->setTemplate('playground-game/common/top_promo');
46
47
        $sliderGames = $this->getGameService()->getActiveSliderGames();
48
        $sliderPages = $this->getPageService()->getActiveSliderPages();
49
50
        // I merge both types of articles and sort them in reverse order of their key
51
        // And as their key is some sort of... date !, It means I sort it in date reverse order ;)
52
        $sliderItems = array_merge($sliderGames, $sliderPages);
53
54
        krsort($sliderItems);
55
56
        $slider->setVariables(array('sliderItems' => $sliderItems));
57
58
        $layoutViewModel->addChild($slider, 'slider');
0 ignored issues
show
Bug introduced by
The method addChild does only exist in Zend\View\Model\ModelInterface, but not in Zend\Mvc\Controller\Plugin\Layout.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
59
60
        $games = $this->getGameService()->getActiveGames(true, '', '');
61
62
        $pages = $this->getPageService()->getActivePages();
63
64
        // I merge both types of articles and sort them in reverse order of their key
65
        // And as their key is some sort of... date !, It means I sort it in date reverse order ;)
66
        $items = array_merge($games, $pages);
67
        krsort($items);
68
69
        if (is_array($items)) {
70
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($items));
71
            $paginator->setItemCountPerPage(7);
72
            $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
73
        } else {
74
            $paginator = $items;
75
        }
76
77
        $this->layout()->setVariables(
0 ignored issues
show
Bug introduced by
The method setVariables does only exist in Zend\View\Model\ModelInterface, but not in Zend\Mvc\Controller\Plugin\Layout.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
78
            array(
79
                'sliderItems'    => $sliderItems,
80
            )
81
        );
82
83
        return new ViewModel(
84
            array(
85
                'items'    => $paginator,
86
               )
87
        );
88
    }
89
90
    public function shareAction()
91
    {
92
        $statusMail = null;
93
94
        $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form');
95
        $form->setAttribute('method', 'post');
96
97
        // buildView must be before sendMail because it adds the game template path to the templateStack
98
        if ($this->getRequest()->isXmlHttpRequest()) {
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, Zend\Psr7Bridge\Zend\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
99
            $viewModel = new JsonModel();
100
        } else {
101
            $viewModel = new ViewModel();
102
        }
103
104
        $user = $this->zfcUserAuthentication()->getIdentity();
0 ignored issues
show
Documentation Bug introduced by
The method zfcUserAuthentication does not exist on object<PlaygroundGame\Co...rontend\HomeController>? Since you implemented __call, maybe consider adding a @method annotation.

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

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

class ParentClass {
    private $data = array();

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

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

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
105
106
        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, Zend\Psr7Bridge\Zend\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
107
            $data = $this->getRequest()->getPost()->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request, Zend\Psr7Bridge\Zend\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
108
            $form->setData($data);
109
            if ($form->isValid()) {
110
                $subject = $this->serviceLocator->get('MvcTranslator')->translate(
111
                    $this->getOptions()->getDefaultSubjectLine(),
112
                    'playgroundgame'
113
                );
114
                $result = $this->getGameService()->sendShareMail($data, null, $user, null, 'share_game', $subject);
115
                if ($result) {
116
                    $statusMail = true;
117
                }
118
            }
119
        }
120
121
        $viewModel->setVariables(
122
            [
123
                'statusMail' => $statusMail,
124
                'form'       => $form,
125
            ]
126
        );
127
128
        return $viewModel;
129
    }
130
131
    public function getOptions()
132
    {
133
        if (!$this->options) {
134
            $this->setOptions($this->getServiceLocator()->get('playgroundgame_module_options'));
135
        }
136
137
        return $this->options;
138
    }
139
140
    public function setOptions($options)
141
    {
142
        $this->options = $options;
143
144
        return $this;
145
    }
146
    
147
    public function getGameService()
148
    {
149
        if (!$this->gameService) {
150
            $this->gameService = $this->getServiceLocator()->get('playgroundgame_game_service');
151
        }
152
    
153
        return $this->gameService;
154
    }
155
    
156
    public function setGameService(\PlaygroundGame\Service\Game $gameService)
157
    {
158
        $this->gameService = $gameService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gameService of type object<PlaygroundGame\Service\Game> is incompatible with the declared type object<PlaygroundGame\Service\GameService> of property $gameService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
159
    
160
        return $this;
161
    }
162
    
163
    public function getPageService()
164
    {
165
        if (!$this->pageService) {
166
            $this->pageService = $this->getServiceLocator()->get('playgroundcms_page_service');
167
        }
168
    
169
        return $this->pageService;
170
    }
171
    
172
    public function setPageService(\PlaygroundCms\Service\Page $pageService)
173
    {
174
        $this->pageService = $pageService;
175
    
176
        return $this;
177
    }
178
}
179