Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

Context::setResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\Controller;
11
12
use Interop\Container\ContainerInterface as InteropContainer;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Slick\Di\ContainerInjectionInterface;
16
use Slick\Mvc\Service\UriGeneratorInterface;
17
18
/**
19
 * Context
20
 *
21
 * @package Slick\Mvc\Controller
22
 * @author  Filipe Silva <[email protected]>
23
 */
24
class Context implements
25
    ControllerContextInterface,
26
    ContainerInjectionInterface
27
{
28
    /**
29
     * @var ResponseInterface
30
     */
31
    private $response;
32
33
    /**
34
     * @var ServerRequestInterface
35
     */
36
    private $request;
37
    /**
38
     * @var UriGeneratorInterface
39
     */
40
    private $uriGenerator;
41
42
    /**
43
     * Creates a controller context
44
     *
45
     * @param UriGeneratorInterface $uriGenerator
46
     */
47
    public function __construct(UriGeneratorInterface $uriGenerator)
48
    {
49
        $this->uriGenerator = $uriGenerator;
50
    }
51
52
    /**
53
     * Instantiates a new instance of this class.
54
     *
55
     * This is a factory method that returns a new instance of this class. The
56
     * factory should pass any needed dependencies into the constructor of this
57
     * class, but not the container itself. Every call to this method must return
58
     * a new instance of this class; that is, it may not implement a singleton.
59
     *
60
     * @param InteropContainer $container
61
     *   The service container this instance should use.
62
     *
63
     * @return Context
64
     */
65
    public static function create(InteropContainer $container)
66
    {
67
        return new Context($container->get(UriGeneratorInterface::class));
68
    }
69
70
    /**
71
     * Registers the HTTP request and response to this context
72
     *
73
     * @param ServerRequestInterface $request
74
     * @param ResponseInterface $response
75
     *
76
     * @return Context|ControllerContextInterface
77
     */
78
    public function register(
79
        ServerRequestInterface $request,
80
        ResponseInterface $response
81
    )
82
    {
83
        $this->request = $request;
84
        $this->setResponse($response);
85
        return $this;
86
    }
87
88
    /**
89
     * Gets the post parameter that was submitted with provided name
90
     *
91
     * If its not submitted the default value will be returned.
92
     * If no arguments are passed the full server parameters from request will
93
     * be returned. In this case the default value is ignored.
94
     *
95
     * @param null|string $name
96
     * @param mixed       $default
97
     *
98
     * @return array|string
99
     */
100
    public function getPost($name = null, $default = null)
101
    {
102
        $parameters = $this->request->getServerParams();
103
        return $this->getDataFrom($parameters, $name, $default);
104
    }
105
106
    /**
107
     * Gets the URL query parameter with provided name
108
     *
109
     * If its not submitted the default value will be returned.
110
     * If no arguments are passed the full URL query parameters from request will
111
     * be returned. In this case the default value is ignored.
112
     *
113
     * @param null|string $name
114
     * @param mixed       $default
115
     *
116
     * @return array|string
117
     */
118
    public function getQuery($name = null, $default = null)
119
    {
120
        $parameters = $this->request->getQueryParams();
121
        return $this->getDataFrom($parameters, $name, $default);
122
    }
123
124
    /**
125
     * Sets a redirection header in the HTTP response
126
     *
127
     * @param string $location Location name, path or identifier
128
     * @param array  $options  Filter options
129
     *
130
     * @return void
131
     */
132
    public function redirect($location, array $options = [])
133
    {
134
        $response = $this->response
135
            ->withStatus(302)
136
            ->withHeader(
137
                'location',
138
                $this->uriGenerator->generate($location, $options)
0 ignored issues
show
Documentation introduced by
$this->uriGenerator->gen...te($location, $options) is of type object<Psr\Http\Message\UriInterface>|null, but the function expects a string|array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
            )
140
        ;
141
        $this->setResponse($response);
142
    }
143
144
    /**
145
     * Disables response rendering
146
     *
147
     * @return Context|ControllerContextInterface
148
     */
149
    public function disableRendering()
150
    {
151
        $this->request = $this->request->withAttribute('rendering', false);
152
        return $this;
153
    }
154
155
    /**
156
     * Sets the view that will be rendered
157
     *
158
     * @param string $viewPath
159
     *
160
     * @return Context|ControllerContextInterface
161
     */
162
    public function setView($viewPath)
163
    {
164
        $this->request = $this->request
165
            ->withAttribute('view', $viewPath);
166
        return $this;
167
    }
168
169
    /**
170
     * Sets a new response
171
     *
172
     * @param ResponseInterface $response
173
     *
174
     * @return Context|ControllerContextInterface
175
     */
176
    public function setResponse(ResponseInterface $response)
177
    {
178
        $this->response = $response;
179
        return $this;
180
    }
181
182
    /**
183
     * Get current HTTP response
184
     *
185
     * @return ResponseInterface
186
     */
187
    public function getResponse()
188
    {
189
        return $this->response;
190
    }
191
192
    /**
193
     * Get current HTTP request
194
     *
195
     * @return ServerRequestInterface
196
     */
197
    public function getRequest()
198
    {
199
        return $this->request;
200
    }
201
202
    /**
203
     * Gets the parameters with provided name from parameters
204
     *
205
     * @param array       $parameters
206
     * @param string|null $name
207
     * @param mixed       $default
208
     *
209
     * @return array|string|mixed
210
     */
211
    private function getDataFrom(
212
        array $parameters,
213
        $name = null,
214
        $default = null
215
    ) {
216
        if ($name === null) {
217
            return $parameters;
218
        }
219
220
        $value = array_key_exists($name, $parameters)
221
            ? $parameters[$name]
222
            : $default;
223
224
        return $value;
225
    }
226
227
}