Completed
Push — master ( 2d5123...0bd1e8 )
by recca
01:56
created

ChunkFile::appendFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
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 \Recca0120\Upload\Filesystem $files
69
     */
70 14
    public function __construct(Filesystem $files = null)
71
    {
72 14
        $this->files = $files ?: new Filesystem();
73 14
    }
74
75
    /**
76
     * setToken.
77
     *
78
     * @param string $token
79
     * @return $this
80
     */
81 3
    public function setToken($token)
82
    {
83 3
        $this->token = $token;
84
85 3
        return $this;
86
    }
87
88
    /**
89
     * setChunkPath.
90
     *
91
     * @param string $chunkPath
92
     * @return $this
93
     */
94 3
    public function setChunkPath($chunkPath)
95
    {
96 3
        $this->chunkPath = $chunkPath;
97
98 3
        return $this;
99
    }
100
101
    /**
102
     * setStoragePath.
103
     *
104
     * @param string $storagePath
105
     * @return $this
106
     */
107 1
    public function setStoragePath($storagePath)
108
    {
109 1
        $this->storagePath = $storagePath;
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * setName.
116
     *
117
     * @param string $name
118
     * @return $this
119
     */
120 3
    public function setName($name)
121
    {
122 3
        $this->name = $name;
123
124 3
        return $this;
125
    }
126
127
    /**
128
     * setMimeType.
129
     *
130
     * @param string $mimeType
131
     * @return $this
132
     */
133 1
    public function setMimeType($mimeType)
134
    {
135 1
        $this->mimeType = $mimeType;
136
137 1
        return $this;
138
    }
139
140
    /**
141
     * throwException.
142
     *
143
     * @param mixed $message
144
     * @param array $headers
145
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
146
     */
147 3
    public function throwException($message = '', $headers = [])
148
    {
149 3
        throw new ChunkedResponseException($message, $headers);
150
    }
151
152
    /**
153
     * appendStream.
154
     *
155
     * @param mixed $source
156
     * @param int $offset
157
     * @return $this
158
     */
159 1
    public function appendStream($source, $offset = 0)
160
    {
161 1
        $chunkFile = $this->chunkFile();
162 1
        $this->files->appendStream($chunkFile, $source, (int) $offset);
163
164 1
        return $this;
165
    }
166
167
    /**
168
     * appendFile.
169
     *
170
     * @param mixed $source
171
     * @param int $index
172
     * @return $this
173
     */
174 1
    public function appendFile($source, $index = 0)
175
    {
176 1
        $chunkFile = $this->chunkFile().'.'.$index;
177 1
        $this->files->appendStream($chunkFile, $source, 0);
178
179 1
        return $this;
180
    }
181
182
    /**
183
     * createUploadedFile.
184
     *
185
     * @return \Illuminate\Http\UploadedFile
186
     */
187 1
    public function createUploadedFile($chunks = null, $storageFile = null)
188
    {
189 1
        $chunkFile = $this->chunkFile();
190 1
        $storageFile = $storageFile ?: $this->storageFile();
191
192 1
        if (is_null($chunks) === false) {
193
            for ($i = 0; $i < $chunks; $i++) {
194
                $chunk = $chunkFile.'.'.$i;
195
                $this->files->append(
196
                    $storageFile,
197
                    $this->files->get($chunk)
198
                );
199
                $this->files->delete($chunk);
200
            }
201
        } else {
202 1
            $this->files->move($chunkFile, $storageFile);
203
        }
204
205 1
        return $this->files->createUploadedFile(
206 1
            $storageFile, $this->name, $this->mimeType
207 1
        );
208
    }
209
210
    /**
211
     * tmpfilename.
212
     *
213
     * @return string
214
     */
215 3
    protected function tmpfilename()
216
    {
217 3
        if (is_null($this->tmpfilename) === true) {
218 3
            $this->tmpfilename = $this->files->tmpfilename($this->name, $this->token);
219 3
        }
220
221 3
        return $this->tmpfilename;
222
    }
223
224
    /**
225
     * chunkFile.
226
     *
227
     * @return string
228
     */
229 3
    protected function chunkFile()
230
    {
231 3
        return $this->chunkPath.$this->tmpfilename().static::TMPFILE_EXTENSION;
232
    }
233
234
    /**
235
     * storageFile.
236
     *
237
     * @return string
238
     */
239 1
    protected function storageFile()
240
    {
241 1
        return $this->storagePath.$this->tmpfilename();
242
    }
243
}
244