Completed
Push — master ( 09b755...fee360 )
by François
07:31 queued 04:01
created

Request::setBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
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
    protected $trustProtocol = false;
28
29
    protected $trustConnection = false;
30
31
    public function getUserAgent()
32
    {
33
        $userAgent = $this->getHeader('User-Agent');
34
        return $userAgent ? $userAgent : '';
35
    }
36
37
    public function getHeader($name)
38
    {
39
        return $this->headers->get($name);
40
    }
41
42
    public function getAllHeaders($ignore = array())
43
    {
44
        $headers = array();
45
        foreach ($this->headers->all() as $name => $value) {
46
            if (empty($ignore) || !in_array($name, $ignore)) {
47
                $headers[$name] = $this->headers->get($name);
48
            }
49
        }
50
        return $headers;
51
    }
52
53
    public function getHeaders()
54
    {
55
        $ignore = array('host', 'cookie');
56
57
        $headers = $this->getAllHeaders($ignore);
58
59
        $connection = $this->getConnection();
60
        if ($connection) {
61
            $headers['connection'] = $connection;
62
        } else {
63
            unset($headers['connection']);
64
        }
65
66
        return $headers;
67
    }
68
69
    public function getCookie($name)
70
    {
71
        return $this->cookies->get($name);
72
    }
73
74
    public function getCookies($names = array())
75
    {
76
        $cookies = array();
77
        foreach ($this->cookies->all() as $name => $value) {
78
            if (in_array($name, $names)) {
79
                $cookies[$name] = $this->cookies->get($name);
80
            }
81
        }
82
        return $cookies;
83
    }
84
85
    public function getAddr()
86
    {
87
        if ($this->addr) {
88
            return $this->addr;
89
        }
90
91
        return $this->getClientIp();
92
    }
93
94
    public function setAddr($addr)
95
    {
96
        $this->addr = $addr;
97
98
        return $this;
99
    }
100
101
    public function getProtocol()
102
    {
103
        if ($this->protocol) {
104
            return $this->protocol;
105
        }
106
107
        if ($this->trustProtocol) {
108
            return $this->server->get('SERVER_PROTOCOL');
109
        }
110
    }
111
112
    public function setProtocol($protocol)
113
    {
114
        $this->protocol = $protocol;
115
116
        return $this;
117
    }
118
119
    public function getConnection()
120
    {
121
        if ($this->connection) {
122
            return $this->connection;
123
        }
124
125
        if ($this->trustConnection) {
126
            return $this->headers->get('connection');
127
        }
128
    }
129
130
    public function setConnection($connection)
131
    {
132
        $this->connection = $connection;
133
134
        return $this;
135
    }
136
137
    public function getBody()
138
    {
139
        if ($this->body) {
140
            return $this->body;
141
        }
142
    }
143
144
    public function setBody($body)
145
    {
146
        $this->body = $body;
147
148
        return $this;
149
    }
150
151
    public function __toString()
152
    {
153
        return $this->getMethod() . ' ' . $this->getHost() . ' ' . $this->getPort() . ' ' . $this->getRequestUri();
154
    }
155
156
    public function toArray()
157
    {
158
        $request = array();
159
160
        $request['scheme']  = $this->getScheme();
161
        $request['method']  = $this->getMethod();
162
        $request['host']    = $this->getHost();
163
        $request['port']    = $this->getPort();
164
        $request['url']     = $this->getRequestUri();
165
        $request['headers'] = $this->getHeaders();
166
167
        $protocol = $this->getProtocol();
168
        if ($protocol) {
169
            $request['protocol'] = $protocol;
170
        }
171
172
        $body = $this->getBody();
173
        if ($body) {
174
            $request['body'] = $body;
175
        }
176
177
        return $request;
178
    }
179
180
}
181