Completed
Push — master ( c0f250...321e24 )
by Filipe
15:10
created

Context::routeParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of slick/web_stack 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\WebStack\Controller;
11
12
use Aura\Router\Route;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Slick\Http\Message\Response;
16
use Slick\WebStack\Service\UriGeneratorInterface;
17
18
/**
19
 * Controller Context
20
 *
21
 * @package Slick\WebStack\Controller
22
 */
23
class Context implements ControllerContextInterface
24
{
25
    /**
26
     * @var ServerRequestInterface
27
     */
28
    private $request;
29
30
    /**
31
     * @var ResponseInterface
32
     */
33
    private $response;
34
35
    /**
36
     * @var Route
37
     */
38
    private $route;
39
40
    /**
41
     * @var bool
42
     */
43
    private $handleResponse = false;
44
45
    /**
46
     * @var UriGeneratorInterface
47
     */
48
    private $uriGenerator;
49
50
    /**
51
     * Creates a controller context
52
     *
53
     * @param ServerRequestInterface $request
54
     * @param Route $route
55
     * @param UriGeneratorInterface $uriGenerator
56
     */
57
    public function __construct(ServerRequestInterface $request, Route $route, UriGeneratorInterface $uriGenerator)
58
    {
59
        $this->request = $request;
60
        $this->route = $route;
61
        $this->uriGenerator = $uriGenerator;
62
    }
63
64
    /**
65
     * Gets the post parameter that was submitted with provided name
66
     *
67
     * If its not submitted the default value will be returned.
68
     * If no arguments are passed the full server parameters from request will
69
     * be returned. In this case the default value is ignored.
70
     *
71
     * @param null|string $name
72
     * @param mixed $default
73
     *
74
     * @return array|string
75
     */
76
    public function postParam($name = null, $default = null)
77
    {
78
        return $this->getData(
79
            $this->request->getParsedBody(),
80
            $name,
81
            $default
82
        );
83
    }
84
85
    /**
86
     * Gets the URL query parameter with provided name
87
     *
88
     * If its not submitted the default value will be returned.
89
     * If no arguments are passed the full URL query parameters from request will
90
     * be returned. In this case the default value is ignored.
91
     *
92
     * @param null|string $name
93
     * @param mixed $default
94
     *
95
     * @return array|string
96
     */
97
    public function queryParam($name = null, $default = null)
98
    {
99
        return $this->getData(
100
            $this->request->getQueryParams(),
101
            $name,
102
            $default
103
        );
104
    }
105
106
    /**
107
     * Gets the route 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 routeParam($name = null, $default = null)
119
    {
120
        return $this->getData($this->route->attributes, $name, $default);
121
    }
122
123
    /**
124
     * Sets a redirection header in the HTTP response
125
     *
126
     * @param string $location Location name, path or identifier
127
     * @param array $options Filter options
128
     *
129
     * @return void
130
     */
131
    public function redirect($location, array $options = [])
132
    {
133
        $this->disableRendering();
134
        $location = (string) $this->uriGenerator->generate($location, $options);
135
        $response = new Response(302, '', ['Location' => $location]);
136
        $this->setResponse($response);
137
    }
138
139
    /**
140
     * Disables response rendering
141
     *
142
     * @return self|ControllerContextInterface
143
     */
144
    public function disableRendering()
145
    {
146
        $this->handleResponse = true;
147
        return $this;
148
    }
149
150
    /**
151
     * Sets the view template to use by render process
152
     *
153
     * @param string $template
154
     *
155
     * @return self|ControllerContextInterface
156
     */
157
    public function useTemplate($template)
158
    {
159
        $this->request = $this->request->withAttribute('template', $template);
160
        return $this;
161
    }
162
163
    /**
164
     * Sets a new response
165
     *
166
     * @param ResponseInterface $response
167
     *
168
     * @return self|ControllerContextInterface
169
     */
170
    public function setResponse(ResponseInterface $response)
171
    {
172
        $this->response = $response;
173
        return $this;
174
    }
175
176
    /**
177
     * Sets a new or updated server request
178
     *
179
     * @param ServerRequestInterface $request
180
     *
181
     * @return self|ControllerContextInterface
182
     */
183
    public function changeRequest(ServerRequestInterface $request)
184
    {
185
        $this->request = $request;
186
        return $this;
187
    }
188
189
    /**
190
     * Get current HTTP response
191
     *
192
     * @return ResponseInterface
193
     */
194
    public function response()
195
    {
196
        return $this->response;
197
    }
198
199
    /**
200
     * Get current HTTP request
201
     *
202
     * @return ServerRequestInterface
203
     */
204
    public function request()
205
    {
206
        return $this->request;
207
    }
208
209
    /**
210
     * True when it handles the response
211
     *
212
     * @return boolean
213
     */
214
    public function handlesResponse()
215
    {
216
        return $this->handleResponse;
217
    }
218
219
    /**
220
     * Checks the request method
221
     *
222
     * @param string $methodName
223
     *
224
     * @return boolean
225
     */
226
    public function requestIs($methodName)
227
    {
228
        $method = $this->request->getMethod();
229
        return $method === strtoupper($methodName);
230
    }
231
232
    /**
233
     * Gets the value(s) from provided data
234
     *
235
     * @param mixed       $data
236
     * @param null|string $name
237
     * @param null|string $default
238
     *
239
     * @return mixed
240
     */
241
    private function getData($data, $name = null, $default = null)
242
    {
243
        if ($name == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $name of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
244
            return $data;
245
        }
246
247
        $value = $default;
248
        if (is_array($data) && array_key_exists($name, $data)) {
249
            $value = $data[$name];
250
        }
251
252
        return $value;
253
    }
254
}
255