Completed
Push — master ( 40400a...73c84a )
by recca
02:55
created

ChunkFile::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
     * $token.
18
     *
19
     * @var string
20
     */
21
    protected $token = null;
22
23
    /**
24
     * $chunkPath.
25
     *
26
     * @var string
27
     */
28
    protected $chunkPath = null;
29
30
    /**
31
     * $storagePath.
32
     *
33
     * @var string
34
     */
35
    protected $storagePath = null;
36
37
    /**
38
     * $name.
39
     *
40
     * @var string
41
     */
42
    protected $name = null;
43
44
    /**
45
     * $mimeType.
46
     *
47
     * @var string
48
     */
49
    protected $mimeType = null;
50
51
    /**
52
     * $tmpfilename.
53
     *
54
     * @var string
55
     */
56
    protected $tmpfilename = null;
57
58
    /**
59
     * __construct.
60
     *
61
     * @param \Recca0120\Upload\Filesystem $filesystem
62
     */
63 17
    public function __construct(Filesystem $filesystem = null)
64
    {
65 17
        $this->filesystem = $filesystem ?: new Filesystem();
0 ignored issues
show
Bug introduced by
The property filesystem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66 17
    }
67
68
    /**
69
     * setToken.
70
     *
71
     * @param string $token
72
     * @return $this
73
     */
74 5
    public function setToken($token)
75
    {
76 5
        $this->token = $token;
77
78 5
        return $this;
79
    }
80
81
    /**
82
     * setChunkPath.
83
     *
84
     * @param string $chunkPath
85
     * @return $this
86
     */
87 5
    public function setChunkPath($chunkPath)
88
    {
89 5
        $this->chunkPath = $chunkPath;
90
91 5
        return $this;
92
    }
93
94
    /**
95
     * setStoragePath.
96
     *
97
     * @param string $storagePath
98
     * @return $this
99
     */
100 5
    public function setStoragePath($storagePath)
101
    {
102 5
        $this->storagePath = $storagePath;
103
104 5
        return $this;
105
    }
106
107
    /**
108
     * setName.
109
     *
110
     * @param string $name
111
     * @return $this
112
     */
113 5
    public function setName($name)
114
    {
115 5
        $this->name = $name;
116
117 5
        return $this;
118
    }
119
120
    /**
121
     * setMimeType.
122
     *
123
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
124
     * @return $this
125
     */
126 5
    public function setMimeType($mimeType)
127
    {
128 5
        $this->mimeType = $mimeType;
129
130 5
        return $this;
131
    }
132
133
    /**
134
     * throwException.
135
     *
136
     * @param string $message
137
     * @param array $headers
138
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
139
     */
140 2
    public function throwException($message = '', $headers = [])
141
    {
142 2
        throw new ChunkedResponseException($message, $headers);
143
    }
144
145
    /**
146
     * appendStream.
147
     *
148
     * @param mixed $source
149
     * @param int $offset
150
     * @return $this
151
     */
152 5
    public function appendStream($source, $offset = 0)
153
    {
154 5
        $this->filesystem->appendStream($this->chunkFile(), $source, (int) $offset);
155
156 5
        return $this;
157
    }
158
159 3
    public function createUploadedFile()
160
    {
161 3
        $this->filesystem->move(
162 3
            $this->chunkFile(), $storageFile = $this->storageFile()
163 3
        );
164
165 3
        return $this->filesystem->createUploadedFile(
166 3
            $storageFile,
167 3
            $this->name,
168 3
            $this->mimeType,
169 3
            $this->filesystem->size($storageFile)
170 3
        );
171
    }
172
173
    /**
174
     * tmpfilename.
175
     *
176
     * @return string
177
     */
178 5
    protected function tmpfilename()
179
    {
180 5
        if (is_null($this->tmpfilename) === true) {
181 5
            $this->tmpfilename = $this->filesystem->tmpfilename($this->name, $this->token);
182 5
        }
183
184 5
        return $this->tmpfilename;
185
    }
186
187
    /**
188
     * chunkFile.
189
     *
190
     * @return string
191
     */
192 5
    protected function chunkFile()
193
    {
194 5
        return $this->chunkPath.$this->tmpfilename().static::TMPFILE_EXTENSION;
195
    }
196
197
    /**
198
     * storageFile.
199
     *
200
     * @return string
201
     */
202 3
    protected function storageFile()
203
    {
204 3
        return $this->storagePath.$this->tmpfilename();
205
    }
206
}
207