Completed
Pull Request — master (#84)
by Samundra
02:27
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 Symfony\Component\HttpFoundation\Request as HttpRequest;
6
7
class Request
8
{
9
    /** @var HttpRequest */
10
    protected $request;
11
12
    /**
13
     * Request constructor.
14
     */
15 1
    public function __construct()
16
    {
17 1
        if (null === $this->request) {
18 1
            $this->request = HttpRequest::createFromGlobals();
19
        }
20 1
    }
21
22
    /**
23
     * Get http method from current request.
24
     *
25
     * @return string
26
     */
27 1
    public function method() : string
28
    {
29 1
        return $this->request->getMethod();
30
    }
31
32
    /**
33
     * Get the current path info for the request.
34
     *
35
     * @return string
36
     */
37 1
    public function path() : string
38
    {
39 1
        return $this->request->getPathInfo();
40
    }
41
42
    /**
43
     * Get upload key from url.
44
     *
45
     * @return string
46
     */
47 1
    public function key() : string
48
    {
49 1
        return basename($this->path());
50
    }
51
52
    /**
53
     * Supported http requests.
54
     *
55
     * @return array
56
     */
57 1
    public function allowedHttpVerbs() : array
58
    {
59
        return [
60 1
            HttpRequest::METHOD_GET,
61 1
            HttpRequest::METHOD_POST,
62 1
            HttpRequest::METHOD_PATCH,
63 1
            HttpRequest::METHOD_DELETE,
64 1
            HttpRequest::METHOD_HEAD,
65 1
            HttpRequest::METHOD_OPTIONS,
66
        ];
67
    }
68
69
    /**
70
     * Retrieve a header from the request.
71
     *
72
     * @param  string            $key
73
     * @param  string|array|null $default
74
     *
75
     * @return string|null
76
     */
77 1
    public function header(string $key, $default = null)
78
    {
79 1
        return $this->request->headers->get($key, $default);
80
    }
81
82
    /**
83
     * Get the root URL for the request.
84
     *
85
     * @return string
86
     */
87 1
    public function url() : string
88
    {
89 1
        return rtrim($this->request->getUriForPath('/'), '/');
90
    }
91
92
    /**
93
     * Extract metadata from header.
94
     *
95
     * @param string $key
96
     * @param string $value
97
     *
98
     * @return array
99
     */
100 2
    public function extractFromHeader(string $key, string $value) : array
101
    {
102 2
        $meta = $this->header($key);
103
104 2
        if (false !== strpos($meta, $value)) {
105 2
            $meta = trim(str_replace($value, '', $meta));
106
107 2
            return explode(' ', $meta) ?? [];
108
        }
109
110 1
        return [];
111
    }
112
113
    /**
114
     * Extract base64 encoded filename from header.
115
     *
116
     * @return string
117
     */
118 3
    public function extractFileName() : string
119
    {
120 3
        return $this->extractMeta('name') ?: $this->extractMeta('filename');
121
    }
122
123
    /**
124
     * Extracts the meta data from the request header.
125
     *
126
     * @param string $requestedKey
127
     *
128
     * @return string
129
     */
130 4
    public function extractMeta(string $requestedKey) : string
131
    {
132 4
        $uploadMetaData = $this->request->headers->get('Upload-Metadata');
133
134 4
        if (empty($uploadMetaData)) {
135 1
            return '';
136
        }
137
138 3
        $uploadMetaDataChunks = explode(',', $uploadMetaData);
139
140 3
        foreach ($uploadMetaDataChunks as $chunk) {
141 3
            list($key, $value) = explode(' ', $chunk);
142
143 3
            if ($key === $requestedKey) {
144 3
                return base64_decode($value);
145
            }
146
        }
147
148 1
        return '';
149
    }
150
151
    /**
152
     * Extract partials from header.
153
     *
154
     * @return array
155
     */
156 1
    public function extractPartials() : array
157
    {
158 1
        return $this->extractFromHeader('Upload-Concat', 'final;');
159
    }
160
161
    /**
162
     * Check if this is a partial upload request.
163
     *
164
     * @return bool
165
     */
166 1
    public function isPartial() : bool
167
    {
168 1
        return $this->header('Upload-Concat') === 'partial';
169
    }
170
171
    /**
172
     * Check if this is a final concatenation request.
173
     *
174
     * @return bool
175
     */
176 1
    public function isFinal() : bool
177
    {
178 1
        return false !== strpos($this->header('Upload-Concat'), 'final;');
179
    }
180
181
    /**
182
     * Get request.
183
     *
184
     * @return HttpRequest
185
     */
186 1
    public function getRequest() : HttpRequest
187
    {
188 1
        return $this->request;
189
    }
190
}
191