Completed
Push — master ( aafb5f...ed91ed )
by Christian
14s queued 10s
created

View   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 362
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Test Coverage

Coverage 87.32%

Importance

Changes 0
Metric Value
wmc 28
lcom 3
cbo 3
dl 0
loc 362
ccs 62
cts 71
cp 0.8732
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A createRedirect() 0 7 1
A createRouteRedirect() 0 12 1
A __construct() 0 9 2
A setData() 0 6 1
A setHeader() 0 6 1
A setHeaders() 0 6 1
A setStatusCode() 0 8 2
A setContext() 0 6 1
A setFormat() 0 6 1
A setLocation() 0 7 1
A setRoute() 0 7 1
A setRouteParameters() 0 6 1
A setResponse() 0 6 1
A getData() 0 4 1
A getStatusCode() 0 4 1
A getHeaders() 0 4 1
A getFormat() 0 4 1
A getLocation() 0 4 1
A getRoute() 0 4 1
A getRouteParameters() 0 4 1
A getResponse() 0 12 3
A getContext() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\View;
13
14
use FOS\RestBundle\Context\Context;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * Default View implementation.
19
 *
20
 * @author Johannes M. Schmitt <[email protected]>
21
 * @author Lukas K. Smith <[email protected]>
22
 */
23
class View
24
{
25
    /**
26
     * @var mixed|null
27
     */
28
    private $data;
29
30
    /**
31
     * @var int|null
32
     */
33
    private $statusCode;
34
35
    /**
36
     * @var string|null
37
     */
38
    private $format;
39
40
    /**
41
     * @var string|null
42
     */
43
    private $location;
44
45
    /**
46
     * @var string|null
47
     */
48
    private $route;
49
50
    /**
51
     * @var array|null
52
     */
53
    private $routeParameters;
54
55
    /**
56
     * @var Context
57
     */
58
    private $context;
59
60
    /**
61
     * @var Response
62
     */
63
    private $response;
64
65
    /**
66
     * Convenience method to allow for a fluent interface.
67
     *
68
     * @param mixed $data
69
     * @param int   $statusCode
70
     * @param array $headers
71
     *
72
     * @return static
73
     */
74
    public static function create($data = null, $statusCode = null, array $headers = [])
75
    {
76
        return new static($data, $statusCode, $headers);
77
    }
78
79
    /**
80
     * Convenience method to allow for a fluent interface while creating a redirect to a
81
     * given url.
82
     *
83
     * @param string $url
84
     * @param int    $statusCode
85
     * @param array  $headers
86
     *
87
     * @return static
88
     */
89
    public static function createRedirect($url, $statusCode = Response::HTTP_FOUND, array $headers = [])
90
    {
91
        $view = static::create(null, $statusCode, $headers);
92
        $view->setLocation($url);
93
94
        return $view;
95 6
    }
96
97 6
    /**
98
     * Convenience method to allow for a fluent interface while creating a redirect to a
99
     * given route.
100
     *
101
     * @param string $route
102
     * @param array  $parameters
103
     * @param int    $statusCode
104
     * @param array  $headers
105
     *
106
     * @return static
107
     */
108
    public static function createRouteRedirect(
109
        $route,
110 1
        array $parameters = [],
111
        $statusCode = Response::HTTP_FOUND,
112 1
        array $headers = []
113 1
    ) {
114
        $view = static::create(null, $statusCode, $headers);
115 1
        $view->setRoute($route);
116
        $view->setRouteParameters($parameters);
117
118
        return $view;
119
    }
120
121
    /**
122
     * Constructor.
123
     *
124
     * @param mixed $data
125
     * @param int   $statusCode
126
     * @param array $headers
127
     */
128
    public function __construct($data = null, $statusCode = null, array $headers = [])
129 2
    {
130
        $this->setData($data);
131
        $this->setStatusCode($statusCode);
132
133
        if (!empty($headers)) {
134
            $this->getResponse()->headers->replace($headers);
135 2
        }
136 2
    }
137 2
138
    /**
139 2
     * Sets the data.
140
     *
141
     * @param mixed $data
142
     *
143
     * @return View
144
     */
145
    public function setData($data)
146
    {
147
        $this->data = $data;
148
149 87
        return $this;
150
    }
151 87
152 87
    /**
153 87
     * Sets a header.
154
     *
155 87
     * @param string $name
156 1
     * @param string $value
157 1
     *
158 87
     * @return View
159
     */
160
    public function setHeader($name, $value)
161
    {
162
        $this->getResponse()->headers->set($name, $value);
163
164
        return $this;
165
    }
166
167 87
    /**
168
     * Sets the headers.
169 87
     *
170
     * @param array $headers
171 87
     *
172
     * @return View
173
     */
174
    public function setHeaders(array $headers)
175
    {
176
        $this->getResponse()->headers->replace($headers);
177
178
        return $this;
179
    }
180
181 15
    /**
182
     * Sets the HTTP status code.
183 15
     *
184
     * @param int|null $code
185 15
     *
186
     * @return View
187
     */
188
    public function setStatusCode($code)
189
    {
190
        if (null !== $code) {
191
            $this->statusCode = $code;
192
        }
193
194
        return $this;
195
    }
196
197
    /**
198
     * Sets the serialization context.
199
     *
200
     * @param Context $context
201
     *
202
     * @return View
203
     */
204
    public function setContext(Context $context)
205
    {
206
        $this->context = $context;
207
208
        return $this;
209
    }
210 1
211
    /**
212 1
     * Sets the format.
213
     *
214 1
     * @param string $format
215
     *
216
     * @return View
217
     */
218
    public function setFormat($format)
219
    {
220
        $this->format = $format;
221
222
        return $this;
223
    }
224 87
225
    /**
226 87
     * Sets the location (implicitly removes the route).
227 22
     *
228 22
     * @param string $location
229
     *
230 87
     * @return View
231
     */
232
    public function setLocation($location)
233
    {
234
        $this->location = $location;
235
        $this->route = null;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Sets the route (implicitly removes the location).
242
     *
243
     * @param string $route
244
     *
245
     * @return View
246
     */
247
    public function setRoute($route)
248
    {
249
        $this->route = $route;
250
        $this->location = null;
251
252
        return $this;
253
    }
254
255
    /**
256 11
     * Sets route data.
257
     *
258 11
     * @param array $parameters
259 1
     *
260
     * @return View
261 11
     */
262
    public function setRouteParameters($parameters)
263 11
    {
264
        $this->routeParameters = $parameters;
265
266
        return $this;
267
    }
268
269
    /**
270
     * Sets the response.
271
     *
272
     * @param Response $response
273 87
     *
274
     * @return View
275 87
     */
276
    public function setResponse(Response $response)
277 87
    {
278
        $this->response = $response;
279
280
        return $this;
281
    }
282
283
    /**
284
     * Gets the data.
285
     *
286
     * @return mixed|null
287 1
     */
288
    public function getData()
289 1
    {
290
        return $this->data;
291 1
    }
292
293
    /**
294
     * Gets the HTTP status code.
295
     *
296
     * @return int|null
297
     */
298
    public function getStatusCode()
299
    {
300
        return $this->statusCode;
301 33
    }
302
303 33
    /**
304
     * Gets the headers.
305 33
     *
306
     * @return array
307
     */
308
    public function getHeaders()
309
    {
310
        return $this->getResponse()->headers->all();
311
    }
312
313
    /**
314
     * Gets the format.
315 8
     *
316
     * @return string|null
317 8
     */
318 8
    public function getFormat()
319
    {
320 8
        return $this->format;
321
    }
322
323
    /**
324
     * Gets the location.
325
     *
326
     * @return string|null
327
     */
328
    public function getLocation()
329
    {
330 3
        return $this->location;
331
    }
332 3
333 3
    /**
334
     * Gets the route.
335 3
     *
336
     * @return string|null
337
     */
338
    public function getRoute()
339
    {
340
        return $this->route;
341
    }
342
343
    /**
344
     * Gets route parameters.
345 3
     *
346
     * @return array|null
347 3
     */
348
    public function getRouteParameters()
349 3
    {
350
        return $this->routeParameters;
351
    }
352
353
    /**
354
     * Gets the response.
355
     *
356
     * @return Response
357
     */
358
    public function getResponse()
359
    {
360
        if (null === $this->response) {
361
            $this->response = new Response();
362
363
            if (null !== ($code = $this->getStatusCode())) {
364
                $this->response->setStatusCode($code);
365
            }
366
        }
367
368
        return $this->response;
369
    }
370
371 65
    /**
372
     * Gets the serialization context.
373 65
     *
374
     * @return Context
375
     */
376
    public function getContext()
377
    {
378
        if (null === $this->context) {
379
            $this->context = new Context();
380
        }
381 53
382
        return $this->context;
383 53
    }
384
}
385