Completed
Push — master ( 093ab2...84b6a3 )
by John
01:55
created

Request::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 11

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\Request\RequestData\RequestData;
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 string
40
     */
41
    protected $ip;
42
    /**
43
     * @var RequestData
44
     */
45
    private $body;
46
    /**
47
     * @var RequestData
48
     */
49
    private $urlData;
50
    /**
51
     * @var MIMEProvider
52
     */
53
    private $MIMEProvider;
54
55
    /**
56
     * Create a request. Pass Either a URL to parse or the parsed parts.
57
     * If both are passed the explicitly stated parts will be used.
58
     * @param MIMEProvider $MIMEProvider
59
     * @param $method
60
     * @param array $headers
61
     * @param RequestData $body
62
     * @param RequestData $urlData
63
     * @param $ip
64
     * @param string $version
65
     * @param string $apiKey
66
     * @param string $endpoint
67
     * @param string $extension
68
     * @param string $instance
69
     */
70
71
    public function __construct(MIMEProvider $MIMEProvider, $method, array $headers, RequestData $body, RequestData $urlData, $ip, $version, $apiKey, $endpoint, $extension, $instance = null){
72
        $this->MIMEProvider = $MIMEProvider;
73
        $this->method = strtolower($method);
74
        $this->headers = $headers;
75
        $this->body = $body;
76
        $this->urlData = $urlData;
77
        $this->ip = $ip;
78
        $this->version = $version;
79
        $this->apiKey = $apiKey;
80
        $this->endpoint = $endpoint;
81
        $this->extension = $extension;
82
        $this->instance = $instance;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getIp() {
89
        return $this->ip;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getMethod() {
96
        return $this->method . ($this->instance ? '' : 'All');
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getHeaders() {
103
        return $this->headers;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getVersion() {
110
        return $this->version;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getApiKey() {
117
        return $this->apiKey;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getEndpoint() {
124
        return $this->endpoint;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getInstance() {
131
        return $this->instance;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getExtension() {
138
        return $this->extension;
139
    }
140
141
    /**
142
     * @return RequestData
143
     */
144
    public function getBody(): RequestData {
145
        return $this->body;
146
    }
147
148
    /**
149
     * @return RequestData
150
     */
151
    public function getUrlData(): RequestData {
152
        return $this->urlData;
153
    }
154
155
    //TODO: Unit test
156
    public function getAcceptableMIMETypes(): array {
157
        $acceptedMIMETypes = [];
158
        if($this->extension) {
159
            //extension to mime type conversion
160
            $acceptedMIMETypes[] = $this->MIMEProvider->getByFileExtension($this->extension);
161
        } else {
162
            //TODO: follow RFC2616 order
163
            $headerAccepts = [];
164
            foreach($this->headers as $key => $value) {
165
                if(strtolower($key) == 'http_accept'){
166
                    $values = explode(',', $value);
167
                    foreach($values as $acceptedType) {
168
                        $typeParts = explode(';', $acceptedType);
169
                        if(count($typeParts) > 0 ){
170
                            $headerAccepts[] = trim($typeParts);
171
                        }
172
                    }
173
                    break;
174
                }
175
            }
176
            $acceptedMIMETypes = array_merge($acceptedMIMETypes, $headerAccepts);
177
        }
178
179
        return $acceptedMIMETypes;
180
    }
181
182
}
183