Completed
Push — master ( 4e1f37...568ba2 )
by Filipe
02:24 queued 15s
created

Controller::getRouterGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc;
11
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Slick\Http\PhpEnvironment\Request;
15
use Slick\Http\PhpEnvironment\Response;
16
17
/**
18
 * Controller
19
 *
20
 * @package Slick\Mvc
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
abstract class Controller implements ControllerInterface
24
{
25
26
    /**
27
     * @var ServerRequestInterface|Request
28
     */
29
    protected $request;
30
31
    /**
32
     * @var ResponseInterface|Response
33
     */
34
    protected $response;
35
36
    /**
37
     * Registers the current HTTP request and response
38
     *
39
     * @param ServerRequestInterface $request
40
     * @param ResponseInterface      $response
41
     *
42
     * @return Controller|$this|self|ControllerInterface
43
     */
44 20
    public function register(
45
        ServerRequestInterface $request, ResponseInterface $response
46
    ) {
47 20
        $this->request = $request;
48 20
        $this->response = $response;
49 20
        return $this;
50
    }
51
52
    /**
53
     * Gets updated HTTP response
54
     *
55
     * @return ResponseInterface
56
     */
57 10
    public function getResponse()
58
    {
59 10
        return $this->response;
60
    }
61
62
    /**
63
     * Gets updated HTTP request
64
     *
65
     * @return ServerRequestInterface
66
     */
67 14
    public function getRequest()
68
    {
69 14
        return $this->request;
70
    }
71
72
    /**
73
     * Sets a value to be used by render
74
     *
75
     * The key argument can be an associative array with values to be set
76
     * or a string naming the passed value. If an array is given then the
77
     * value will be ignored.
78
     *
79
     * Those values must be set in the request attributes so they can be used
80
     * latter by any other middle ware in the stack.
81
     *
82
     * @param string|array $key
83
     * @param mixed        $value
84
     *
85
     * @return Controller|$this|self|ControllerInterface
86
     */
87 8
    public function set($key, $value = null)
88
    {
89 8
        if (is_string($key)) {
90 6
            return $this->registerVar($key, $value);
91
        }
92
93 2
        foreach ($key as $name => $value) {
94 2
            $this->registerVar($name, $value);
95 2
        }
96 2
        return $this;
97
    }
98
99
    /**
100
     * Enables or disables rendering
101
     *
102
     * @param bool $disable
103
     * @return ControllerInterface|self|$this
104
     */
105 2
    public function disableRendering($disable = true)
106
    {
107 2
        $this->request = $this->request->withAttribute('render', !$disable);
108 2
        return $this;
109
    }
110
111
    /**
112
     * Changes the current rendering template
113
     *
114
     * @param string $template
115
     * @return ControllerInterface|self|$this
116
     */
117 2
    public function setView($template)
118
    {
119 2
        $this->request = $this->request->withAttribute('template', $template);
120 2
        return $this;
121
    }
122
123
    /**
124
     * Redirects the flow to another route/path
125
     *
126
     * @param string $path the route or path to redirect to
127
     *
128
     * @return Controller|self|$this
129
     */
130 4
    public function redirect($path)
131
    {
132 4
        $regExp = '/\/\/|https?:/i';
133 4
        if (preg_match($regExp, $path)) {
134 2
            $this->response = $this->createRedirectResponse($path);
135 2
            return $this;
136
        }
137 2
        $generated = call_user_func_array(
138 2
            [$this->getRouterGenerator(), 'generate'],
139 2
            func_get_args()
140 2
        );
141
        $path = $generated
142 2
            ? $generated
143 2
            : $path;
144 2
        $basePath = rtrim($this->request->getBasePath(), '/');
0 ignored issues
show
Bug introduced by
The method getBasePath does only exist in Slick\Http\PhpEnvironment\Request, but not in Psr\Http\Message\ServerRequestInterface.

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...
145 2
        $this->response = $this->createRedirectResponse("{$basePath}/{$path}");
146 2
        return $this;
147
    }
148
149
    /**
150
     * Register a variable value
151
     *
152
     * @param string $key
153
     * @param mixed $value
154
     *
155
     * @return Controller|$this|self
156
     */
157 8
    protected function registerVar($key, $value)
158
    {
159 8
        $attrName = ControllerInterface::REQUEST_ATTR_VIEW_DATA;
160 8
        $attributes = $this->request->getAttributes();
161 8
        $attributes[$attrName][$key] = $value;
162 8
        $this->request = $this->request
163 8
            ->withAttribute($attrName, $attributes[$attrName]);
164 8
        return $this;
165
    }
166
167
    /**
168
     * Return Router path generator
169
     *
170
     * @return \Aura\Router\Generator
171
     */
172 2
    protected function getRouterGenerator()
173
    {
174
        /** @var Router $router */
175 2
        $router = Application::container()->get('router.middleware');
176 2
        return $router->getRouterContainer()->getGenerator();
177
    }
178
179
    /**
180
     * Creates a redirect response for provided path
181
     * 
182
     * @param string $path
183
     * 
184
     * @return Response
185
     */
186 4
    protected function createRedirectResponse($path)
187
    {
188 4
        $response = $this->response->withStatus(302)
189 4
            ->withHeader('Location', $path);
190 4
        return $response;
191
    }
192
}