Completed
Push — master ( bbb486...6c9a0a )
by Restu
14:10
created

Response::setNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 9.4285
1
<?php
2
namespace JayaCode\Framework\Core\Http;
3
4
use Symfony\Component\HttpFoundation\Response as BaseResponse;
5
6
class Response extends BaseResponse
7
{
8
    /**
9
     * if content is an array, then convert to json
10
     * @param mixed $content
11
     * @return BaseResponse
12
     */
13
    public function setContent($content)
14
    {
15
        if (is_array($content)) {
16
            $this->headers->set('Content-Type', 'application/json');
17
            $content = json_encode($content);
18
        }
19
20
        return parent::setContent($content);
0 ignored issues
show
Security Cross-Site Scripting introduced by
$content can contain request data and is used in output context(s) leading to a potential security vulnerability.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
21
    }
22
23
    /**
24
     * @param mixed $content The response content, see setContent()
25
     * @param int   $status  The response status code
26
     * @param array $headers An array of response headers
27
     *
28
     * @return Response
29
     */
30
    public static function create($content = '', $status = 200, $headers = array())
31
    {
32
        return new static($content, $status, $headers);
33
    }
34
35
    /**
36
     * Setup response 404
37
     * @param string $path
38
     */
39
    public function setNotFound($path)
40
    {
41
        $this->setStatusCode(404);
42
        $this->setContent("not found : " . $path);
43
    }
44
}
45