Total Complexity | 53 |
Total Lines | 308 |
Duplicated Lines | 0 % |
Changes | 13 | ||
Bugs | 5 | Features | 2 |
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 |
||
12 | class LfmPath |
||
13 | { |
||
14 | private $working_dir; |
||
15 | private $item_name; |
||
16 | private $is_thumb = false; |
||
17 | |||
18 | private $helper; |
||
19 | |||
20 | public function __construct(Lfm $lfm = null) |
||
21 | { |
||
22 | $this->helper = $lfm; |
||
23 | } |
||
24 | |||
25 | public function __get($var_name) |
||
26 | { |
||
27 | if ($var_name == 'storage') { |
||
28 | return $this->helper->getStorage($this->path('url')); |
||
|
|||
29 | } |
||
30 | } |
||
31 | |||
32 | public function __call($function_name, $arguments) |
||
33 | { |
||
34 | return $this->storage->$function_name(...$arguments); |
||
35 | } |
||
36 | |||
37 | public function dir($working_dir) |
||
42 | } |
||
43 | |||
44 | public function thumb($is_thumb = true) |
||
45 | { |
||
46 | $this->is_thumb = $is_thumb; |
||
47 | |||
48 | return $this; |
||
49 | } |
||
50 | |||
51 | public function setName($item_name) |
||
52 | { |
||
53 | $this->item_name = $item_name; |
||
54 | |||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | public function getName() |
||
59 | { |
||
60 | return $this->item_name; |
||
61 | } |
||
62 | |||
63 | public function path($type = 'storage') |
||
64 | { |
||
65 | if ($type == 'working_dir') { |
||
66 | // working directory: /{user_slug} |
||
67 | return $this->translateToLfmPath($this->normalizeWorkingDir()); |
||
68 | } elseif ($type == 'url') { |
||
69 | // storage: files/{user_slug} |
||
70 | return $this->helper->getCategoryName() . $this->path('working_dir'); |
||
71 | } elseif ($type == 'storage') { |
||
72 | // storage: files/{user_slug} |
||
73 | // storage on windows: files\{user_slug} |
||
74 | return str_replace(Lfm::DS, $this->helper->ds(), $this->path('url')); |
||
75 | } else { |
||
76 | // absolute: /var/www/html/project/storage/app/files/{user_slug} |
||
77 | // absolute on windows: C:\project\storage\app\files\{user_slug} |
||
78 | return $this->storage->rootPath() . $this->path('storage'); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | public function translateToLfmPath($path) |
||
83 | { |
||
84 | return str_replace($this->helper->ds(), Lfm::DS, $path); |
||
85 | } |
||
86 | |||
87 | public function url() |
||
88 | { |
||
89 | return $this->storage->url($this->path('url')); |
||
90 | } |
||
91 | |||
92 | public function folders() |
||
93 | { |
||
94 | $all_folders = array_map(function ($directory_path) { |
||
95 | return $this->pretty($directory_path, true); |
||
96 | }, $this->storage->directories()); |
||
97 | |||
98 | $folders = array_filter($all_folders, function ($directory) { |
||
99 | return $directory->name !== $this->helper->getThumbFolderName(); |
||
100 | }); |
||
101 | |||
102 | return $this->sortByColumn($folders); |
||
103 | } |
||
104 | |||
105 | public function files() |
||
112 | } |
||
113 | |||
114 | public function pretty($item_path, $isDirectory = false) |
||
115 | { |
||
116 | return Container::getInstance()->makeWith(LfmItem::class, [ |
||
117 | 'lfm' => (clone $this)->setName($this->helper->getNameFromPath($item_path)), |
||
118 | 'helper' => $this->helper, |
||
119 | 'isDirectory' => $isDirectory |
||
120 | ]); |
||
121 | } |
||
122 | |||
123 | public function delete() |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Create folder if not exist. |
||
134 | * |
||
135 | * @param string $path Real path of a directory. |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function createFolder() |
||
139 | { |
||
140 | if ($this->storage->exists($this)) { |
||
141 | return false; |
||
142 | } |
||
143 | |||
144 | $this->storage->makeDirectory(0777, true, true); |
||
145 | } |
||
146 | |||
147 | public function isDirectory() |
||
148 | { |
||
149 | $working_dir = $this->path('working_dir'); |
||
150 | $parent_dir = substr($working_dir, 0, strrpos($working_dir, '/')); |
||
151 | |||
152 | $parent_directories = array_map(function ($directory_path) { |
||
153 | return app(static::class)->translateToLfmPath($directory_path); |
||
154 | }, app(static::class)->dir($parent_dir)->directories()); |
||
155 | |||
156 | return in_array($this->path('url'), $parent_directories); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Check a folder and its subfolders is empty or not. |
||
161 | * |
||
162 | * @param string $directory_path Real path of a directory. |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function directoryIsEmpty() |
||
168 | } |
||
169 | |||
170 | public function normalizeWorkingDir() |
||
171 | { |
||
172 | $path = $this->working_dir |
||
173 | ?: $this->helper->input('working_dir') |
||
174 | ?: $this->helper->getRootFolder(); |
||
175 | |||
176 | if ($this->is_thumb) { |
||
177 | // Prevent if working dir is "/" normalizeWorkingDir will add double "//" that breaks S3 functionality |
||
178 | $path = rtrim($path, Lfm::DS) . Lfm::DS . $this->helper->getThumbFolderName(); |
||
179 | } |
||
180 | |||
181 | if ($this->getName()) { |
||
182 | // Prevent if working dir is "/" normalizeWorkingDir will add double "//" that breaks S3 functionality |
||
183 | $path = rtrim($path, Lfm::DS) . Lfm::DS . $this->getName(); |
||
184 | } |
||
185 | |||
186 | return $path; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Sort files and directories. |
||
191 | * |
||
192 | * @param mixed $arr_items Array of files or folders or both. |
||
193 | * @return array of object |
||
194 | */ |
||
195 | public function sortByColumn($arr_items) |
||
196 | { |
||
197 | $sort_by = $this->helper->input('sort_type'); |
||
198 | if (in_array($sort_by, ['name', 'time'])) { |
||
199 | $key_to_sort = $sort_by; |
||
200 | } else { |
||
201 | $key_to_sort = 'name'; |
||
202 | } |
||
203 | |||
204 | uasort($arr_items, function ($a, $b) use ($key_to_sort) { |
||
205 | return strcasecmp($a->{$key_to_sort}, $b->{$key_to_sort}); |
||
206 | }); |
||
207 | |||
208 | return $arr_items; |
||
209 | } |
||
210 | |||
211 | public function error($error_type, $variables = []) |
||
214 | } |
||
215 | |||
216 | // Upload section |
||
217 | public function upload($file) |
||
218 | { |
||
219 | $new_file_name = $this->getNewName($file); |
||
220 | $new_file_path = $this->setName($new_file_name)->path('absolute'); |
||
221 | |||
222 | event(new ImageIsUploading($new_file_path)); |
||
223 | try { |
||
224 | $this->setName($new_file_name)->storage->save($file); |
||
225 | |||
226 | $this->generateThumbnail($new_file_name); |
||
227 | } catch (\Exception $e) { |
||
228 | \Log::info($e); |
||
235 | } |
||
236 | |||
237 | public function validateUploadedFile($file) |
||
260 | } |
||
261 | |||
262 | private function getNewName($file) |
||
299 | } |
||
300 | |||
301 | public function generateThumbnail($file_name) |
||
322 |
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.