Completed
Push — master ( 92de9c...ca5a09 )
by Renato
04:31
created

StorageManager::cropImage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 6
dl 0
loc 23
ccs 0
cts 20
cp 0
crap 12
rs 9.552
c 0
b 0
f 0
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\Image;
9
10
/**
11
 * Class StorageManager
12
 */
13
class StorageManager
14
{
15
    /**
16
     * @var Storage
17
     */
18
    protected $storage;
19
20
    /**
21
     * @var ImagineFactory
22
     */
23
    protected $imagineFactory;
24
25
    /**
26
     * Construct
27
     *
28
     * @param Storage        $storage
29
     * @param ImagineFactory $imagineFactory
30
     */
31
    public function __construct(Storage $storage, ImagineFactory $imagineFactory = null)
32
    {
33
        $this->storage = $storage;
34
        $this->imagineFactory = $imagineFactory;
35
    }
36
37
    /**
38
     * File Exists
39
     *
40
     * @param string $filename Path File
41
     *
42
     * @return bool
43
     */
44
    public function exists($filename)
45
    {
46
        try {
47
            return $this->storage->exists($filename);
48
49
        } catch (\Exception $e) {
50
            return false;
51
        }
52
    }
53
54
    /**
55
     * Get Size
56
     *
57
     * @param string $filename Path File
58
     *
59
     * @return bool
60
     */
61
    public function size($filename)
62
    {
63
        try {
64
            return intval($this->storage->size($filename));
65
66
        } catch (\Exception $e) {
67
            return 0;
68
        }
69
    }
70
71
    /**
72
     * Get MimeType File
73
     *
74
     * @param string $filename Path File
75
     *
76
     * @return bool
77
     */
78
    public function mimeType($filename)
79
    {
80
        try {
81
            return $this->storage->mimeType($filename);
82
83
        } catch (\Exception $e) {
84
            return null;
85
        }
86
    }
87
88
    /**
89
     * Path is Directory
90
     *
91
     * @param string $path Path Directory
92
     *
93
     * @return bool
94
     */
95
    public function isDir($path)
96
    {
97
        $mimeType = $this->mimeType($path);
98
99
        if ($this->exists($path) && (!$mimeType || $mimeType == 'directory')) {
100
            return true;
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * Is File
108
     *
109
     * @param string $filename Path File
110
     *
111
     * @return bool
112
     */
113
    public function isFile($filename)
114
    {
115
        return !$this->isDir($filename);
116
    }
117
118
    /**
119
     * Get Meta Data
120
     *
121
     * @param string $filename Path File
122
     *
123
     * @return bool
124
     */
125
    public function metaData($filename)
126
    {
127
        try {
128
            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...
129
130
        } catch (\Exception $e) {
131
            return null;
132
        }
133
    }
134
135
    /**
136
     * Read Content File
137
     *
138
     * @param string $filename Path File
139
     *
140
     * @return string
141
     */
142
    public function readFile($filename)
143
    {
144
        return $this->storage->get($filename);
145
    }
146
147
    /**
148
     * Delete File
149
     *
150
     * @param string $filename Path File
151
     *
152
     * @return bool
153
     */
154
    public function deleteFile($filename)
155
    {
156
        if ($this->isDir($filename)) {
157
            return false;
158
        }
159
160
        return $this->storage->delete($filename);
161
    }
162
163
    /**
164
     * Delete Folder
165
     *
166
     * @param string $folder
167
     *
168
     * @return bool
169
     */
170
    public function deleteFolder($folder)
171
    {
172
        if ($this->isFile($folder)) {
173
            return false;
174
        }
175
176
        return $this->storage->deleteDirectory($folder);
177
    }
178
179
    /**
180
     * Files in Folder
181
     *
182
     * @param string $path
183
     * @param bool   $recursive
184
     *
185
     * @return array
186
     */
187
    public function files($path, $recursive = false)
188
    {
189
        if ($this->isFile($path)) {
190
            return null;
191
        }
192
193
        return $this->storage->files($path, (bool) $recursive);
194
    }
195
196
    /**
197
     * UploadFile
198
     *
199
     * @param UploadedFile $file     Uploaded File
200
     * @param string       $folder   String Folder
201
     * @param string       $name     String Name
202
     * @param bool         $override Boolean Over Ride
203
     * @param array        $config   Array Config Upload
204
     *
205
     * @return bool
206
     */
207
    public function uploadFile(UploadedFile $file, $folder = null, $name = null, $override = false, array $config = [])
208
    {
209
        $data = $this->parseFile($file, $folder, $name, $override);
210
211
        $success = (bool) $this->storage->put($data['filename'], file_get_contents($file), $config);
0 ignored issues
show
Documentation introduced by
$config is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
212
213
        if ($success) {
214
            return $data;
215
        }
216
217
        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
     * @param array        $config  Array Config Upload
229
     *
230
     * @return bool
231
     */
232
    public function uploadImage(
233
        UploadedFile $file,
234
        $folder = null,
235
        $name = null,
236
        array $options = [],
237
        $override = false,
238
        array $config = []
239
    ) {
240
        $pathImage = $file->getPathname();
241
        $data = $this->parseFile($file, $folder, $name, $override);
242
243
        if ($this->imagineFactory) {
244
            $width = isset($options['width']) ? intval($options['width']) : 0;
245
            $height = isset($options['height']) ? intval($options['height']) : 0;
246
            $scale = isset($options['scale']) ? (bool) $options['scale'] : true;
247
            $opacity = isset($options['opacity']) ? (float) $options['opacity'] : null;
248
            $watermark = isset($options['watermark']) ? $options['watermark'] : null;
249
            $quality = isset($options['quality']) ? intval($options['quality']) : 85; // Quality Deufault: 85;
250
251
            $imagine = $this->imagineFactory->make($pathImage);
252
            $imagine->stripProfiles();
253
            $imagine->resize($width, $height, !$scale);
254
            $imagine->opacity($opacity);
255
            $imagine->watermark($watermark);
256
            $imagine = $imagine->save($pathImage.'.'.$data['extension'], $quality);
257
            $data['size'] = $imagine->filesize();
258
            $content = $imagine->encode();
259
        } else {
260
            $content = file_get_contents($file);
261
        }
262
263
        $success = $this->storage->put($data['filename'], $content, $config);
0 ignored issues
show
Documentation introduced by
$config is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
264
265
        if ($success) {
266
            return $data;
267
        }
268
269
        return false;
270
    }
271
272
    /**
273
     * Parse Filename
274
     *
275
     * @param UploadedFile $file     Uploaded File
276
     * @param string       $name     String Name
277
     * @param string       $folder   String Folder
278
     * @param bool         $override Boolean Over Ride
279
     *
280
     * @return bool|array
281
     */
282
    protected function parseFile($file, $folder = null, $name = null, $override = false)
283
    {
284
        $folder = trim((string) $folder, '/');
285
        $folder = $folder ? "{$folder}/" : "";
286
        $this->storage->makeDirectory($folder);
287
288
        $name = $name ?: $file->getClientOriginalName();
289
        $nameOriginal = str_slug(pathinfo($name, PATHINFO_FILENAME));
290
291
        if (empty($nameOriginal)) {
292
            $nameOriginal = str_random(10);
293
        }
294
        $extension = $file->getClientOriginalExtension();
295
        $size = $file->getClientSize();
296
        $mime = $file->getClientMimeType();
297
298
        $sufix = '';
299
        $count = 1;
300
        do {
301
            $name = "{$nameOriginal}{$sufix}.{$extension}";
302
            $filename = "{$folder}{$name}";
303
            $sufix = "({$count})";
304
            $count++;
305
306
        } while (!$override && $this->storage->exists($filename));
307
308
        return compact('filename', 'name', 'extension', 'size', 'mime');
309
    }
310
311
    /**
312
     * Crop Image
313
     *
314
     * @param string $filename
315
     * @param int    $width
316
     * @param int    $height
317
     * @param int    $x
318
     * @param int    $y
319
     *
320
     * @return bool
321
     */
322
    public function cropImage(
323
        $filename,
324
        $width,
325
        $height,
326
        $x,
327
        $y,
328
        $target = null
329
    ) {
330
        if (!$this->imagineFactory) {
331
            return false;
332
        }
333
334
        $image = $this->storage->get($filename);
335
        $imagine = $this->imagineFactory->make($image);
336
        $imagine->crop($width, $height, $x, $y);
337
        $content = $imagine->encode();
338
339
        if (!$target) {
340
            $target = $filename;
341
        }
342
343
        return $this->storage->put($target, $content);
344
    }
345
346
    /**
347
     * Dynamically handle calls into the query instance.
348
     *
349
     * @param  string  $method
350
     * @param  array  $parameters
351
     * @return mixed
352
     */
353
    public function __call($method, $parameters)
354
    {
355
        return $this->storage->{$method}(...$parameters);
356
    }
357
}
358