Completed
Push — master ( a8cb62...764ba2 )
by François
03:52
created

Request::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 12
nc 2
nop 0
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 $connection;
24
25
    protected $trustProtocol = false;
26
27
    protected $trustConnection = false;
28
29
    public function getUserAgent()
30
    {
31
        $userAgent = $this->getHeader('User-Agent');
32
        return $userAgent ? $userAgent : '';
33
    }
34
35
    public function getHeader($name)
36
    {
37
        return $this->headers->get($name);
38
    }
39
40
    public function getAllHeaders($ignore = array())
41
    {
42
        $headers = array();
43
        foreach ($this->headers->all() as $name => $value) {
44
            if (!$ignore || !in_array($name, $ignore)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ignore of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
45
                $headers[$name] = $this->headers->get($name);
46
            }
47
        }
48
        return $headers;
49
    }
50
51
    public function getHeaders()
52
    {
53
        $ignore = array('host', 'cookie');
54
55
        $headers = $this->getAllHeaders($ignore);
56
57
        $connection = $this->getConnection();
58
        if ($connection) {
59
            $headers['connection'] = $connection;
60
        } else {
61
            unset($headers['connection']);
62
        }
63
64
        return $headers;
65
    }
66
67
    public function getCookie($name)
68
    {
69
        return $this->cookies->get($name);
70
    }
71
72
    public function getCookies($names = array())
73
    {
74
        $cookies = array();
75
        foreach ($this->cookies->all() as $name => $value) {
76
            if (in_array($name, $names)) {
77
                $cookies[$name] = $this->cookies->get($name);
78
            }
79
        }
80
        return $cookies;
81
    }
82
83
    public function getAddr()
84
    {
85
        if ($this->addr) {
86
            return $this->addr;
87
        }
88
89
        return $this->getClientIp();
90
    }
91
92
    public function setAddr($addr)
93
    {
94
        $this->addr = $addr;
95
96
        return $this;
97
    }
98
99
    public function getProtocol()
100
    {
101
        if ($this->protocol) {
102
            return $this->protocol;
103
        }
104
105
        if ($this->trustProtocol) {
106
            return $this->server->get('SERVER_PROTOCOL');
107
        }
108
    }
109
110
    public function setProtocol($protocol)
111
    {
112
        $this->protocol = $protocol;
113
114
        return $this;
115
    }
116
117
    public function getConnection()
118
    {
119
        if ($this->connection) {
120
            return $this->connection;
121
        }
122
123
        if ($this->trustConnection) {
124
            return $this->headers->get('connection');
125
        }
126
    }
127
128
    public function setConnection($connection)
129
    {
130
        $this->connection = $connection;
131
132
        return $this;
133
    }
134
135
    public function __toString()
136
    {
137
        return $this->getMethod() . ' ' . $this->getHost() . ' ' . $this->getPort() . ' ' . $this->getRequestUri();
138
    }
139
140
    public function toArray()
141
    {
142
        $request = array();
143
144
        $request['scheme']     = $this->getScheme();
145
        $request['method']     = $this->getMethod();
146
        $request['host']       = $this->getHost();
147
        $request['port']       = $this->getPort();
148
        $request['url']        = $this->getRequestUri();
149
        $request['headers']    = $this->getHeaders();
150
151
        $protocol = $this->getProtocol();
152
        if ($protocol) {
153
            $request['protocol'] = $protocol;
154
        }
155
156
        return $request;
157
    }
158
159
}
160