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