Request   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 4
cbo 0
dl 0
loc 111
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A get() 0 10 2
A path() 0 4 1
A getVerb() 0 4 1
A post() 0 4 1
A getIp() 0 4 1
A getUrl() 0 10 3
1
<?php
2
/**
3
 * Simple request class
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category   OpCacheGUI
8
 * @package    Network
9
 * @author     Pieter Hordijk <[email protected]>
10
 * @copyright  Copyright (c) 2014 Pieter Hordijk <https://github.com/PeeHaa>
11
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
12
 * @version    1.0.0
13
 */
14
namespace OpCacheGUI\Network;
15
16
/**
17
 * Simple request class
18
 *
19
 * @category   OpCacheGUI
20
 * @package    Network
21
 * @author     Pieter Hordijk <[email protected]>
22
 */
23
class Request implements RequestData
24
{
25
    /**
26
     * @var array The get variables
27
     */
28
    private $getVariables = [];
29
30
    /**
31
     * @var array The post variables
32
     */
33
    private $postVariables = [];
34
35
    /**
36
     * @var array The server variables
37
     */
38
    private $serverVariables = [];
39
40
    /**
41
     * @var array The path variables
42
     */
43
    private $pathVariables = [];
44
45
    /**
46
     * Creates instance
47
     *
48
     * @param array $get    The get variables
49
     * @param array $post   The post variables
50
     * @param array $server The server variables
51
     */
52 14
    public function __construct(array $get, array $post, array $server)
53
    {
54 14
        $this->getVariables    = $get;
55 14
        $this->postVariables   = $post;
56 14
        $this->serverVariables = $server;
57 14
        $this->pathVariables   = explode('/', trim($server['REQUEST_URI'], '/'));
58 14
    }
59
60
    /**
61
     * Gets the variable or returns false when it does not exists
62
     *
63
     * @return string The value of the get param or false when it does not exist
64
     */
65 2
    public function get()
66
    {
67 2
        if (!$this->getVariables) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getVariables 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...
68 1
            return '';
69
        }
70
71 1
        $queryStringParams = array_keys($this->getVariables);
72
73 1
        return reset($queryStringParams);
74
    }
75
76
    /**
77
     * Gets the variable
78
     *
79
     * @return string The value of the path param
80
     */
81 5
    public function path()
82
    {
83 5
        return end($this->pathVariables);
84
    }
85
86
    /**
87
     * Gets the verb of the request
88
     *
89
     * @return string The verb used by the request
90
     */
91 1
    public function getVerb()
92
    {
93 1
        return $this->serverVariables['REQUEST_METHOD'];
94
    }
95
96
    /**
97
     * Gets a post variable
98
     *
99
     * @param string $name The name of the post variable to get
100
     *
101
     * @return mixed The value
102
     */
103 1
    public function post($name)
104
    {
105 1
        return $this->postVariables[$name];
106
    }
107
108
    /**
109
     * Gets the current URL
110
     *
111
     * @return string The current URL
112
     */
113 3
    public function getUrl()
114
    {
115 3
        $scheme = 'http';
116
117 3
        if (isset($this->serverVariables['HTTPS']) && $this->serverVariables['HTTPS'] === 'on') {
118 1
            $scheme .= 's';
119
        }
120
121 3
        return $scheme . '://' . $this->serverVariables['HTTP_HOST'] . $this->serverVariables['REQUEST_URI'];
122
    }
123
124
    /**
125
     * Gets the user's IP address
126
     *
127
     * @return string The IP of the user
128
     */
129 1
    public function getIp()
130
    {
131 1
        return $this->serverVariables['REMOTE_ADDR'];
132
    }
133
}
134