Completed
Push — master ( 856ad6...a8c7fa )
by Ankit
02:24
created

Request::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
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
     * @return null|string
34
     */
35 1
    public function checksum()
36
    {
37 1
        return $this->request->segment(2);
38
    }
39
40
    /**
41
     * Supported http requests.
42
     *
43
     * @return array
44
     */
45 1
    public function allowedHttpVerbs() : array
46
    {
47
        return [
48 1
            HttpRequest::METHOD_GET,
49 1
            HttpRequest::METHOD_POST,
50 1
            HttpRequest::METHOD_PATCH,
51 1
            HttpRequest::METHOD_DELETE,
52 1
            HttpRequest::METHOD_HEAD,
53 1
            HttpRequest::METHOD_OPTIONS,
54
        ];
55
    }
56
57
    /**
58
     * Retrieve a header from the request.
59
     *
60
     * @param  string            $key
61
     * @param  string|array|null $default
62
     *
63
     * @return string|null
64
     */
65 1
    public function header(string $key, $default = null)
66
    {
67 1
        return $this->request->header($key, $default);
68
    }
69
70
    /**
71
     * Get the root URL for the request.
72
     *
73
     * @return string
74
     */
75 1
    public function url() : string
76
    {
77 1
        return $this->request->root();
78
    }
79
80
    /**
81
     * Extract metadata from header.
82
     *
83
     * @param string $key
84
     * @param string $value
85
     *
86
     * @return array
87
     */
88 3
    public function extractFromHeader(string $key, string $value) : array
89
    {
90 3
        $meta = $this->header($key);
91
92 3
        if (false !== strpos($meta, $value)) {
93 3
            $meta = trim(str_replace($value, '', $meta));
94
95 3
            return explode(' ', $meta) ?? [];
96
        }
97
98 1
        return [];
99
    }
100
101
    /**
102
     * Extract base64 encoded filename from header.
103
     *
104
     * @return string|null
105
     */
106 2
    public function extractFileName()
107
    {
108 2
        $file = current($this->extractFromHeader('Upload-Metadata', 'filename'));
109
110 2
        return $file ? base64_decode($file) : null;
111
    }
112
113
    /**
114
     * Extract partials from header.
115
     *
116
     * @return array
117
     */
118 1
    public function extractPartials() : array
119
    {
120 1
        return $this->extractFromHeader('Upload-Concat', 'final;');
121
    }
122
123
    /**
124
     * Check if this is a partial upload request.
125
     *
126
     * @return bool
127
     */
128 1
    public function isPartial() : bool
129
    {
130 1
        return $this->header('Upload-Concat') === 'partial';
131
    }
132
133
    /**
134
     * Check if this is a final concatenation request.
135
     *
136
     * @return bool
137
     */
138 1
    public function isFinal() : bool
139
    {
140 1
        return false !== strpos($this->header('Upload-Concat'), 'final;');
141
    }
142
143
    /**
144
     * Get request.
145
     *
146
     * @return HttpRequest
147
     */
148 1
    public function getRequest()
149
    {
150 1
        return $this->request;
151
    }
152
}
153