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

Api   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 156
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A domain() 0 4 1
A path() 0 4 1
A makeDirectory() 0 8 2
A cleanDirectory() 0 13 4
receive() 0 1 ?
A deleteUploadedFile() 0 10 2
A completedResponse() 0 4 1
A chunkPath() 0 6 1
A storagePath() 0 6 1
1
<?php
2
3
namespace Recca0120\Upload;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use Recca0120\Upload\Contracts\Api as ApiContract;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
abstract class Api implements ApiContract
11
{
12
    /**
13
     * $request.
14
     *
15
     * @var \Illuminate\Http\Request
16
     */
17
    public $request;
18
19
    /**
20
     * $filesystem.
21
     *
22
     * @var \Recca0120\Upload\Filesystem
23
     */
24
    public $filesystem;
25
26
    /**
27
     * $config.
28
     *
29
     * @var array
30
     */
31
    protected $config;
32
33
    /**
34
     * __construct.
35
     *
36
     * @param array $config
37
     * @param \Illuminate\Http\Request $request
38
     * @param \Recca0120\Upload\Filesystem $filesystem
39
     */
40 17
    public function __construct($config = [], Request $request = null, Filesystem $filesystem = null, ChunkFile $chunkFile = null)
41
    {
42 17
        $this->request = $request ?: Request::capture();
43 17
        $this->filesystem = $filesystem ?: new Filesystem();
44 17
        $this->chunkFile = $chunkFile ?: new ChunkFile($this->filesystem);
0 ignored issues
show
Bug introduced by
The property chunkFile 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...
45 17
        $this->config = array_merge([
46 17
            'chunks' => sys_get_temp_dir().'/chunks',
47 17
            'storage' => 'storage/temp',
48 17
            'domain' => $this->request->root(),
49 17
            'path' => 'storage/temp',
50 17
        ], $config);
51 17
    }
52
53
    /**
54
     * domain.
55
     *
56
     * @return string
57
     */
58 1
    public function domain()
59
    {
60 1
        return rtrim($this->config['domain'], '/').'/';
61
    }
62
63
    /**
64
     * path.
65
     *
66
     * @return string
67
     */
68 1
    public function path()
69
    {
70 1
        return rtrim($this->config['path'], '/').'/';
71
    }
72
73
    /**
74
     * makeDirectory.
75
     *
76
     * @return $this
77
     */
78 6
    public function makeDirectory($path)
79
    {
80 6
        if ($this->filesystem->isDirectory($path) === false) {
81 1
            $this->filesystem->makeDirectory($path, 0777, true, true);
82 1
        }
83
84 6
        return $this;
85
    }
86
87
    /**
88
     * cleanDirectory.
89
     */
90 2
    public function cleanDirectory($path)
91
    {
92 2
        $time = time();
93 2
        $maxFileAge = 3600;
94 2
        $files = (array) $this->filesystem->files($path);
95 2
        foreach ($files as $file) {
96 1
            if ($this->filesystem->isFile($file) === true &&
97 1
                $this->filesystem->lastModified($file) < ($time - $maxFileAge)
98 1
            ) {
99 1
                $this->filesystem->delete($file);
100 1
            }
101 2
        }
102 2
    }
103
104
    /**
105
     * receive.
106
     *
107
     * @param string $inputName
108
     * @return \Symfony\Component\HttpFoundation\File\UploadedFile
109
     *
110
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
111
     */
112
    abstract public function receive($inputName);
113
114
    /**
115
     * deleteUploadedFile.
116
     *
117
     * @param \Symfony\Component\HttpFoundation\File\UploadedFile
118
     * @return $this
119
     */
120 1
    public function deleteUploadedFile(UploadedFile $uploadedFile)
121
    {
122 1
        $file = $uploadedFile->getPathname();
123 1
        if ($this->filesystem->isFile($file) === true) {
124 1
            $this->filesystem->delete($file);
125 1
        }
126 1
        $this->cleanDirectory($this->config['chunks']);
127
128 1
        return $this;
129
    }
130
131
    /**
132
     * completedResponse.
133
     *
134
     * @param \Illuminate\Http\JsonResponse $response
135
     * @return \Illuminate\Http\JsonResponse
136
     */
137 1
    public function completedResponse(JsonResponse $response)
138
    {
139 1
        return $response;
140
    }
141
142
    /**
143
     * chunkPath.
144
     *
145
     * @return string
146
     */
147 5
    protected function chunkPath()
148
    {
149 5
        $this->makeDirectory($this->config['chunks']);
150
151 5
        return rtrim($this->config['chunks'], '/').'/';
152
    }
153
154
    /**
155
     * storagePath.
156
     *
157
     * @return string
158
     */
159 5
    protected function storagePath()
160
    {
161 5
        $this->makeDirectory($this->config['storage']);
162
163 5
        return rtrim($this->config['storage'], '/').'/';
164
    }
165
}
166