Completed
Push — master ( 1012d8...9f6a15 )
by Restu
13:22
created

Response   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 38
rs 10
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDataContent() 0 9 2
A create() 0 4 1
A setNotFound() 0 5 1
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
     */
12
    public function setDataContent($content)
13
    {
14
        if (is_array($content)) {
15
            $this->headers->set('Content-Type', 'application/json');
16
            $content = json_encode($content);
17
        }
18
19
        $this->setContent($content);
20
    }
21
22
    /**
23
     * @param mixed $content The response content, see setContent()
24
     * @param int   $status  The response status code
25
     * @param array $headers An array of response headers
26
     *
27
     * @return Response
28
     */
29
    public static function create($content = '', $status = 200, $headers = array())
30
    {
31
        return new static($content, $status, $headers);
32
    }
33
34
    /**
35
     * Setup response 404
36
     * @param string $path
37
     */
38
    public function setNotFound($path)
39
    {
40
        $this->setStatusCode(404);
41
        $this->setContent("not found : " . $path);
0 ignored issues
show
Security Cross-Site Scripting introduced by
'not found : ' . $path 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...
42
    }
43
}
44