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

Request::extractFileName()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
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
    public function __construct()
16
    {
17
        if (null === $this->request) {
18
            $this->request = HttpRequest::createFromGlobals();
19
        }
20
    }
21
22
    /**
23
     * Get http method from current request.
24
     *
25
     * @return string
26
     */
27
    public function method() : string
28
    {
29
        return $this->request->getMethod();
30
    }
31
32
    /**
33
     * Get the current path info for the request.
34
     *
35
     * @return string
36
     */
37
    public function path() : string
38
    {
39
        return $this->request->getPathInfo();
40
    }
41
42
    /**
43
     * Get upload key from url.
44
     *
45
     * @return string
46
     */
47
    public function key() : string
48
    {
49
        return basename($this->path());
50
    }
51
52
    /**
53
     * Supported http requests.
54
     *
55
     * @return array
56
     */
57
    public function allowedHttpVerbs() : array
58
    {
59
        return [
60
            HttpRequest::METHOD_GET,
61
            HttpRequest::METHOD_POST,
62
            HttpRequest::METHOD_PATCH,
63
            HttpRequest::METHOD_DELETE,
64
            HttpRequest::METHOD_HEAD,
65
            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
    public function header(string $key, $default = null)
78
    {
79
        return $this->request->headers->get($key, $default);
80
    }
81
82
    /**
83
     * Get the root URL for the request.
84
     *
85
     * @return string
86
     */
87
    public function url() : string
88
    {
89
        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
    public function extractFromHeader(string $key, string $value) : array
101
    {
102
        $meta = $this->header($key);
103
104
        if (false !== strpos($meta, $value)) {
105
            $meta = trim(str_replace($value, '', $meta));
106
107
            return explode(' ', $meta) ?? [];
108
        }
109
110
        return [];
111
    }
112
113
    /**
114
     * Extract base64 encoded filename from header.
115
     *
116
     * @return string
117
     */
118
    public function extractFileName() : string
119
    {
120
        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
    public function extractMeta(string $requestedKey) : string
131
    {
132
        $uploadMetaData = $this->request->headers->get('Upload-Metadata');
133
134
        if (empty($uploadMetaData)) {
135
            return '';
136
        }
137
138
        $uploadMetaDataChunks = explode(',', $uploadMetaData);
139
140
        foreach ($uploadMetaDataChunks as $chunk) {
141
            list($key, $value) = explode(' ', $chunk);
142
143
            if ($key === $requestedKey) {
144
                return base64_decode($value);
145
            }
146
        }
147
148
        return '';
149
    }
150
151
    /**
152
     * Extract partials from header.
153
     *
154
     * @return array
155
     */
156
    public function extractPartials() : array
157
    {
158
        return $this->extractFromHeader('Upload-Concat', 'final;');
159
    }
160
161
    /**
162
     * Check if this is a partial upload request.
163
     *
164
     * @return bool
165
     */
166
    public function isPartial() : bool
167
    {
168
        return $this->header('Upload-Concat') === 'partial';
169
    }
170
171
    /**
172
     * Check if this is a final concatenation request.
173
     *
174
     * @return bool
175
     */
176
    public function isFinal() : bool
177
    {
178
        return false !== strpos($this->header('Upload-Concat'), 'final;');
179
    }
180
181
    /**
182
     * Get request.
183
     *
184
     * @return HttpRequest
185
     */
186
    public function getRequest() : HttpRequest
187
    {
188
        return $this->request;
189
    }
190
}
191