Completed
Push — master ( 42366c...c37bfa )
by recca
03:38
created

FileAPI   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.24%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 93
ccs 40
cts 42
cp 0.9524
rs 10
c 3
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B receive() 0 30 3
A getOriginalName() 0 12 2
A getMimeType() 0 9 2
A parseContentRange() 0 11 2
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
        $completed = $end >= $total - 1;
26
27 3
        $this->chunkFile
28 3
            ->setToken($this->request->get('token'))
29 3
            ->setChunkPath($this->chunkPath())
30 3
            ->setStoragePath($this->storagePath())
31 3
            ->setName($originalName)
32 3
            ->setMimeType($mimeType)
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\ChunkFile::setMimeType() does only seem to accept string, 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...
33 3
            ->appendStream('php://input', $start);
34
35
        return $completed === true
36 3
            ? $this->chunkFile->createUploadedFile()
37 3
            : $this->chunkFile->throwException([
38
                'files' => [
39 1
                    'name' => $originalName,
40 1
                    'size' => $end,
41 1
                    'type' => $mimeType,
42 1
                ],
43 3
            ], ['X-Last-Known-Byte' => $end]);
44
    }
45
46
    /**
47
     * getOriginalName.
48
     *
49
     * @param string $contentDisposition
50
     * @return string
51
     */
52 3
    protected function getOriginalName($contentDisposition)
53
    {
54 3
        $originalName = (string) $this->request->get('name');
55 3
        if (empty($originalName) === true) {
56 3
            list($originalName) = sscanf(
57 3
                $contentDisposition,
58
                'attachment; filename=%s'
59 3
            );
60 3
        }
61
62 3
        return preg_replace('/[\'"]/', '', $originalName);
63
    }
64
65
    /**
66
     * getMimeType.
67
     *
68
     * @param string $originalName
69
     * @return string
70
     */
71 3
    protected function getMimeType($originalName)
72
    {
73 3
        $mimeType = (string) $this->request->header('content-type');
74 3
        if (empty($mimeType) === true) {
75
            $mimeType = $this->files->mimeType($originalName);
76
        }
77
78 3
        return $mimeType;
79
    }
80
81
    /**
82
     * parseContentRange.
83
     *
84
     * @return array
85
     */
86 3
    protected function parseContentRange()
87
    {
88 3
        $contentRange = $this->request->header('content-range');
89 3
        if (empty($contentRange) === false) {
90 2
            return sscanf($contentRange, 'bytes %d-%d/%d');
91
        }
92
93 1
        $total = $end = (int) $this->request->header('content-length');
94
95 1
        return [0, $end, $total];
96
    }
97
}
98