Completed
Push — master ( 370c5e...d20bc9 )
by Renato
06:13
created

StorageManager::uploadImage()   D

Complexity

Conditions 9
Paths 130

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 9
eloc 26
c 2
b 1
f 1
nc 130
nop 5
dl 0
loc 36
ccs 21
cts 21
cp 1
crap 9
rs 4.6666
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 21
    public function __construct(Storage $storage, ImagineFactory $imagineFactory = null)
33
    {
34 21
        $this->storage = $storage;
35 21
        $this->imagineFactory = $imagineFactory;
36 21
    }
37
38
    /**
39
     * File Exists
40
     *
41
     * @param string $filename Path File
42
     *
43
     * @return bool
44
     */
45 8
    public function exists($filename)
46
    {
47
        try {
48 8
            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 8
    public function mimeType($filename)
80
    {
81
        try {
82 8
            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 6
    public function isDir($path)
97
    {
98 6
        $mimeType = $this->mimeType($path);
99
100 6
        if ($this->exists($path) && (!$mimeType || $mimeType == 'directory')) {
101 3
            return true;
102
        }
103
104 3
        return false;
105
    }
106
107
    /**
108
     * Is File
109
     *
110
     * @param string $filename Path File
111
     *
112
     * @return bool
113
     */
114 2
    public function isFile($filename)
115
    {
116 2
        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
     * UploadFile
166
     *
167
     * @param UploadedFile $file     Uploaded File
168
     * @param string       $folder   String Folder
169
     * @param string       $name     String Name
170
     * @param bool         $override Boolean Over Ride
171
     *
172
     * @return bool
173
     */
174 2
    public function uploadFile(UploadedFile $file, $folder = null, $name = null, $override = false)
175
    {
176 2
        $data = $this->parseFile($file, $folder, $name, $override);
177
178 2
        $success = (bool) $this->storage->put($data['filename'], file_get_contents($file));
179
180 2
        if ($success) {
181 1
            return $data;
182
        }
183
184 1
        return false;
185
    }
186
187
    /**
188
     * Upload Image
189
     *
190
     * @param UploadedFile $file     Uploaded File
191
     * @param string       $folder   String Folder
192
     * @param string       $name     String Name
193
     * @param array        $options  Array Options
194
     * @param bool         $override Boolean Over Ride
195
     *
196
     * @return bool
197
     */
198 4
    public function uploadImage(
199
        UploadedFile $file,
200
        $folder = null,
201
        $name = null,
202
        array $options = array(),
203
        $override = false
204
    ) {
205 4
        $pathImage = $file->getPathname();
206 4
        $data = $this->parseFile($file, $folder, $name, $override);
207
208 4
        if ($this->imagineFactory) {
209 1
            $width = isset($options['width']) ? intval($options['width']) : 0;
210 1
            $height = isset($options['height']) ? intval($options['height']) : 0;
211 1
            $scale = isset($options['scale']) ? (bool) $options['scale'] : true;
212 1
            $opacity = isset($options['opacity']) ? (float) $options['opacity'] : null;
213 1
            $watermark = isset($options['watermark']) ? $options['watermark'] : null;
214 1
            $quality = isset($options['quality']) ? intval($options['quality']) : 85; // Quality Deufault: 85;
215
            
216 1
            $imagine = $this->imagineFactory->make($pathImage);
217 1
            $imagine->resize($width, $height, !$scale);
218 1
            $imagine->opacity($opacity);
219 1
            $imagine->watermark($watermark);
220
                
221 1
            $content = $imagine->encode($data['extension'], $quality);
222 1
        } else {
223 3
            $content = file_get_contents($file);
224
        }
225
226 4
        $success = $this->storage->put($data['filename'], $content);
0 ignored issues
show
Bug introduced by
It seems like $content defined by $imagine->encode($data['extension'], $quality) on line 221 can also be of type object<Intervention\Image\Image>; however, Illuminate\Contracts\Filesystem\Filesystem::put() does only seem to accept string|resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
227
228 4
        if ($success) {
229 3
            return $data;
230
        }
231
232 1
        return false;
233
    }
234
235
    /**
236
     * Parse Filename
237
     *
238
     * @param UploadedFile $file     Uploaded File
239
     * @param string       $name     String Name
240
     * @param string       $folder   String Folder
241
     * @param bool         $override Boolean Over Ride
242
     *
243
     * @return bool|array
244
     */
245 6
    protected function parseFile($file, $folder = null, $name = null, $override = false)
246
    {
247 6
        $folder = trim((string) $folder, '/');
248 6
        $folder = $folder ? "{$folder}/" : "";
249 6
        $this->storage->makeDirectory($folder);
250
251 6
        $name = $name ?: $file->getClientOriginalName();
252 6
        $nameOriginal = str_slug(pathinfo($name, PATHINFO_FILENAME));
253
254 6
        if (empty($nameOriginal)) {
255 1
            $nameOriginal = str_random(10);
256 1
        }
257 6
        $extension = $file->getClientOriginalExtension();
258 6
        $size = $file->getClientSize();
259 6
        $mime = $file->getClientMimeType();
260
261 6
        $sufix = '';
262 6
        $count = 1;
263
        do {
264 6
            $name = "{$nameOriginal}{$sufix}.{$extension}";
265 6
            $filename = "{$folder}{$name}";
266 6
            $sufix = "({$count})";
267 6
            $count++;
268
269 6
        } while (!$override && $this->storage->exists($filename));
270
271 6
        return compact('filename', 'name', 'extension', 'size', 'mime');
272
    }
273
}
274