Completed
Pull Request — master (#166)
by
unknown
02:18
created

Request::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TusPhp;
4
5
use TusPhp\Tus\Server;
6
use Symfony\Component\HttpFoundation\Request as HttpRequest;
7
8
class Request
9
{
10
    /** @var HttpRequest */
11
    protected $request;
12
13
    /**
14
     * Request constructor.
15
     */
16 1
    public function __construct()
17
    {
18 1
        if (null === $this->request) {
19 1
            $this->request = HttpRequest::createFromGlobals();
20
        }
21 1
    }
22
23
    /**
24
     * Get http method from current request.
25
     *
26
     * @return string
27
     */
28 1
    public function method() : string
29
    {
30 1
        return $this->request->getMethod();
31
    }
32
33
    /**
34
     * Get the current path info for the request.
35
     *
36
     * @return string
37
     */
38 1
    public function path() : string
39
    {
40 1
        return $this->request->getPathInfo();
41
    }
42
43
    /**
44
     * Get upload key from url.
45
     *
46
     * @return string
47
     */
48 1
    public function key() : string
49
    {
50 1
        return basename($this->path());
51
    }
52
53
    /**
54
     * Supported http requests.
55
     *
56
     * @return array
57
     */
58 1
    public function allowedHttpVerbs() : array
59
    {
60
        return [
61 1
            HttpRequest::METHOD_GET,
62 1
            HttpRequest::METHOD_POST,
63 1
            HttpRequest::METHOD_PATCH,
64 1
            HttpRequest::METHOD_DELETE,
65 1
            HttpRequest::METHOD_HEAD,
66 1
            HttpRequest::METHOD_OPTIONS,
67
        ];
68
    }
69
70
    /**
71
     * Retrieve a header from the request.
72
     *
73
     * @param  string               $key
74
     * @param  string|string[]|null $default
75
     *
76
     * @return string|null
77
     */
78 1
    public function header(string $key, $default = null)
79
    {
80 1
        return $this->request->headers->get($key, $default);
0 ignored issues
show
Bug introduced by
It seems like $default can also be of type string[]; however, parameter $default of Symfony\Component\HttpFoundation\HeaderBag::get() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        return $this->request->headers->get($key, /** @scrutinizer ignore-type */ $default);
Loading history...
81
    }
82
83
    /**
84
     * Get the root URL for the request.
85
     *
86
     * @return string
87
     */
88 1
    public function url() : string
89
    {
90 1
        return rtrim($this->request->getUriForPath('/'), '/');
91
    }
92
93
    /**
94
     * Extract metadata from header.
95
     *
96
     * @param string $key
97
     * @param string $value
98
     *
99
     * @return array
100
     */
101 2
    public function extractFromHeader(string $key, string $value) : array
102
    {
103 2
        $meta = $this->header($key);
104
105 2
        if (false !== strpos($meta, $value)) {
106 2
            $meta = trim(str_replace($value, '', $meta));
107
108 2
            return explode(' ', $meta) ?? [];
109
        }
110
111 1
        return [];
112
    }
113
114
    /**
115
     * Extract base64 encoded filename from header.
116
     *
117
     * @return string
118
     */
119 2
    public function extractFileName() : string
120
    {
121 2
        return $this->extractMeta('name') ?: $this->extractMeta('filename');
122
    }
123
124
    /**
125
     * Extracts the meta data from the request header.
126
     *
127
     * @param string $requestedKey
128
     *
129
     * @return string
130
     */
131 3
    public function extractMeta(string $requestedKey) : string
132
    {
133 3
        $uploadMetaData = $this->request->headers->get('Upload-Metadata');
134
135 3
        if (empty($uploadMetaData)) {
136 1
            return '';
137
        }
138
139 2
        $uploadMetaDataChunks = explode(',', $uploadMetaData);
140
141 2
        foreach ($uploadMetaDataChunks as $chunk) {
142 2
            list($key, $value) = explode(' ', $chunk);
143
144 2
            if ($key === $requestedKey) {
145 2
                return base64_decode($value);
146
            }
147
        }
148
149 1
        return '';
150
    }
151
152
    /**
153
     * Extracts all meta data from the request header.
154
     *
155
     * @return string[]
156
     */
157
    public function extractAllMeta() : array
158
    {
159
        $uploadMetaData = $this->request->headers->get('Upload-Metadata');
160
161
        if (empty($uploadMetaData)) {
162
            return [];
163
        }
164
165
        $uploadMetaDataChunks = explode(',', $uploadMetaData);
166
167
        $result = [];
168
        foreach ($uploadMetaDataChunks as $chunk) {
169
            list($key, $value) = explode(' ', $chunk);
170
171
            $result[$key] = base64_decode($value);
172
        }
173
174
        return $result;
175
    }
176
177
    /**
178
     * Extract partials from header.
179
     *
180
     * @return array
181
     */
182 1
    public function extractPartials() : array
183
    {
184 1
        return $this->extractFromHeader('Upload-Concat', Server::UPLOAD_TYPE_FINAL . ';');
185
    }
186
187
    /**
188
     * Check if this is a partial upload request.
189
     *
190
     * @return bool
191
     */
192 1
    public function isPartial() : bool
193
    {
194 1
        return Server::UPLOAD_TYPE_PARTIAL === $this->header('Upload-Concat');
195
    }
196
197
    /**
198
     * Check if this is a final concatenation request.
199
     *
200
     * @return bool
201
     */
202 1
    public function isFinal() : bool
203
    {
204 1
        return false !== strpos($this->header('Upload-Concat'), Server::UPLOAD_TYPE_FINAL . ';');
205
    }
206
207
    /**
208
     * Get request.
209
     *
210
     * @return HttpRequest
211
     */
212 1
    public function getRequest() : HttpRequest
213
    {
214 1
        return $this->request;
215
    }
216
}
217