Completed
Branch master (51d2f6)
by John
02:24
created

Request::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 6
Metric Value
c 9
b 0
f 6
dl 0
loc 11
rs 9.4286
cc 1
eloc 10
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace LunixREST\Request;
3
use LunixREST\Exceptions\InvalidRequestFormatException;
4
5
/**
6
 * Class Request
7
 * @package LunixREST\Request
8
 */
9
class Request {
10
    /**
11
     * @var string
12
     */
13
    protected $method;
14
    /**
15
     * @var array
16
     */
17
    protected $headers;
18
    /**
19
     * @var string
20
     */
21
    protected $version;
22
    /**
23
     * @var string
24
     */
25
    protected $apiKey;
26
    /**
27
     * @var string
28
     */
29
    protected $endpoint;
30
    /**
31
     * @var string
32
     */
33
    protected $instance;
34
    /**
35
     * @var string
36
     */
37
    protected $extension;
38
    /**
39
     * @var array
40
     */
41
    protected $data;
42
    /**
43
     * @var string
44
     */
45
    protected $ip;
46
47
    /**
48
     * Create a request. Pass Either a URL to parse or the parsed parts.
49
     * If both are passed the explicitly stated parts will be used.
50
     * @param $method
51
     * @param array $headers
52
     * @param array $data
53
     * @param $ip
54
     * @param string $version
55
     * @param string $apiKey
56
     * @param string $endpoint
57
     * @param string $extension
58
     * @param string $instance
59
     */
60
    public function __construct($method, array $headers, array $data, $ip, $version, $apiKey, $endpoint, $extension, $instance = null){
61
        $this->method = strtolower($method);
62
        $this->headers = $headers;
63
        $this->data = $data;
64
        $this->ip = $ip;
65
        $this->version = $version;
66
        $this->apiKey = $apiKey;
67
        $this->endpoint = $endpoint;
68
        $this->extension = $extension;
69
        $this->instance = $instance;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getIp()
76
    {
77
        return $this->ip;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getMethod()
84
    {
85
        return $this->method . ($this->instance ? '' : 'All');
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getHeaders()
92
    {
93
        return $this->headers;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getVersion()
100
    {
101
        return $this->version;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getApiKey()
108
    {
109
        return $this->apiKey;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getEndpoint()
116
    {
117
        return $this->endpoint;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getInstance()
124
    {
125
        return $this->instance;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getExtension()
132
    {
133
        return $this->extension;
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function getData()
140
    {
141
        return $this->data;
142
    }
143
144
    /**
145
     * @param $method
146
     * @param array $headers
147
     * @param array $data
148
     * @param $ip
149
     * @param string $url
150
     * @return Request
151
     * @throws InvalidRequestFormatException
152
     */
153
    public static function createFromURL($method, array $headers, array $data, $ip, $url){
154
        $splitURL = explode('/', trim($url, '/'));
155
        if(count($splitURL) < 3){
156
            throw new InvalidRequestFormatException();
157
        }
158
        //Find endpoint
159
        $version = $splitURL[0];
160
        $apiKey = $splitURL[1];
161
        $endpoint = $splitURL[2];
162
163
        $splitExtension = explode('.', $splitURL[count($splitURL) - 1]);
164
        $extension = array_pop($splitExtension);
165
166
        $instance = null;
167
168
        if(count($splitURL) == 4){
169
            $instance = implode('.', $splitExtension);
170
        } else {
171
            $endpoint = implode('.', $splitExtension);
172
        }
173
174
175
        return new Request($method, $headers, $data, $ip, $version, $apiKey, $endpoint, $extension, $instance);
176
    }
177
}
178