Completed
Push — master ( 21573f...0b6989 )
by Lukas Kahwe
06:29
created

View::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 FOS\RestBundle\Context\ContextInterface;
16
use Symfony\Component\Templating\TemplateReferenceInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * Default View implementation.
21
 *
22
 * @author Johannes M. Schmitt <[email protected]>
23
 * @author Lukas K. Smith <[email protected]>
24
 */
25
class View
26
{
27
    /**
28
     * @var mixed|null
29
     */
30
    private $data;
31
32
    /**
33
     * @var int|null
34
     */
35
    private $statusCode;
36
37
    /**
38
     * @var mixed|null
39
     */
40
    private $templateData = [];
41
42
    /**
43
     * @var TemplateReference|string|null
44
     */
45
    private $template;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $templateVar;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $engine;
56
57
    /**
58
     * @var string|null
59
     */
60
    private $format;
61
62
    /**
63
     * @var string|null
64
     */
65
    private $location;
66
67
    /**
68
     * @var string|null
69
     */
70
    private $route;
71
72
    /**
73
     * @var array|null
74
     */
75
    private $routeParameters;
76
77
    /**
78
     * @var ContextInterface
79
     */
80
    private $serializationContext;
81
82
    /**
83
     * @var Response
84
     */
85
    private $response;
86
87
    /**
88
     * Convenience method to allow for a fluent interface.
89
     *
90
     * @param mixed $data
91
     * @param int   $statusCode
92
     * @param array $headers
93
     *
94
     * @return static
95
     */
96 9
    public static function create($data = null, $statusCode = null, array $headers = [])
97
    {
98 9
        return new static($data, $statusCode, $headers);
99
    }
100
101
    /**
102
     * Convenience method to allow for a fluent interface while creating a redirect to a
103
     * given url.
104
     *
105
     * @param string $url
106
     * @param int    $statusCode
107
     * @param array  $headers
108
     *
109
     * @return static
110
     */
111 1
    public static function createRedirect($url, $statusCode = Response::HTTP_FOUND, array $headers = [])
112
    {
113 1
        $view = static::create(null, $statusCode, $headers);
114 1
        $view->setLocation($url);
115
116 1
        return $view;
117
    }
118
119
    /**
120
     * Convenience method to allow for a fluent interface while creating a redirect to a
121
     * given route.
122
     *
123
     * @param string $route
124
     * @param array  $parameters
125
     * @param int    $statusCode
126
     * @param array  $headers
127
     *
128
     * @return static
129
     */
130 1
    public static function createRouteRedirect(
131
        $route,
132
        array $parameters = [],
133
        $statusCode = Response::HTTP_FOUND,
134
        array $headers = []
135
    ) {
136 1
        $view = static::create(null, $statusCode, $headers);
137 1
        $view->setRoute($route);
138 1
        $view->setRouteParameters($parameters);
139
140 1
        return $view;
141
    }
142
143
    /**
144
     * Constructor.
145
     *
146
     * @param mixed $data
147
     * @param int   $statusCode
148
     * @param array $headers
149
     */
150 79
    public function __construct($data = null, $statusCode = null, array $headers = [])
151
    {
152 79
        $this->setData($data);
153 79
        $this->setStatusCode($statusCode);
154 79
        $this->setTemplateVar('data');
155
156 79
        if (!empty($headers)) {
157 1
            $this->getResponse()->headers->replace($headers);
158 1
        }
159 79
    }
160
161
    /**
162
     * Sets the data.
163
     *
164
     * @param mixed $data
165
     *
166
     * @return View
167
     */
168 79
    public function setData($data)
169
    {
170 79
        $this->data = $data;
171
172 79
        return $this;
173
    }
174
175
    /**
176
     * Set template variable.
177
     *
178
     * @param array|callable $data
179
     *
180
     * @return View
181
     */
182 6
    public function setTemplateData($data = [])
183
    {
184 6
        $this->templateData = $data;
185
186 6
        return $this;
187
    }
188
189
    /**
190
     * Sets a header.
191
     *
192
     * @param string $name
193
     * @param string $value
194
     *
195
     * @return View
196
     */
197
    public function setHeader($name, $value)
198
    {
199
        $this->getResponse()->headers->set($name, $value);
200
201
        return $this;
202
    }
203
204
    /**
205
     * Sets the headers.
206
     *
207
     * @param array $headers
208
     *
209
     * @return View
210
     */
211 1
    public function setHeaders(array $headers)
212
    {
213 1
        $this->getResponse()->headers->replace($headers);
214
215 1
        return $this;
216
    }
217
218
    /**
219
     * Sets the HTTP status code.
220
     *
221
     * @param int|null $code
222
     *
223
     * @return View
224
     */
225 79
    public function setStatusCode($code)
226
    {
227 79
        if (null !== $code) {
228 16
            $this->statusCode = $code;
229 16
        }
230
231 79
        return $this;
232
    }
233
234
    /**
235
     * Sets the serialization context.
236
     *
237
     * @param ContextInterface $serializationContext
238
     *
239
     * @return View
240
     */
241
    public function setSerializationContext(ContextInterface $serializationContext)
242
    {
243
        $this->serializationContext = $serializationContext;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Sets template to use for the encoding.
250
     *
251
     * @param string|TemplateReferenceInterface $template
252
     *
253
     * @throws \InvalidArgumentException if the template is neither a string nor an instance of TemplateReferenceInterface
254
     *
255
     * @return View
256
     */
257 2
    public function setTemplate($template)
258
    {
259 2
        if (!(is_string($template) || $template instanceof TemplateReferenceInterface)) {
260 1
            throw new \InvalidArgumentException('The template should be a string or implement TemplateReferenceInterface');
261
        }
262 2
        $this->template = $template;
0 ignored issues
show
Documentation Bug introduced by
It seems like $template can also be of type object<Symfony\Component...lateReferenceInterface>. However, the property $template is declared as type object<FOS\RestBundle\Vi...eReference>|string|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
263
264 2
        return $this;
265
    }
266
267
    /**
268
     * Sets template variable name to be used in templating formats.
269
     *
270
     * @param string $templateVar
271
     *
272
     * @return View
273
     */
274 79
    public function setTemplateVar($templateVar)
275
    {
276 79
        $this->templateVar = $templateVar;
277
278 79
        return $this;
279
    }
280
281
    /**
282
     * Sets the engine.
283
     *
284
     * @param string $engine
285
     *
286
     * @return View
287
     */
288 1
    public function setEngine($engine)
289
    {
290 1
        $this->engine = $engine;
291
292 1
        return $this;
293
    }
294
295
    /**
296
     * Sets the format.
297
     *
298
     * @param string $format
299
     *
300
     * @return View
301
     */
302 34
    public function setFormat($format)
303
    {
304 34
        $this->format = $format;
305
306 34
        return $this;
307
    }
308
309
    /**
310
     * Sets the location (implicitly removes the route).
311
     *
312
     * @param string $location
313
     *
314
     * @return View
315
     */
316 8
    public function setLocation($location)
317
    {
318 8
        $this->location = $location;
319 8
        $this->route = null;
320
321 8
        return $this;
322
    }
323
324
    /**
325
     * Sets the route (implicitly removes the location).
326
     *
327
     * @param string $route
328
     *
329
     * @return View
330
     */
331 2
    public function setRoute($route)
332
    {
333 2
        $this->route = $route;
334 2
        $this->location = null;
335
336 2
        return $this;
337
    }
338
339
    /**
340
     * Sets route data.
341
     *
342
     * @param array $parameters
343
     *
344
     * @return View
345
     */
346 2
    public function setRouteParameters($parameters)
347
    {
348 2
        $this->routeParameters = $parameters;
349
350 2
        return $this;
351
    }
352
353
    /**
354
     * Sets the response.
355
     *
356
     * @param Response $response
357
     *
358
     * @return View
359
     */
360
    public function setResponse(Response $response)
361
    {
362
        $this->response = $response;
363
364
        return $this;
365
    }
366
367
    /**
368
     * Gets the data.
369
     *
370
     * @return mixed|null
371
     */
372 57
    public function getData()
373
    {
374 57
        return $this->data;
375
    }
376
377
    /**
378
     * Gets the template data.
379
     *
380
     * @return mixed|null
381
     */
382 22
    public function getTemplateData()
383
    {
384 22
        return $this->templateData;
385
    }
386
387
    /**
388
     * Gets the HTTP status code.
389
     *
390
     * @return int|null
391
     */
392 58
    public function getStatusCode()
393
    {
394 58
        return $this->statusCode;
395
    }
396
397
    /**
398
     * Gets the headers.
399
     *
400
     * @return array
401
     */
402 2
    public function getHeaders()
403
    {
404 2
        return $this->getResponse()->headers->all();
405
    }
406
407
    /**
408
     * Gets the template.
409
     *
410
     * @return TemplateReferenceInterface|string|null
411
     */
412 13
    public function getTemplate()
413
    {
414 13
        return $this->template;
415
    }
416
417
    /**
418
     * Gets the template variable name.
419
     *
420
     * @return string|null
421
     */
422 12
    public function getTemplateVar()
423
    {
424 12
        return $this->templateVar;
425
    }
426
427
    /**
428
     * Gets the engine.
429
     *
430
     * @return string|null
431
     */
432 1
    public function getEngine()
433
    {
434 1
        return $this->engine;
435
    }
436
437
    /**
438
     * Gets the format.
439
     *
440
     * @return string|null
441
     */
442 34
    public function getFormat()
443
    {
444 34
        return $this->format;
445
    }
446
447
    /**
448
     * Gets the location.
449
     *
450
     * @return string|null
451
     */
452 43
    public function getLocation()
453
    {
454 43
        return $this->location;
455
    }
456
457
    /**
458
     * Gets the route.
459
     *
460
     * @return string|null
461
     */
462 44
    public function getRoute()
463
    {
464 44
        return $this->route;
465
    }
466
467
    /**
468
     * Gets route parameters.
469
     *
470
     * @return array|null
471
     */
472 1
    public function getRouteParameters()
473
    {
474 1
        return $this->routeParameters;
475
    }
476
477
    /**
478
     * Gets the response.
479
     *
480
     * @return Response
481
     */
482 49
    public function getResponse()
483
    {
484 49
        if (null === $this->response) {
485 49
            $this->response = new Response();
486
487 49
            if (null !== ($code = $this->getStatusCode())) {
488 12
                $this->response->setStatusCode($code);
489 12
            }
490 49
        }
491
492 49
        return $this->response;
493
    }
494
495
    /**
496
     * Gets the serialization context.
497
     *
498
     * @return ContextInterface
499
     */
500 35
    public function getSerializationContext()
501
    {
502 35
        if (null === $this->serializationContext) {
503 35
            $this->serializationContext = new Context();
504 35
        }
505
506 35
        return $this->serializationContext;
507
    }
508
}
509