Filesystem   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 0
loc 89
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A basename() 0 4 1
A tmpfilename() 0 4 1
A appendStream() 0 14 3
A createUploadedFile() 0 10 4
A convertToResource() 0 13 4
1
<?php
2
3
namespace Recca0120\Upload;
4
5
use Recca0120\Upload\Exceptions\ResourceOpenException;
6
use Illuminate\Filesystem\Filesystem as IlluminateFilesystem;
7
8
class Filesystem extends IlluminateFilesystem
9
{
10
    /**
11
     * Extract the trailing name component from a file path.
12
     *
13
     * @param string $path
14
     * @return string
15
     */
16 1
    public function basename($path)
17
    {
18 1
        return pathinfo($path, PATHINFO_BASENAME);
19
    }
20
21
    /**
22
     * tmpfilename.
23
     *
24
     * @param string $path
25
     * @param string $hash
26
     * @return string
27
     */
28 1
    public function tmpfilename($path, $hash = null)
29
    {
30 1
        return md5($path.$hash).'.'.strtolower($this->extension($path));
31
    }
32
33
    /**
34
     * appendStream.
35
     *
36
     * @param string $output
37
     * @param string|resource $input
38
     * @param int $offset
39
     */
40 3
    public function appendStream($output, $input, $offset = 0)
41
    {
42 3
        $mode = ($offset === 0) ? 'wb' : 'ab';
43 3
        $output = $this->convertToResource($output, $mode, 'output');
44 2
        $input = $this->convertToResource($input, 'rb', 'input');
45
46 1
        fseek($output, $offset);
47 1
        while ($buffer = fread($input, 4096)) {
48 1
            fwrite($output, $buffer);
49
        }
50
51 1
        fclose($output);
52 1
        fclose($input);
53 1
    }
54
55
    /**
56
     * createUploadedFile.
57
     *
58
     * @param string $path
59
     * @param string $originalName
60
     * @param string $mimeType
61
     * @param int $size
62
     * @return \Illuminate\Http\UploadedFile
63
     */
64 1
    public function createUploadedFile($path, $originalName, $mimeType = null, $size = null)
65
    {
66 1
        $class = class_exists('Illuminate\Http\UploadedFile') === true ?
67 1
            'Illuminate\Http\UploadedFile' : 'Symfony\Component\HttpFoundation\File\UploadedFile';
68
69 1
        $mimeType = $mimeType ?: $this->mimeType($path);
70 1
        $size = $size ?: $this->size($path);
71
72 1
        return new $class($path, $originalName, $mimeType, $size, UPLOAD_ERR_OK, true);
73
    }
74
75
    /**
76
     * convertToResource.
77
     *
78
     * @param string|resource $resource
79
     * @param string $mode
80
     * @param string $type
81
     * @return resource
82
     */
83 3
    protected function convertToResource($resource, $mode = 'wb', $type = 'input')
84
    {
85 3
        $resource = is_resource($resource) === true ?
86 3
            $resource : @fopen($resource, $mode);
87
88 3
        if (is_resource($resource) === false) {
89 2
            $code = $type === 'input' ? 101 : 102;
90
91 2
            throw new ResourceOpenException('Failed to open '.$type.' stream.', $code);
92
        }
93
94 2
        return $resource;
95
    }
96
}
97