Completed
Pull Request — master (#71)
by Ankit
02:02
created

Request::method()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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|null
117
     */
118
    public function extractFileName()
119
    {
120
        $meta = $this->header('Upload-Metadata');
121
122
        if (empty($meta)) {
123
            return null;
124
        }
125
126
        if (false !== strpos($meta, ',')) {
127
            $pieces = explode(',', $meta);
128
129
            list(/* $key */, $file) = explode(' ', $pieces[0]);
130
        } else {
131
            list(/* $key */, $file) = explode(' ', $meta);
132
        }
133
134
        return base64_decode($file);
135
    }
136
137
    /**
138
     * Extract partials from header.
139
     *
140
     * @return array
141
     */
142
    public function extractPartials() : array
143
    {
144
        return $this->extractFromHeader('Upload-Concat', 'final;');
145
    }
146
147
    /**
148
     * Check if this is a partial upload request.
149
     *
150
     * @return bool
151
     */
152
    public function isPartial() : bool
153
    {
154
        return $this->header('Upload-Concat') === 'partial';
155
    }
156
157
    /**
158
     * Check if this is a final concatenation request.
159
     *
160
     * @return bool
161
     */
162
    public function isFinal() : bool
163
    {
164
        return false !== strpos($this->header('Upload-Concat'), 'final;');
165
    }
166
167
    /**
168
     * Get request.
169
     *
170
     * @return HttpRequest
171
     */
172
    public function getRequest()
173
    {
174
        return $this->request;
175
    }
176
}
177