Api   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 183
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
receive() 0 1 ?
A __construct() 0 12 4
A domain() 0 4 1
A path() 0 4 1
A makeDirectory() 0 8 2
A cleanDirectory() 0 15 4
A deleteUploadedFile() 0 10 2
A completedResponse() 0 4 1
A chunkPath() 0 6 1
A storagePath() 0 6 1
A createChunkFile() 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
    protected $request;
18
19
    /**
20
     * $files.
21
     *
22
     * @var \Recca0120\Upload\Filesystem
23
     */
24
    protected $files;
25
26
    /**
27
     * $chunkFile.
28
     *
29
     * @var \Recca0120\Upload\ChunkFileFactory
30
     */
31
    protected $ChunkFileFactory;
32
33
    /**
34
     * $config.
35
     *
36
     * @var array
37
     */
38
    protected $config;
39
40
    /**
41
     * __construct.
42
     *
43
     * @param array $config
44
     * @param \Illuminate\Http\Request $request
45
     * @param \Recca0120\Upload\Filesystem $files
46
     * @param \Recca0120\Upload\ChunkFileFactory $chunkFileFactory
47
     */
48 24
    public function __construct($config = [], Request $request = null, Filesystem $files = null, ChunkFileFactory $chunkFileFactory = null)
49
    {
50 24
        $this->request = $request ?: Request::capture();
51 24
        $this->files = $files ?: new Filesystem();
52 24
        $this->chunkFileFactory = $chunkFileFactory ?: new ChunkFileFactory($this->files);
0 ignored issues
show
Bug introduced by
The property chunkFileFactory does not seem to exist. Did you mean ChunkFileFactory?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53 24
        $this->config = array_merge([
54 24
            'chunks' => sys_get_temp_dir().'/chunks',
55 24
            'storage' => 'storage/temp',
56 24
            'domain' => $this->request->root(),
57 24
            'path' => 'storage/temp',
58 24
        ], $config);
59 24
    }
60
61
    /**
62
     * domain.
63
     *
64
     * @return string
65
     */
66 1
    public function domain()
67
    {
68 1
        return rtrim($this->config['domain'], '/').'/';
69
    }
70
71
    /**
72
     * path.
73
     *
74
     * @return string
75
     */
76 1
    public function path()
77
    {
78 1
        return rtrim($this->config['path'], '/').'/';
79
    }
80
81
    /**
82
     * makeDirectory.
83
     *
84
     * @return $this
85
     */
86 11
    public function makeDirectory($path)
87
    {
88 11
        if ($this->files->isDirectory($path) === false) {
89 1
            $this->files->makeDirectory($path, 0777, true, true);
90
        }
91
92 11
        return $this;
93
    }
94
95
    /**
96
     * cleanDirectory.
97
     *
98
     * @param string $path
99
     * @return $this
100
     */
101 2
    public function cleanDirectory($path)
102
    {
103 2
        $time = time();
104 2
        $maxFileAge = 3600;
105 2
        $files = (array) $this->files->files($path);
106 2
        foreach ($files as $file) {
107 1
            if ($this->files->isFile($file) === true &&
108 1
                $this->files->lastModified($file) < ($time - $maxFileAge)
109
            ) {
110 1
                $this->files->delete($file);
111
            }
112
        }
113
114 2
        return $this;
115
    }
116
117
    /**
118
     * receive.
119
     *
120
     * @param string $inputName
121
     * @return \Symfony\Component\HttpFoundation\File\UploadedFile
122
     *
123
     * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
124
     */
125
    abstract public function receive($inputName);
126
127
    /**
128
     * deleteUploadedFile.
129
     *
130
     * @param \Symfony\Component\HttpFoundation\File\UploadedFile
131
     * @return $this
132
     */
133 1
    public function deleteUploadedFile(UploadedFile $uploadedFile)
134
    {
135 1
        $file = $uploadedFile->getPathname();
136 1
        if ($this->files->isFile($file) === true) {
137 1
            $this->files->delete($file);
138
        }
139 1
        $this->cleanDirectory($this->chunkPath());
140
141 1
        return $this;
142
    }
143
144
    /**
145
     * completedResponse.
146
     *
147
     * @param \Illuminate\Http\JsonResponse $response
148
     * @return \Illuminate\Http\JsonResponse
149
     */
150 1
    public function completedResponse(JsonResponse $response)
151
    {
152 1
        return $response;
153
    }
154
155
    /**
156
     * chunkPath.
157
     *
158
     * @return string
159
     */
160 10
    protected function chunkPath()
161
    {
162 10
        $this->makeDirectory($this->config['chunks']);
163
164 10
        return rtrim($this->config['chunks'], '/').'/';
165
    }
166
167
    /**
168
     * storagePath.
169
     *
170
     * @return string
171
     */
172 9
    protected function storagePath()
173
    {
174 9
        $this->makeDirectory($this->config['storage']);
175
176 9
        return rtrim($this->config['storage'], '/').'/';
177
    }
178
179
    /**
180
     * createChunkFile.
181
     *
182
     * @param string $name
183
     * @param string $uuid
184
     * @return \Recca0120\Upload\ChunkFile
185
     */
186 9
    protected function createChunkFile($name, $uuid = null, $mimeType = null)
187
    {
188 9
        return $this->chunkFileFactory->create(
0 ignored issues
show
Bug introduced by
The property chunkFileFactory does not seem to exist. Did you mean ChunkFileFactory?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
189 9
            $name, $this->chunkPath(), $this->storagePath(), $uuid, $mimeType
190
        );
191
    }
192
}
193