Completed
Push — master ( 259751...17fe37 )
by greg
04:12 queued 01:22
created

HomeController::getSettingsService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
    /**
23
     * @var \PlaygroundDesign\Service\Settings
24
     */
25
    protected $settingsService;
26
27
    protected $options;
28
    
29
    /**
30
     *
31
     * @var ServiceManager
32
     */
33
    protected $serviceLocator;
34
35
    public function __construct(ServiceLocatorInterface $locator)
36
    {
37
        $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...
38
    }
39
40
    public function getServiceLocator()
41
    {
42
        return $this->serviceLocator;
43
    }
44
    
45
    public function indexAction()
46
    {
47
        $paginationSettings = $this->getSettingsService()->getSettingsMapper()->findById(1);
48
        $layoutViewModel = $this->layout();
49
50
        $slider = new ViewModel();
51
        $slider->setTemplate('playground-game/common/top_promo');
52
53
        $sliderGames = $this->getGameService()->getActiveSliderGames();
54
        $sliderPages = $this->getPageService()->getActiveSliderPages();
55
56
        // I merge both types of articles and sort them in reverse order of their key
57
        // And as their key is some sort of... date !, It means I sort it in date reverse order ;)
58
        $sliderItems = array_merge($sliderGames, $sliderPages);
59
60
        krsort($sliderItems);
61
62
        $slider->setVariables(array('sliderItems' => $sliderItems));
63
64
        $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...
65
66
        $games = $this->getGameService()->getActiveGames(true, '', '');
67
68
        $pages = $this->getPageService()->getActivePages();
69
70
        // I merge both types of articles and sort them in reverse order of their key
71
        // And as their key is some sort of... date !, It means I sort it in date reverse order ;)
72
        $items = array_merge($games, $pages);
73
        krsort($items);
74
75
        if (is_array($items)
76
            && $paginationSettings
77
            && $paginationSettings->getHomePagination() !== null
78
            && $paginationSettings->getHomePagination() > 0
79
        ) {
80
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($items));
81
            $paginator->setItemCountPerPage($paginationSettings->getHomePagination());
82
            $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
83
        } else {
84
            $paginator = $items;
85
        }
86
87
        $this->layout()->setVariables(['sliderItems' => $sliderItems]);
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...
88
89
        return new ViewModel(['items' => $paginator]);
90
    }
91
92
    public function shareAction()
93
    {
94
        $statusMail = null;
95
96
        $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form');
97
        $form->setAttribute('method', 'post');
98
99
        // buildView must be before sendMail because it adds the game template path to the templateStack
100
        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...
101
            $viewModel = new JsonModel();
102
        } else {
103
            $viewModel = new ViewModel();
104
        }
105
106
        $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...
107
108
        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...
109
            $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...
110
            $form->setData($data);
111
            if ($form->isValid()) {
112
                $subject = $this->serviceLocator->get('MvcTranslator')->translate(
113
                    $this->getOptions()->getDefaultSubjectLine(),
114
                    'playgroundgame'
115
                );
116
                $result = $this->getGameService()->sendShareMail($data, null, $user, null, 'share_game', $subject);
117
                if ($result) {
118
                    $statusMail = true;
119
                }
120
            }
121
        }
122
123
        $viewModel->setVariables(
124
            [
125
                'statusMail' => $statusMail,
126
                'form'       => $form,
127
            ]
128
        );
129
130
        return $viewModel;
131
    }
132
133
    public function getOptions()
134
    {
135
        if (!$this->options) {
136
            $this->setOptions($this->getServiceLocator()->get('playgroundgame_module_options'));
137
        }
138
139
        return $this->options;
140
    }
141
142
    public function setOptions($options)
143
    {
144
        $this->options = $options;
145
146
        return $this;
147
    }
148
    
149
    public function getGameService()
150
    {
151
        if (!$this->gameService) {
152
            $this->gameService = $this->getServiceLocator()->get('playgroundgame_game_service');
153
        }
154
    
155
        return $this->gameService;
156
    }
157
    
158
    public function setGameService(\PlaygroundGame\Service\Game $gameService)
159
    {
160
        $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...
161
    
162
        return $this;
163
    }
164
    
165
    public function getPageService()
166
    {
167
        if (!$this->pageService) {
168
            $this->pageService = $this->getServiceLocator()->get('playgroundcms_page_service');
169
        }
170
    
171
        return $this->pageService;
172
    }
173
174
    public function getSettingsService()
175
    {
176
        if (!$this->settingsService) {
177
            $this->settingsService = $this->getServiceLocator()->get('playgrounddesign_settings_service');
178
        }
179
    
180
        return $this->settingsService;
181
    }
182
    
183
    public function setPageService(\PlaygroundCms\Service\Page $pageService)
184
    {
185
        $this->pageService = $pageService;
186
    
187
        return $this;
188
    }
189
}
190