Passed
Pull Request — master (#82)
by
unknown
01:58
created

Request::extractFileName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
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 1
    public function extractFromHeader(string $key, string $value) : array
101
    {
102 1
        $meta = $this->header($key);
103
104 1
        if (false !== strpos($meta, $value)) {
105 1
            $meta = trim(str_replace($value, '', $meta));
106
107 1
            return explode(' ', $meta) ?? [];
108
        }
109
110 1
        return [];
111
    }
112
113
    /**
114
     * Extract base64 encoded filename from header.
115
     *
116
     * @return string|null
117
     */
118 3
    public function extractFileName() : ?string
119
    {
120 3
        $meta = $this->header('Upload-Metadata');
121
122 3
        if (empty($meta)) {
123 1
            return null;
124
        }
125
126 2
        $pieces = explode(',', $meta);
127 2
        foreach ($pieces as $piece) {
128 2
          list($metaName, $metaValue) = explode(' ', $piece);
129 2
          $metaValues[$metaName] = base64_decode($metaValue);
130
        }
131
132 2
        return $metaValues['filename'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $metaValues seems to be defined by a foreach iteration on line 127. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
133
    }
134
135
    /**
136
     * Extract partials from header.
137
     *
138
     * @return array
139
     */
140
    public function extractPartials() : array
141
    {
142
        return $this->extractFromHeader('Upload-Concat', 'final;');
143
    }
144
145
    /**
146
     * Check if this is a partial upload request.
147
     *
148
     * @return bool
149
     */
150
    public function isPartial() : bool
151
    {
152
        return $this->header('Upload-Concat') === 'partial';
153
    }
154
155
    /**
156
     * Check if this is a final concatenation request.
157
     *
158
     * @return bool
159
     */
160
    public function isFinal() : bool
161
    {
162
        return false !== strpos($this->header('Upload-Concat'), 'final;');
163
    }
164
165
    /**
166
     * Get request.
167
     *
168
     * @return HttpRequest
169
     */
170
    public function getRequest() : HttpRequest
171
    {
172
        return $this->request;
173
    }
174
}
175