Completed
Push — master ( 573033...f6138e )
by Ankit
02:21
created

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