Total Complexity | 40 |
Total Lines | 343 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 4 | Features | 1 |
Complex classes like Lfm 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 Lfm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Lfm |
||
13 | { |
||
14 | const PACKAGE_NAME = 'laravel-filemanager'; |
||
15 | const DS = '/'; |
||
16 | |||
17 | protected $config; |
||
18 | protected $request; |
||
19 | |||
20 | public function __construct(Config $config = null, Request $request = null) |
||
21 | { |
||
22 | $this->config = $config; |
||
23 | $this->request = $request; |
||
24 | } |
||
25 | |||
26 | public function getStorage($storage_path) |
||
29 | } |
||
30 | |||
31 | public function input($key) |
||
32 | { |
||
33 | return $this->translateFromUtf8($this->request->input($key)); |
||
|
|||
34 | } |
||
35 | |||
36 | public function config($key) |
||
37 | { |
||
38 | return $this->config->get('lfm.' . $key); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get only the file name. |
||
43 | * |
||
44 | * @param string $path Real path of a file. |
||
45 | * @return string |
||
46 | */ |
||
47 | public function getNameFromPath($path) |
||
50 | } |
||
51 | |||
52 | public function utf8Pathinfo($path, $part_name) |
||
53 | { |
||
54 | // XXX: all locale work-around for issue: utf8 file name got emptified |
||
55 | // if there's no '/', we're probably dealing with just a filename |
||
56 | // so just put an 'a' in front of it |
||
57 | if (strpos($path, '/') === false) { |
||
58 | $path_parts = pathinfo('a' . $path); |
||
59 | } else { |
||
60 | $path = str_replace('/', '/a', $path); |
||
61 | $path_parts = pathinfo($path); |
||
62 | } |
||
63 | |||
64 | return substr($path_parts[$part_name], 1); |
||
65 | } |
||
66 | |||
67 | public function allowFolderType($type) |
||
68 | { |
||
69 | if ($type == 'user') { |
||
70 | return $this->allowMultiUser(); |
||
71 | } else { |
||
72 | return $this->allowShareFolder(); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | public function getCategoryName() |
||
77 | { |
||
78 | $type = $this->currentLfmType(); |
||
79 | |||
80 | return $this->config->get('lfm.folder_categories.' . $type . '.folder_name', 'files'); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get current lfm type. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function currentLfmType() |
||
89 | { |
||
90 | $lfm_type = 'file'; |
||
91 | |||
92 | $request_type = lcfirst(Str::singular($this->input('type') ?: '')); |
||
93 | $available_types = array_keys($this->config->get('lfm.folder_categories') ?: []); |
||
94 | |||
95 | if (in_array($request_type, $available_types)) { |
||
96 | $lfm_type = $request_type; |
||
97 | } |
||
98 | |||
99 | return $lfm_type; |
||
100 | } |
||
101 | |||
102 | public function getDisplayMode() |
||
103 | { |
||
104 | $type_key = $this->currentLfmType(); |
||
105 | $startup_view = $this->config->get('lfm.folder_categories.' . $type_key . '.startup_view'); |
||
106 | |||
107 | $view_type = 'grid'; |
||
108 | $target_display_type = $this->input('show_list') ?: $startup_view; |
||
109 | |||
110 | if (in_array($target_display_type, ['list', 'grid'])) { |
||
111 | $view_type = $target_display_type; |
||
112 | } |
||
113 | |||
114 | return $view_type; |
||
115 | } |
||
116 | |||
117 | public function getUserSlug() |
||
118 | { |
||
119 | $config = $this->config->get('lfm.private_folder_name'); |
||
120 | |||
121 | if (is_callable($config)) { |
||
122 | return call_user_func($config); |
||
123 | } |
||
124 | |||
125 | if (class_exists($config)) { |
||
126 | return app()->make($config)->userField(); |
||
127 | } |
||
128 | |||
129 | return empty(auth()->user()) ? '' : auth()->user()->$config; |
||
130 | } |
||
131 | |||
132 | public function getRootFolder($type = null) |
||
133 | { |
||
134 | if (is_null($type)) { |
||
135 | $type = 'share'; |
||
136 | if ($this->allowFolderType('user')) { |
||
137 | $type = 'user'; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | if ($type === 'user') { |
||
142 | $folder = $this->getUserSlug(); |
||
143 | } else { |
||
144 | $folder = $this->config->get('lfm.shared_folder_name'); |
||
145 | } |
||
146 | |||
147 | // the slash is for url, dont replace it with directory seperator |
||
148 | return '/' . $folder; |
||
149 | } |
||
150 | |||
151 | public function getThumbFolderName() |
||
152 | { |
||
153 | return $this->config->get('lfm.thumb_folder_name'); |
||
154 | } |
||
155 | |||
156 | public function getFileType($ext) |
||
157 | { |
||
158 | return $this->config->get("lfm.file_type_array.{$ext}", 'File'); |
||
159 | } |
||
160 | |||
161 | public function availableMimeTypes() |
||
162 | { |
||
163 | return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.valid_mime'); |
||
164 | } |
||
165 | |||
166 | public function maxUploadSize() |
||
167 | { |
||
168 | return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.max_size'); |
||
169 | } |
||
170 | |||
171 | public function getPaginationPerPage() |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Check if users are allowed to use their private folders. |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function allowMultiUser() |
||
182 | { |
||
183 | return $this->config->get('lfm.allow_private_folder') === true; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Check if users are allowed to use the shared folder. |
||
188 | * This can be disabled only when allowMultiUser() is true. |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function allowShareFolder() |
||
193 | { |
||
194 | if (! $this->allowMultiUser()) { |
||
195 | return true; |
||
196 | } |
||
197 | |||
198 | return $this->config->get('lfm.allow_shared_folder') === true; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Translate file name to make it compatible on Windows. |
||
203 | * |
||
204 | * @param string $input Any string. |
||
205 | * @return string |
||
206 | */ |
||
207 | public function translateFromUtf8($input) |
||
208 | { |
||
209 | if ($this->isRunningOnWindows()) { |
||
210 | $input = iconv('UTF-8', mb_detect_encoding($input), $input); |
||
211 | } |
||
212 | |||
213 | return $input; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get directory seperator of current operating system. |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | public function ds() |
||
222 | { |
||
223 | $ds = Lfm::DS; |
||
224 | if ($this->isRunningOnWindows()) { |
||
225 | $ds = '\\'; |
||
226 | } |
||
227 | |||
228 | return $ds; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Check current operating system is Windows or not. |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | public function isRunningOnWindows() |
||
237 | { |
||
238 | return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Shorter function of getting localized error message.. |
||
243 | * |
||
244 | * @param mixed $error_type Key of message in lang file. |
||
245 | * @param mixed $variables Variables the message needs. |
||
246 | * @return string |
||
247 | */ |
||
248 | public function error($error_type, $variables = []) |
||
249 | { |
||
250 | throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-' . $error_type, $variables)); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Generates routes of this package. |
||
255 | * |
||
256 | * @return void |
||
257 | */ |
||
258 | public static function routes() |
||
355 | }); |
||
356 | } |
||
357 | } |
||
358 |
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.