Passed
Push — master ( d094eb...62f043 )
by devosc
02:35
created

HttpResponse::header()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Response\Config;
7
8
use Mvc5\Arg;
9
use Mvc5\Cookie\Cookies;
10
use Mvc5\Cookie\HttpCookies;
11
use Mvc5\Http\Headers;
12
use Mvc5\Http\HttpHeaders;
13
use Mvc5\Response\Response;
14
15
trait HttpResponse
16
{
17
    /**
18
     *
19
     */
20
    use \Mvc5\Http\Config\Response;
21
22
    /**
23
     * @var array
24
     */
25
    protected $config = [];
26
27
    /**
28
     * @param mixed $body
29
     * @param int|null $status
30
     * @param array|Headers $headers
31
     * @param array $config
32
     */
33 25
    function __construct($body = null, int $status = null, $headers = [], array $config = [])
34
    {
35 25
        !isset($config[Arg::COOKIES]) &&
36 23
            $config[Arg::COOKIES] = new HttpCookies;
37
38 25
        !($config[Arg::COOKIES] instanceof Cookies) &&
39 2
            $config[Arg::COOKIES] = new HttpCookies($config[Arg::COOKIES]);
40
41 25
        $config[Arg::HEADERS] = $headers instanceof Headers ? $headers : new HttpHeaders($headers);
42 25
        $config[Arg::STATUS] = $status;
43 25
        $config[Arg::BODY] = $body;
44
45 25
        $this->config = $config;
46 25
    }
47
48
    /**
49
     * @return Cookies
50
     */
51 4
    function cookies() : Cookies
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
    {
53 4
        return $this[Arg::COOKIES];
54
    }
55
56
    /**
57
     * @param array|string $name
58
     * @return array|string
59
     */
60
    function header($name)
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
    {
62
        if (is_string($name)) {
63
            return implode(', ', (array) ($this->headers()[$name] ?? ''));
64
        }
65
66 1
        $matched = [];
67
68
        foreach($name as $key) {
69 1
            $matched[$key] = implode(', ', (array) ($this->headers()[$key] ?? ''));
70
        }
71
72
        return $matched;
73
    }
74
75
    /**
76 2
     * @param string $name
77
     * @param string|null $value
78 2
     * @param int|null $expire
79
     * @param string|null $path
80
     * @param string|null $domain
81
     * @param bool|null $secure
82
     * @param bool|null $httponly
83
     * @return self|mixed
84
     */
85
    function withCookie($name, $value = null, $expire = null,
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
86 1
                        string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : Response
87
    {
88 1
        return $this->withCookies($this->cookies()->with($name, $value, $expire, $path, $domain, $secure, $httponly));
89
    }
90
91
    /**
92
     * @param array|Cookies $cookies
93
     * @return self|mixed
94
     */
95 1
    function withCookies($cookies) : Response
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
96
    {
97 1
        return $this->with(Arg::COOKIES, $cookies instanceof Cookies ? $cookies : new HttpCookies($cookies));
0 ignored issues
show
Bug introduced by
It seems like with() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        return $this->/** @scrutinizer ignore-call */ with(Arg::COOKIES, $cookies instanceof Cookies ? $cookies : new HttpCookies($cookies));
Loading history...
98
    }
99
100
    /**
101
     * @param string $name
102
     * @param string $value
103
     * @return self|mixed
104
     */
105 1
    function withHeader($name, $value) : Response
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
    {
107 1
        return $this->with(Arg::HEADERS, $this->headers()->with((string) $name, $value));
108
    }
109
110
    /**
111
     * @param array|Headers $headers
112
     * @return self|mixed
113
     */
114 1
    function withHeaders($headers) : Response
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
115
    {
116 1
        return $this->with(Arg::HEADERS, $headers instanceof Headers ? $headers : new HttpHeaders($headers));
117
    }
118
119
    /**
120
     * @param int $status
121
     * @param string $reason
122
     * @return self|mixed
123
     */
124
    function withStatus($status, $reason = '') : Response
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
125
    {
126
        return $this->with([Arg::STATUS => (int) $status, Arg::REASON => (string) $reason]);
127
    }
128
129
    /**
130
     * @param string $version
131
     * @return self|mixed
132
     */
133
    function withVersion(string $version) : Response
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
    {
135
        return $this->with(Arg::VERSION, $version);
136
    }
137
}
138