Completed
Push — master ( ef5a6b...11193f )
by recca
01:36
created

ChunkFile   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80.48%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 191
ccs 33
cts 41
cp 0.8048
rs 10

9 Methods

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