Controller::getSessionManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Faulancer\Controller;
4
5
use Faulancer\Exception\FileNotFoundException;
6
use Faulancer\Exception\InvalidArgumentException;
7
use Faulancer\Exception\ServiceNotFoundException;
8
use Faulancer\Http\Http;
9
use Faulancer\Http\Request;
10
use Faulancer\Http\Response;
11
use Faulancer\Service\AuthenticatorService;
12
use Faulancer\Service\DbService;
13
use Faulancer\Session\SessionManager;
14
use Faulancer\ServiceLocator\ServiceInterface;
15
use Faulancer\View\Helper\Route;
16
use Faulancer\View\ViewController;
17
use Faulancer\ServiceLocator\ServiceLocator;
18
19
/**
20
 * Class AbstractController
21
 *
22
 * @category Controller
23
 * @package  Faulancer\AbstractController
24
 * @author   Florian Knapp <[email protected]>
25
 */
26
class Controller
27
{
28
29
    /**
30
     * Contains the views per controller request
31
     *
32
     * @var array
33
     */
34
    private $_viewArray = [];
35
36
    /**
37
     * Contains the current request
38
     *
39
     * @var Request
40
     */
41
    protected $request;
42
43
    /**
44
     * AbstractController constructor.
45
     *
46
     * @param Request $request The request object
47
     */
48
    public function __construct(Request $request)
49
    {
50
        $this->request = $request;
51
    }
52
53
    /**
54
     * Returns the service locator
55
     *
56
     * @return ServiceLocator
57
     */
58
    public function getServiceLocator(): ServiceLocator
59
    {
60
        return ServiceLocator::instance();
61
    }
62
63
    /**
64
     * Returns the session manager
65
     *
66
     * @return SessionManager|ServiceInterface
67
     */
68
    public function getSessionManager(): SessionManager
69
    {
70
        return $this->getServiceLocator()->get(SessionManager::class);
71
    }
72
73
    /**
74
     * Returns the view controller
75
     *
76
     * @return ViewController
77
     */
78
    public function getView(): ViewController
79
    {
80
        $calledClass = get_called_class();
81
82
        if (in_array($calledClass, array_keys($this->_viewArray), true)) {
83
            return $this->_viewArray[$calledClass];
84
        }
85
86
        $viewController = new ViewController();
87
88
        $this->_viewArray[$calledClass] = $viewController;
89
90
        return $viewController;
91
92
    }
93
94
    /**
95
     * Returns the orm/entity manager
96
     *
97
     * @return DbService|ServiceInterface
98
     */
99
    public function getDb(): DbService
100
    {
101
        return $this->getServiceLocator()->get(DbService::class);
102
    }
103
104
    /**
105
     * Render view with given template
106
     *
107
     * @param string $template  The template to be rendered
108
     * @param array  $variables The variables for the template
109
     *
110
     * @return Response
111
     */
112
    public function render(string $template = '', array $variables = []) :Response
113
    {
114
        $this->addAssets();
115
116
        try {
117
118
            /** @var Response $response */
119
            $response = $this->getServiceLocator()->get(Response::class);
120
121
            $viewResult = $this->getView()
122
                ->setTemplate($template)
123
                ->setVariables($variables)
124
                ->render();
125
126
        } catch (FileNotFoundException $e) {
127
            $viewResult = $e->getMessage();
128
        } catch (ServiceNotFoundException $e) {
129
            $viewResult = $e->getMessage();
130
        }
131
132
        return $response->setContent($viewResult);
133
    }
134
135
    /**
136
     * Check if user is permitted based on his role(s)
137
     *
138
     * @param array $roles The corresponding user roles
139
     *
140
     * @return bool
141
     */
142
    public function isPermitted(array $roles = []): bool
143
    {
144
        /** @var AuthenticatorService $authService */
145
        $authService = $this->getServiceLocator()->get(AuthenticatorService::class);
146
147
        return $authService->isPermitted($roles);
148
    }
149
150
    /**
151
     * Redirect to specific uri
152
     *
153
     * @param string $uri The target uri
154
     *
155
     * @return bool
156
     *
157
     * @throws InvalidArgumentException
158
     */
159
    public function redirect(string $uri) :bool
160
    {
161
        /** @var Http $httpService */
162
        $httpService = $this->getServiceLocator()->get(Http::class);
163
        return $httpService->redirect($uri);
164
    }
165
166
    /**
167
     * Set a generic text token which is valid for exactly one call
168
     *
169
     * @param string $key     Key for the flash message
170
     * @param string $message Content for the flash message
171
     *
172
     * @return void
173
     */
174
    public function setFlashMessage(string $key, string $message)
175
    {
176
        $sessionManager = $this->getSessionManager();
177
        $sessionManager->setFlashMessage($key, $message);
0 ignored issues
show
Bug introduced by
The method setFlashMessage does only exist in Faulancer\Session\SessionManager, but not in Faulancer\ServiceLocator\ServiceInterface.

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...
178
    }
179
180
    /**
181
     * Retrieve a flash message
182
     *
183
     * @param string $key The flash message key
184
     *
185
     * @return string|null
186
     */
187
    public function getFlashMessage(string $key)
188
    {
189
        $sessionManager = $this->getSessionManager();
190
        return $sessionManager->getFlashMessage($key);
0 ignored issues
show
Bug introduced by
The method getFlashMessage does only exist in Faulancer\Session\SessionManager, but not in Faulancer\ServiceLocator\ServiceInterface.

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...
191
    }
192
193
    /**
194
     * Get the url for a specific route name
195
     *
196
     * @param string $name       Name of the route
197
     * @param array  $parameters Apply parameters where necessary
198
     * @param bool   $absolute   Return an absolute url with host as prefix
199
     *
200
     * @return string
201
     */
202
    public function route(string $name, array $parameters = [], bool $absolute = false)
203
    {
204
        return (new Route())($name, $parameters, $absolute);
205
    }
206
207
    /**
208
     * Return the current request object
209
     *
210
     * @return Request
211
     */
212
    public function getRequest() :Request
213
    {
214
        return $this->request;
215
    }
216
217
    /**
218
     * Add default assets for every action
219
     */
220
    protected function addAssets() {
221
        // Should be inherited by child classes
222
    }
223
224
}