Completed
Push — master ( 2f4806...4e3d73 )
by Renato
05:45
created

StorageManager   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 293
ccs 88
cts 88
cp 1
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 1
cbo 5

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A exists() 0 9 2
A size() 0 9 2
A mimeType() 0 9 2
A isDir() 0 10 4
A isFile() 0 4 1
A metaData() 0 9 2
A readFile() 0 4 1
A deleteFile() 0 8 2
A deleteFolder() 0 8 2
A files() 0 8 2
A uploadFile() 0 12 2
D uploadImage() 0 36 9
B parseFile() 0 28 6
1
<?php
2
3
namespace NwLaravel\FileStorage;
4
5
use \Exception;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
use Illuminate\Contracts\Filesystem\Filesystem as Storage;
8
use Intervention\Image\ImageManager;
9
use Intervention\Image\Image;
10
11
/**
12
 * Class StorageManager
13
 */
14
class StorageManager
15
{
16
    /**
17
     * @var Storage
18
     */
19
    protected $storage;
20
21
    /**
22
     * @var ImagineFactory
23
     */
24
    protected $imagineFactory;
25
26
    /**
27
     * Construct
28
     *
29
     * @param Storage        $storage
30
     * @param ImagineFactory $imagineFactory
31
     */
32 25
    public function __construct(Storage $storage, ImagineFactory $imagineFactory = null)
33
    {
34 25
        $this->storage = $storage;
35 25
        $this->imagineFactory = $imagineFactory;
36 25
    }
37
38
    /**
39
     * File Exists
40
     *
41
     * @param string $filename Path File
42
     *
43
     * @return bool
44
     */
45 12
    public function exists($filename)
46
    {
47
        try {
48 12
            return $this->storage->exists($filename);
49
50 1
        } catch (\Exception $e) {
51 1
            return false;
52
        }
53
    }
54
55
    /**
56
     * Get Size
57
     *
58
     * @param string $filename Path File
59
     *
60
     * @return bool
61
     */
62 2
    public function size($filename)
63
    {
64
        try {
65 2
            return intval($this->storage->size($filename));
66
67 1
        } catch (\Exception $e) {
68 1
            return 0;
69
        }
70
    }
71
72
    /**
73
     * Get MimeType File
74
     *
75
     * @param string $filename Path File
76
     *
77
     * @return bool
78
     */
79 12
    public function mimeType($filename)
80
    {
81
        try {
82 12
            return $this->storage->mimeType($filename);
83
84 1
        } catch (\Exception $e) {
85 1
            return null;
86
        }
87
    }
88
89
    /**
90
     * Path is Directory
91
     *
92
     * @param string $path Path Directory
93
     *
94
     * @return bool
95
     */
96 10
    public function isDir($path)
97
    {
98 10
        $mimeType = $this->mimeType($path);
99
100 10
        if ($this->exists($path) && (!$mimeType || $mimeType == 'directory')) {
101 5
            return true;
102
        }
103
104 5
        return false;
105
    }
106
107
    /**
108
     * Is File
109
     *
110
     * @param string $filename Path File
111
     *
112
     * @return bool
113
     */
114 6
    public function isFile($filename)
115
    {
116 6
        return !$this->isDir($filename);
117
    }
118
119
    /**
120
     * Get Meta Data
121
     *
122
     * @param string $filename Path File
123
     *
124
     * @return bool
125
     */
126 2
    public function metaData($filename)
127
    {
128
        try {
129 2
            return $this->storage->getMetadata($filename);
0 ignored issues
show
Bug introduced by
The method getMetadata() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
131 1
        } catch (\Exception $e) {
132 1
            return null;
133
        }
134
    }
135
136
    /**
137
     * Read Content File
138
     *
139
     * @param string $filename Path File
140
     *
141
     * @return bool
142
     */
143 1
    public function readFile($filename)
144
    {
145 1
        return $this->storage->get($filename);
146
    }
147
148
    /**
149
     * Delete File
150
     *
151
     * @param string $filename Path File
152
     *
153
     * @return bool
154
     */
155 2
    public function deleteFile($filename)
156
    {
157 2
        if ($this->isDir($filename)) {
158 1
            return false;
159
        }
160
161 1
        return $this->storage->delete($filename);
162
    }
163
164
    /**
165
     * Delete Folder
166
     *
167
     * @param string $folder Path Folder
0 ignored issues
show
Bug introduced by
There is no parameter named $folder. 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...
168
     *
169
     * @return bool
170
     */
171 2
    public function deleteFolder($path)
172
    {
173 2
        if ($this->isFile($path)) {
174 1
            return false;
175
        }
176
177 1
        return $this->storage->deleteDirectory($path);
178
    }
179
180
    /**
181
     * Files in Folder
182
     *
183
     * @param string $folder
0 ignored issues
show
Bug introduced by
There is no parameter named $folder. 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...
184
     * @param bool   $recursive
185
     *
186
     * @return array
187
     */
188 2
    public function files($path, $recursive = false)
189
    {
190 2
        if ($this->isFile($path)) {
191 1
            return null;
192
        }
193
194 1
        return $this->storage->files($path, (bool) $recursive);
195
    }
196
197
    /**
198
     * UploadFile
199
     *
200
     * @param UploadedFile $file     Uploaded File
201
     * @param string       $folder   String Folder
202
     * @param string       $name     String Name
203
     * @param bool         $override Boolean Over Ride
204
     *
205
     * @return bool
206
     */
207 2
    public function uploadFile(UploadedFile $file, $folder = null, $name = null, $override = false)
208
    {
209 2
        $data = $this->parseFile($file, $folder, $name, $override);
210
211 2
        $success = (bool) $this->storage->put($data['filename'], file_get_contents($file));
212
213 2
        if ($success) {
214 1
            return $data;
215
        }
216
217 1
        return false;
218
    }
219
220
    /**
221
     * Upload Image
222
     *
223
     * @param UploadedFile $file     Uploaded File
224
     * @param string       $folder   String Folder
225
     * @param string       $name     String Name
226
     * @param array        $options  Array Options
227
     * @param bool         $override Boolean Over Ride
228
     *
229
     * @return bool
230
     */
231 4
    public function uploadImage(
232
        UploadedFile $file,
233
        $folder = null,
234
        $name = null,
235
        array $options = array(),
236
        $override = false
237
    ) {
238 4
        $pathImage = $file->getPathname();
239 4
        $data = $this->parseFile($file, $folder, $name, $override);
240
241 4
        if ($this->imagineFactory) {
242 1
            $width = isset($options['width']) ? intval($options['width']) : 0;
243 1
            $height = isset($options['height']) ? intval($options['height']) : 0;
244 1
            $scale = isset($options['scale']) ? (bool) $options['scale'] : true;
245 1
            $opacity = isset($options['opacity']) ? (float) $options['opacity'] : null;
246 1
            $watermark = isset($options['watermark']) ? $options['watermark'] : null;
247 1
            $quality = isset($options['quality']) ? intval($options['quality']) : 85; // Quality Deufault: 85;
248
            
249 1
            $imagine = $this->imagineFactory->make($pathImage);
250 1
            $imagine->resize($width, $height, !$scale);
251 1
            $imagine->opacity($opacity);
252 1
            $imagine->watermark($watermark);
253
                
254 1
            $content = $imagine->encode($data['extension'], $quality)->getEncoded();
255 1
        } else {
256 3
            $content = file_get_contents($file);
257
        }
258
259 4
        $success = $this->storage->put($data['filename'], $content);
260
261 4
        if ($success) {
262 3
            return $data;
263
        }
264
265 1
        return false;
266
    }
267
268
    /**
269
     * Parse Filename
270
     *
271
     * @param UploadedFile $file     Uploaded File
272
     * @param string       $name     String Name
273
     * @param string       $folder   String Folder
274
     * @param bool         $override Boolean Over Ride
275
     *
276
     * @return bool|array
277
     */
278 6
    protected function parseFile($file, $folder = null, $name = null, $override = false)
279
    {
280 6
        $folder = trim((string) $folder, '/');
281 6
        $folder = $folder ? "{$folder}/" : "";
282 6
        $this->storage->makeDirectory($folder);
283
284 6
        $name = $name ?: $file->getClientOriginalName();
285 6
        $nameOriginal = str_slug(pathinfo($name, PATHINFO_FILENAME));
286
287 6
        if (empty($nameOriginal)) {
288 1
            $nameOriginal = str_random(10);
289 1
        }
290 6
        $extension = $file->getClientOriginalExtension();
291 6
        $size = $file->getClientSize();
292 6
        $mime = $file->getClientMimeType();
293
294 6
        $sufix = '';
295 6
        $count = 1;
296
        do {
297 6
            $name = "{$nameOriginal}{$sufix}.{$extension}";
298 6
            $filename = "{$folder}{$name}";
299 6
            $sufix = "({$count})";
300 6
            $count++;
301
302 6
        } while (!$override && $this->storage->exists($filename));
303
304 6
        return compact('filename', 'name', 'extension', 'size', 'mime');
305
    }
306
}
307