Total Complexity | 54 |
Total Lines | 313 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 5 | Features | 3 |
Complex classes like LfmPath often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LfmPath, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | ======= |
||
15 | use UniSharp\LaravelFilemanager\LfmUploadValidator; |
||
16 | >>>>>>> 98f57e5f2eb5af652a32266cd9b9f7f047a77d62 |
||
17 | |||
18 | class LfmPath |
||
19 | { |
||
20 | private $working_dir; |
||
21 | private $item_name; |
||
22 | private $is_thumb = false; |
||
23 | |||
24 | private $helper; |
||
25 | |||
26 | public function __construct(Lfm $lfm = null) |
||
27 | { |
||
28 | $this->helper = $lfm; |
||
29 | } |
||
30 | |||
31 | public function __get($var_name) |
||
32 | { |
||
33 | if ($var_name == 'storage') { |
||
34 | return $this->helper->getStorage($this->path('url')); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | public function __call($function_name, $arguments) |
||
39 | { |
||
40 | return $this->storage->$function_name(...$arguments); |
||
41 | } |
||
42 | |||
43 | public function dir($working_dir) |
||
44 | { |
||
45 | $this->working_dir = $working_dir; |
||
46 | |||
47 | return $this; |
||
48 | } |
||
49 | |||
50 | public function thumb($is_thumb = true) |
||
51 | { |
||
52 | $this->is_thumb = $is_thumb; |
||
53 | |||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | public function setName($item_name) |
||
58 | { |
||
59 | $this->item_name = $item_name; |
||
60 | |||
61 | return $this; |
||
62 | } |
||
63 | |||
64 | public function getName() |
||
65 | { |
||
66 | return $this->item_name; |
||
67 | } |
||
68 | |||
69 | public function path($type = 'storage') |
||
70 | { |
||
71 | if ($type == 'working_dir') { |
||
72 | // working directory: /{user_slug} |
||
73 | return $this->translateToLfmPath($this->normalizeWorkingDir()); |
||
74 | } elseif ($type == 'url') { |
||
75 | // storage: files/{user_slug} |
||
76 | // storage without folder: {user_slug} |
||
77 | return $this->helper->getCategoryName() === '.' |
||
78 | ? ltrim($this->path('working_dir'), '/') |
||
79 | : $this->helper->getCategoryName() . $this->path('working_dir'); |
||
80 | } elseif ($type == 'storage') { |
||
81 | // storage: files/{user_slug} |
||
82 | // storage on windows: files\{user_slug} |
||
83 | return str_replace(Lfm::DS, $this->helper->ds(), $this->path('url')); |
||
84 | } else { |
||
85 | // absolute: /var/www/html/project/storage/app/files/{user_slug} |
||
86 | // absolute on windows: C:\project\storage\app\files\{user_slug} |
||
87 | return $this->storage->rootPath() . $this->path('storage'); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function translateToLfmPath($path) |
||
92 | { |
||
93 | return str_replace($this->helper->ds(), Lfm::DS, $path); |
||
94 | } |
||
95 | |||
96 | public function url() |
||
97 | { |
||
98 | return $this->storage->url($this->path('url')); |
||
99 | } |
||
100 | |||
101 | public function folders() |
||
102 | { |
||
103 | $all_folders = array_map(function ($directory_path) { |
||
104 | return $this->pretty($directory_path, true); |
||
105 | }, $this->storage->directories()); |
||
106 | |||
107 | $folders = array_filter($all_folders, function ($directory) { |
||
108 | return $directory->name !== $this->helper->getThumbFolderName(); |
||
109 | }); |
||
110 | |||
111 | return $this->sortByColumn($folders); |
||
112 | } |
||
113 | |||
114 | public function files() |
||
115 | { |
||
116 | $files = array_map(function ($file_path) { |
||
117 | return $this->pretty($file_path); |
||
118 | }, $this->storage->files()); |
||
119 | |||
120 | return $this->sortByColumn($files); |
||
121 | } |
||
122 | |||
123 | public function pretty($item_path, $isDirectory = false) |
||
124 | { |
||
125 | return Container::getInstance()->makeWith(LfmItem::class, [ |
||
126 | 'lfm' => (clone $this)->setName($this->helper->getNameFromPath($item_path)), |
||
127 | 'helper' => $this->helper, |
||
128 | 'isDirectory' => $isDirectory |
||
129 | ]); |
||
130 | } |
||
131 | |||
132 | public function delete() |
||
133 | { |
||
134 | if ($this->isDirectory()) { |
||
135 | return $this->storage->deleteDirectory(); |
||
136 | } else { |
||
137 | return $this->storage->delete(); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Create folder if not exist. |
||
143 | * |
||
144 | * @param string $path Real path of a directory. |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function createFolder() |
||
148 | { |
||
149 | if ($this->storage->exists($this)) { |
||
150 | return false; |
||
151 | } |
||
152 | |||
153 | $this->storage->makeDirectory(0777, true, true); |
||
154 | } |
||
155 | |||
156 | public function isDirectory() |
||
157 | { |
||
158 | $working_dir = $this->path('working_dir'); |
||
159 | $parent_dir = substr($working_dir, 0, strrpos($working_dir, '/')); |
||
160 | |||
161 | $parent_directories = array_map(function ($directory_path) { |
||
162 | return app(static::class)->translateToLfmPath($directory_path); |
||
163 | }, app(static::class)->dir($parent_dir)->directories()); |
||
164 | |||
165 | return in_array($this->path('url'), $parent_directories); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Check a folder and its subfolders is empty or not. |
||
170 | * |
||
171 | * @param string $directory_path Real path of a directory. |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function directoryIsEmpty() |
||
175 | { |
||
176 | return count($this->storage->allFiles()) == 0; |
||
177 | } |
||
178 | |||
179 | public function normalizeWorkingDir() |
||
180 | { |
||
181 | $path = $this->working_dir |
||
182 | ?: $this->helper->input('working_dir') |
||
183 | ?: $this->helper->getRootFolder(); |
||
184 | |||
185 | if ($this->is_thumb) { |
||
186 | // Prevent if working dir is "/" normalizeWorkingDir will add double "//" that breaks S3 functionality |
||
187 | $path = rtrim($path, Lfm::DS) . Lfm::DS . $this->helper->getThumbFolderName(); |
||
188 | } |
||
189 | |||
190 | if ($this->getName()) { |
||
191 | // Prevent if working dir is "/" normalizeWorkingDir will add double "//" that breaks S3 functionality |
||
192 | $path = rtrim($path, Lfm::DS) . Lfm::DS . $this->getName(); |
||
193 | } |
||
194 | |||
195 | return $path; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Sort files and directories. |
||
200 | * |
||
201 | * @param mixed $arr_items Array of files or folders or both. |
||
202 | * @return array of object |
||
203 | */ |
||
204 | public function sortByColumn($arr_items) |
||
205 | { |
||
206 | $sort_by = $this->helper->input('sort_type'); |
||
207 | if (in_array($sort_by, ['name', 'time'])) { |
||
208 | $key_to_sort = $sort_by; |
||
209 | } else { |
||
210 | $key_to_sort = 'name'; |
||
211 | } |
||
212 | |||
213 | uasort($arr_items, function ($a, $b) use ($key_to_sort) { |
||
214 | return strcasecmp($a->{$key_to_sort}, $b->{$key_to_sort}); |
||
215 | }); |
||
216 | |||
217 | return $arr_items; |
||
218 | } |
||
219 | |||
220 | public function error($error_type, $variables = []) |
||
221 | { |
||
222 | throw new \Exception($this->helper->error($error_type, $variables)); |
||
223 | } |
||
224 | |||
225 | // Upload section |
||
226 | public function upload($file) |
||
227 | { |
||
228 | $new_file_name = $this->getNewName($file); |
||
229 | $new_file_path = $this->setName($new_file_name)->path('absolute'); |
||
230 | |||
231 | event(new FileIsUploading($new_file_path)); |
||
232 | event(new ImageIsUploading($new_file_path)); |
||
233 | try { |
||
234 | $this->setName($new_file_name)->storage->save($file); |
||
235 | |||
236 | $this->generateThumbnail($new_file_name); |
||
237 | } catch (\Exception $e) { |
||
238 | \Log::info($e); |
||
239 | return $this->error('invalid'); |
||
240 | } |
||
241 | // TODO should be "FileWasUploaded" |
||
242 | event(new FileWasUploaded($new_file_path)); |
||
243 | event(new ImageWasUploaded($new_file_path)); |
||
244 | |||
245 | return $new_file_name; |
||
246 | } |
||
247 | |||
248 | public function validateUploadedFile($file) |
||
249 | { |
||
250 | $validator = new LfmUploadValidator($file); |
||
251 | |||
252 | $validator->sizeLowerThanIniMaximum(); |
||
253 | |||
254 | $validator->uploadWasSuccessful(); |
||
255 | |||
256 | if (!config('lfm.over_write_on_duplicate')) { |
||
257 | $validator->nameIsNotDuplicate($this->getNewName($file), $this); |
||
258 | } |
||
259 | |||
260 | $validator->isNotExcutable(); |
||
261 | |||
262 | if (config('lfm.should_validate_mime', false)) { |
||
263 | $validator->mimeTypeIsValid($this->helper->availableMimeTypes()); |
||
264 | } |
||
265 | |||
266 | if (config('lfm.should_validate_size', false)) { |
||
267 | $validator->sizeIsLowerThanConfiguredMaximum($this->helper->maxUploadSize()); |
||
268 | } |
||
269 | |||
270 | return true; |
||
271 | } |
||
272 | |||
273 | private function getNewName($file) |
||
274 | { |
||
275 | $new_file_name = $this->helper->translateFromUtf8( |
||
276 | trim($this->helper->utf8Pathinfo($file->getClientOriginalName(), "filename")) |
||
277 | ); |
||
278 | |||
279 | $extension = $file->getClientOriginalExtension(); |
||
280 | |||
281 | if (config('lfm.rename_file') === true) { |
||
282 | $new_file_name = uniqid(); |
||
283 | } elseif (config('lfm.alphanumeric_filename') === true) { |
||
284 | $new_file_name = preg_replace('/[^A-Za-z0-9\-\']/', '_', $new_file_name); |
||
285 | } elseif (config('lfm.slug_filename') === true){ |
||
286 | $new_file_name = Str::slug($new_file_name, '_'); |
||
287 | } |
||
288 | |||
289 | if ($extension) { |
||
290 | $new_file_name_with_extention = $new_file_name . '.' . $extension; |
||
291 | } |
||
292 | |||
293 | if (config('lfm.rename_duplicates') === true) { |
||
294 | $counter = 1; |
||
295 | $file_name_without_extentions = $new_file_name; |
||
296 | while ($this->setName(($extension) ? $new_file_name_with_extention : $new_file_name)->exists()) { |
||
297 | if (config('lfm.alphanumeric_filename') === true) { |
||
298 | $suffix = '_'.$counter; |
||
299 | } else { |
||
300 | $suffix = " ({$counter})"; |
||
301 | } |
||
302 | $new_file_name = $file_name_without_extentions.$suffix; |
||
303 | |||
304 | if ($extension) { |
||
305 | $new_file_name_with_extention = $new_file_name . '.' . $extension; |
||
306 | } |
||
307 | $counter++; |
||
308 | } |
||
309 | } |
||
310 | |||
311 | return ($extension) ? $new_file_name_with_extention : $new_file_name; |
||
312 | } |
||
313 | |||
314 | public function generateThumbnail($file_name) |
||
315 | { |
||
316 | $original_image = $this->pretty($file_name); |
||
317 | |||
318 | if (!$original_image->shouldCreateThumb()) { |
||
319 | return; |
||
320 | } |
||
321 | |||
322 | // create folder for thumbnails |
||
323 | $this->setName(null)->thumb(true)->createFolder(); |
||
324 | |||
325 | // generate cropped image content |
||
326 | $this->setName($file_name)->thumb(true); |
||
327 | $thumbWidth = $this->helper->shouldCreateCategoryThumb() && $this->helper->categoryThumbWidth() ? $this->helper->categoryThumbWidth() : config('lfm.thumb_img_width', 200); |
||
335 |