Completed
Push — master ( 88af5c...2f18e2 )
by recca
07:12
created

FileAPI::getMimeType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Recca0120\Upload;
4
5
class FileAPI extends Api
6
{
7
    /**
8
     * receive.
9
     *
10
     * @param string $name
11
     * @return \Symfony\Component\HttpFoundation\File\UploadedFile
12
     *
13
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
14
     */
15 4
    public function receive($name)
16
    {
17 4
        $contentDisposition = (string) $this->request->header('content-disposition');
18 4
        if (empty($contentDisposition) === true) {
19 1
            return $this->request->file($name);
20
        }
21
22 3
        list($start, $end, $total) = $this->parseContentRange();
23 3
        $originalName = $this->getOriginalName($contentDisposition);
24 3
        $mimeType = $this->getMimeType($originalName);
25 3
        $uuid = $this->request->get('token');
26 3
        $completed = $end >= $total - 1;
27
28 3
        $chunkFile = $this->createChunkFile($originalName, $mimeType, $uuid);
0 ignored issues
show
Security Bug introduced by
It seems like $mimeType defined by $this->getMimeType($originalName) on line 24 can also be of type false; however, Recca0120\Upload\Api::createChunkFile() does only seem to accept string|null, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
29 3
        $chunkFile->appendStream('php://input', $start);
30
31 3
        return $completed === true
32 2
            ? $chunkFile->createUploadedFile()
33 1
            : $chunkFile->throwException([
34
                'files' => [
35 1
                    'name' => $originalName,
36 1
                    'size' => $end,
37 1
                    'type' => $chunkFile->getMimeType(),
38
                ],
39 3
            ], ['X-Last-Known-Byte' => $end]);
40
    }
41
42
    /**
43
     * getOriginalName.
44
     *
45
     * @param string $contentDisposition
46
     * @return string
47
     */
48 3
    protected function getOriginalName($contentDisposition)
49
    {
50 3
        $originalName = (string) $this->request->get('name');
51 3
        if (empty($originalName) === true) {
52 3
            list($originalName) = sscanf(
53 3
                $contentDisposition,
54 3
                'attachment; filename=%s'
55
            );
56
        }
57
58 3
        return preg_replace('/[\'"]/', '', $originalName);
59
    }
60
61
    /**
62
     * getMimeType.
63
     *
64
     * @param string $originalName
65
     * @return string
66
     */
67 3
    protected function getMimeType($originalName)
68
    {
69 3
        $mimeType = (string) $this->request->header('content-type');
70 3
        if (empty($mimeType) === true) {
71
            $mimeType = $this->files->mimeType($originalName);
72
        }
73
74 3
        return $mimeType;
75
    }
76
77
    /**
78
     * parseContentRange.
79
     *
80
     * @return array
81
     */
82 3
    protected function parseContentRange()
83
    {
84 3
        $contentRange = $this->request->header('content-range');
85 3
        if (empty($contentRange) === false) {
86 2
            return sscanf($contentRange, 'bytes %d-%d/%d');
87
        }
88
89 1
        $total = $end = (int) $this->request->header('content-length');
90
91 1
        return [0, $end, $total];
92
    }
93
}
94