Completed
Push — master ( 8e0587...afc775 )
by recca
01:27
created

ChunkFile::getMimeType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Recca0120\Upload;
4
5
use Recca0120\Upload\Exceptions\ChunkedResponseException;
6
7
class ChunkFile
8
{
9
    /**
10
     * TMPFILE_EXTENSION.
11
     *
12
     * @var string
13
     */
14
    const TMPFILE_EXTENSION = '.part';
15
16
    /**
17
     * $files.
18
     *
19
     * @var \Recca0120\Upload\Filesystem
20
     */
21
    protected $files;
22
23
    /**
24
     * $token.
25
     *
26
     * @var string
27
     */
28
    protected $token = null;
29
30
    /**
31
     * $chunkPath.
32
     *
33
     * @var string
34
     */
35
    protected $chunkPath = null;
36
37
    /**
38
     * $storagePath.
39
     *
40
     * @var string
41
     */
42
    protected $storagePath = null;
43
44
    /**
45
     * $name.
46
     *
47
     * @var string
48
     */
49
    protected $name = null;
50
51
    /**
52
     * $mimeType.
53
     *
54
     * @var string
55
     */
56
    protected $mimeType = null;
57
58
    /**
59
     * $tmpfilename.
60
     *
61
     * @var string
62
     */
63
    protected $tmpfilename = null;
64
65
    /**
66
     * __construct.
67
     *
68
     * @param string $name
69
     * @param string $chunkPath
70 14
     * @param string $storagePath
71
     * @param string $token
72 14
     * @param \Recca0120\Upload\Filesystem $files
73 14
     */
74
    public function __construct($name, $chunkPath, $storagePath, $token = null, Filesystem $files = null)
75
    {
76
        $this->files = $files ?: new Filesystem();
77
        $this->name = $name;
78
        $this->chunkPath = $chunkPath;
79
        $this->storagePath = $storagePath;
80
        $this->token = $token;
81 3
        $this->mimeType = $this->files->mimeType($this->name);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->files->mimeType($this->name) can also be of type false. However, the property $mimeType is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
82
    }
83 3
84
    /**
85 3
     * getMimeType.
86
     *
87
     * @return string
88
     */
89
    public function getMimeType()
90
    {
91
        return $this->mimeType;
92
    }
93
94 3
    /**
95
     * throwException.
96 3
     *
97
     * @param mixed $message
98 3
     * @param array $headers
99
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
100
     */
101
    public function throwException($message = '', $headers = [])
102
    {
103
        throw new ChunkedResponseException($message, $headers);
104
    }
105
106
    /**
107 1
     * appendStream.
108
     *
109 1
     * @param mixed $source
110
     * @param int $offset
111 1
     * @return $this
112
     */
113
    public function appendStream($source, $offset = 0)
114
    {
115
        $chunkFile = $this->chunkFile();
116
        $this->files->appendStream($chunkFile, $source, (int) $offset);
117
118
        return $this;
119
    }
120 3
121
    /**
122 3
     * appendFile.
123
     *
124 3
     * @param mixed $source
125
     * @param int $index
126
     * @return $this
127
     */
128
    public function appendFile($source, $index = 0)
129
    {
130
        $chunkFile = $this->chunkFile().'.'.$index;
131
        $this->files->appendStream($chunkFile, $source, 0);
132
133 1
        return $this;
134
    }
135 1
136
    /**
137 1
     * createUploadedFile.
138
     *
139
     * @return \Illuminate\Http\UploadedFile
140
     */
141
    public function createUploadedFile($chunks = null, $storageFile = null)
142
    {
143
        $chunkFile = $this->chunkFile();
144
        $storageFile = $storageFile ?: $this->storageFile();
145
146
        if (is_null($chunks) === false) {
147 3
            for ($i = 0; $i < $chunks; $i++) {
148
                $chunk = $chunkFile.'.'.$i;
149 3
                $this->files->append(
150
                    $storageFile,
151
                    $this->files->get($chunk)
152
                );
153
                $this->files->delete($chunk);
154
            }
155
        } else {
156
            $this->files->move($chunkFile, $storageFile);
157
        }
158
159 1
        return $this->files->createUploadedFile(
160
            $storageFile, $this->name, $this->mimeType
161 1
        );
162 1
    }
163
164 1
    /**
165
     * tmpfilename.
166
     *
167
     * @return string
168
     */
169
    protected function tmpfilename()
170
    {
171
        if (is_null($this->tmpfilename) === true) {
172
            $this->tmpfilename = $this->files->tmpfilename($this->name, $this->token);
173
        }
174 1
175
        return $this->tmpfilename;
176 1
    }
177 1
178
    /**
179 1
     * chunkFile.
180
     *
181
     * @return string
182
     */
183
    protected function chunkFile()
184
    {
185
        return $this->chunkPath.$this->tmpfilename().static::TMPFILE_EXTENSION;
186
    }
187 1
188
    /**
189 1
     * storageFile.
190 1
     *
191
     * @return string
192 1
     */
193
    protected function storageFile()
194
    {
195
        return $this->storagePath.$this->tmpfilename();
196
    }
197
}
198