ChunkFile::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 6
crap 2
1
<?php
2
3
namespace Recca0120\Upload;
4
5
use ErrorException;
6
use Recca0120\Upload\Exceptions\ChunkedResponseException;
7
8
class ChunkFile
9
{
10
    /**
11
     * TMPFILE_EXTENSION.
12
     *
13
     * @var string
14
     */
15
    const TMPFILE_EXTENSION = '.part';
16
17
    /**
18
     * $files.
19
     *
20
     * @var \Recca0120\Upload\Filesystem
21
     */
22
    protected $files;
23
24
    /**
25
     * $token.
26
     *
27
     * @var string
28
     */
29
    protected $token = null;
30
31
    /**
32
     * $chunkPath.
33
     *
34
     * @var string
35
     */
36
    protected $chunkPath = null;
37
38
    /**
39
     * $storagePath.
40
     *
41
     * @var string
42
     */
43
    protected $storagePath = null;
44
45
    /**
46
     * $name.
47
     *
48
     * @var string
49
     */
50
    protected $name = null;
51
52
    /**
53
     * $mimeType.
54
     *
55
     * @var string
56
     */
57
    protected $mimeType = null;
58
59
    /**
60
     * $tmpfilename.
61
     *
62
     * @var string
63
     */
64
    protected $tmpfilename = null;
65
66
    /**
67
     * __construct.
68
     *
69
     * @param string $name
70
     * @param string $chunkPath
71
     * @param string $storagePath
72
     * @param string $mimeType
73
     * @param string $token
74
     * @param \Recca0120\Upload\Filesystem $files
75
     */
76 5
    public function __construct($name, $chunkPath, $storagePath, $token = null, $mimeType = null, Filesystem $files = null)
77
    {
78 5
        $this->files = $files ?: new Filesystem();
79 5
        $this->name = $name;
80 5
        $this->chunkPath = $chunkPath;
81 5
        $this->storagePath = $storagePath;
82 5
        $this->token = $token;
83 5
        $this->mimeType = $mimeType;
84 5
    }
85
86
    /**
87
     * getMimeType.
88
     *
89
     * @return string
90
     */
91 1
    public function getMimeType()
92
    {
93
        try {
94 1
            return $this->mimeType ?: $this->files->mimeType($this->name);
95
        } catch (ErrorException $e) {
96
            return;
97
        }
98
    }
99
100
    /**
101
     * throwException.
102
     *
103
     * @param mixed $message
104
     * @param array $headers
105
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
106
     */
107 3
    public function throwException($message = '', $headers = [])
108
    {
109 3
        throw new ChunkedResponseException($message, $headers);
110
    }
111
112
    /**
113
     * appendStream.
114
     *
115
     * @param mixed $source
116
     * @param int $offset
117
     * @return $this
118
     */
119 1
    public function appendStream($source, $offset = 0)
120
    {
121 1
        $chunkFile = $this->chunkFile();
122 1
        $this->files->appendStream($chunkFile, $source, (int) $offset);
123
124 1
        return $this;
125
    }
126
127
    /**
128
     * appendFile.
129
     *
130
     * @param mixed $source
131
     * @param int $index
132
     * @return $this
133
     */
134 1
    public function appendFile($source, $index = 0)
135
    {
136 1
        $chunkFile = $this->chunkFile().'.'.$index;
137 1
        $this->files->appendStream($chunkFile, $source, 0);
138
139 1
        return $this;
140
    }
141
142
    /**
143
     * createUploadedFile.
144
     *
145
     * @return \Illuminate\Http\UploadedFile
146
     */
147 1
    public function createUploadedFile($chunks = null, $storageFile = null)
148
    {
149 1
        $chunkFile = $this->chunkFile();
150 1
        $storageFile = $storageFile ?: $this->storageFile();
151
152 1
        if (is_null($chunks) === false) {
153
            for ($i = 0; $i < $chunks; $i++) {
154
                $chunk = $chunkFile.'.'.$i;
155
                $this->files->append(
156
                    $storageFile,
157
                    $this->files->get($chunk)
158
                );
159
                $this->files->delete($chunk);
160
            }
161
        } else {
162 1
            $this->files->move($chunkFile, $storageFile);
163
        }
164
165 1
        return $this->files->createUploadedFile(
166 1
            $storageFile, $this->name, $this->getMimeType()
0 ignored issues
show
Security Bug introduced by
It seems like $this->getMimeType() targeting Recca0120\Upload\ChunkFile::getMimeType() can also be of type false; however, Recca0120\Upload\Filesystem::createUploadedFile() does only seem to accept string|null, did you maybe forget to handle an error condition?
Loading history...
167
        );
168
    }
169
170
    /**
171
     * tmpfilename.
172
     *
173
     * @return string
174
     */
175 3
    protected function tmpfilename()
176
    {
177 3
        if (is_null($this->tmpfilename) === true) {
178 3
            $this->tmpfilename = $this->files->tmpfilename($this->name, $this->token);
179
        }
180
181 3
        return $this->tmpfilename;
182
    }
183
184
    /**
185
     * chunkFile.
186
     *
187
     * @return string
188
     */
189 3
    protected function chunkFile()
190
    {
191 3
        return $this->chunkPath.$this->tmpfilename().static::TMPFILE_EXTENSION;
192
    }
193
194
    /**
195
     * storageFile.
196
     *
197
     * @return string
198
     */
199 1
    protected function storageFile()
200
    {
201 1
        return $this->storagePath.$this->tmpfilename();
202
    }
203
}
204