Request::setConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 1
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
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 Bouncer;
13
14
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest;
15
16
class Request extends HttpFoundationRequest
17
{
18
19
    protected $addr;
20
21
    protected $protocol;
22
23
    protected $body;
24
25
    protected $connection;
26
27
    public function getUserAgent()
28
    {
29
        $userAgent = $this->getHeader('User-Agent');
30
        return $userAgent ? $userAgent : '';
31
    }
32
33
    public function getHeader($name)
34
    {
35
        return $this->headers->get($name);
36
    }
37
38
    public function getAllHeaders($ignore = array())
39
    {
40
        $headers = array();
41
        foreach ($this->headers->all() as $name => $value) {
42
            if (empty($ignore) || !in_array($name, $ignore)) {
43
                $headers[$name] = $this->headers->get($name);
44
            }
45
        }
46
        return $headers;
47
    }
48
49
    public function getHeaders()
50
    {
51
        $ignore = array('host', 'cookie', 'connection');
52
53
        $headers = $this->getAllHeaders($ignore);
54
55
        $connection = $this->getConnection();
56
        if (isset($connection)) {
57
            $headers['connection'] = $connection;
58
        }
59
60
        return $headers;
61
    }
62
63
    public function getCookie($name)
64
    {
65
        return $this->cookies->get($name);
66
    }
67
68
    public function getCookies($names = array())
69
    {
70
        $cookies = array();
71
        foreach ($this->cookies->all() as $name => $value) {
72
            if (in_array($name, $names)) {
73
                $cookies[$name] = $this->cookies->get($name);
74
            }
75
        }
76
        return $cookies;
77
    }
78
79
    public function getAddr()
80
    {
81
        if ($this->addr) {
82
            return $this->addr;
83
        }
84
85
        return $this->getClientIp();
86
    }
87
88
    public function setAddr($addr)
89
    {
90
        $this->addr = $addr;
91
92
        return $this;
93
    }
94
95
    /*
96
     * @return string|null
97
     */
98
    public function getProtocol()
99
    {
100
        if ($this->protocol) {
101
            return $this->protocol;
102
        }
103
104
        return $this->server->get('SERVER_PROTOCOL');
105
    }
106
107
    /*
108
     * @param string
109
     */
110
    public function setProtocol($protocol)
111
    {
112
        $this->protocol = $protocol;
113
114
        return $this;
115
    }
116
117
    /*
118
     * @return string|null
119
     */
120
    public function getConnection()
121
    {
122
        if ($this->connection) {
123
            return $this->connection;
124
        }
125
126
        return $this->headers->get('connection');
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->headers->get('connection'); of type string|array adds the type array to the return on line 126 which is incompatible with the return type documented by Bouncer\Request::getConnection of type string|null.
Loading history...
127
    }
128
129
    /*
130
     * @param string
131
     */
132
    public function setConnection($connection)
133
    {
134
        $this->connection = $connection;
135
136
        return $this;
137
    }
138
139
    /*
140
     * @return string|null
141
     */
142
    public function getBody()
143
    {
144
        if ($this->body) {
145
            return $this->body;
146
        }
147
    }
148
149
    /*
150
     * @param string
151
     */
152
    public function setBody($body)
153
    {
154
        $this->body = $body;
155
156
        return $this;
157
    }
158
159
    public function __toString()
160
    {
161
        return $this->getMethod() . ' ' . $this->getHost() . ' ' . $this->getPort() . ' ' . $this->getRequestUri();
162
    }
163
164
    public function toArray()
165
    {
166
        $request = array();
167
168
        $request['scheme']  = $this->getScheme();
169
        $request['method']  = $this->getMethod();
170
        $request['host']    = $this->getHost();
171
        $request['port']    = $this->getPort();
172
        $request['url']     = $this->getRequestUri();
173
        $request['headers'] = $this->getHeaders();
174
175
        $protocol = $this->getProtocol();
176
        if (isset($protocol)) {
177
            $request['protocol'] = $protocol;
178
        }
179
180
        $body = $this->getBody();
181
        if (isset($body)) {
182
            $request['body'] = $body;
183
        }
184
185
        return $request;
186
    }
187
188
}
189