| Total Complexity | 55 |
| Total Lines | 324 |
| Duplicated Lines | 0 % |
| Changes | 16 | ||
| Bugs | 5 | Features | 4 |
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 |
||
| 15 | class LfmPath |
||
| 16 | { |
||
| 17 | private $working_dir; |
||
| 18 | private $item_name; |
||
| 19 | private $is_thumb = false; |
||
| 20 | |||
| 21 | private $helper; |
||
| 22 | |||
| 23 | public function __construct(Lfm $lfm = null) |
||
| 24 | { |
||
| 25 | $this->helper = $lfm; |
||
| 26 | } |
||
| 27 | |||
| 28 | public function __get($var_name) |
||
| 29 | { |
||
| 30 | if ($var_name == 'storage') { |
||
| 31 | return $this->helper->getStorage($this->path('url')); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | public function __call($function_name, $arguments) |
||
| 36 | { |
||
| 37 | return $this->storage->$function_name(...$arguments); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function dir($working_dir) |
||
| 41 | { |
||
| 42 | $this->working_dir = $working_dir; |
||
| 43 | |||
| 44 | return $this; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function thumb($is_thumb = true) |
||
| 48 | { |
||
| 49 | $this->is_thumb = $is_thumb; |
||
| 50 | |||
| 51 | return $this; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function setName($item_name) |
||
| 55 | { |
||
| 56 | $this->item_name = $item_name; |
||
| 57 | |||
| 58 | return $this; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function getName() |
||
| 62 | { |
||
| 63 | return $this->item_name; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function path($type = 'storage') |
||
| 67 | { |
||
| 68 | if ($type == 'working_dir') { |
||
| 69 | // working directory: /{user_slug} |
||
| 70 | return $this->translateToLfmPath($this->normalizeWorkingDir()); |
||
| 71 | } elseif ($type == 'url') { |
||
| 72 | // storage: files/{user_slug} |
||
| 73 | // storage without folder: {user_slug} |
||
| 74 | return $this->helper->getCategoryName() === '.' |
||
| 75 | ? ltrim($this->path('working_dir'), '/') |
||
| 76 | : $this->helper->getCategoryName() . $this->path('working_dir'); |
||
| 77 | } elseif ($type == 'storage') { |
||
| 78 | // storage: files/{user_slug} |
||
| 79 | // storage on windows: files\{user_slug} |
||
| 80 | return str_replace(Lfm::DS, $this->helper->ds(), $this->path('url')); |
||
| 81 | } else { |
||
| 82 | // absolute: /var/www/html/project/storage/app/files/{user_slug} |
||
| 83 | // absolute on windows: C:\project\storage\app\files\{user_slug} |
||
| 84 | return $this->storage->rootPath() . $this->path('storage'); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | public function translateToLfmPath($path) |
||
| 89 | { |
||
| 90 | return str_replace($this->helper->ds(), Lfm::DS, $path); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function url() |
||
| 94 | { |
||
| 95 | return $this->storage->url($this->path('url')); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function folders() |
||
| 99 | { |
||
| 100 | $all_folders = array_map(function ($directory_path) { |
||
| 101 | return $this->pretty($directory_path, true); |
||
| 102 | }, $this->storage->directories()); |
||
| 103 | |||
| 104 | $folders = array_filter($all_folders, function ($directory) { |
||
| 105 | return $directory->name !== $this->helper->getThumbFolderName(); |
||
| 106 | }); |
||
| 107 | |||
| 108 | return $this->sortByColumn($folders); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function files() |
||
| 112 | { |
||
| 113 | $files = array_map(function ($file_path) { |
||
| 114 | return $this->pretty($file_path); |
||
| 115 | }, $this->storage->files()); |
||
| 116 | |||
| 117 | return $this->sortByColumn($files); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function pretty($item_path, $isDirectory = false) |
||
| 121 | { |
||
| 122 | return Container::getInstance()->makeWith(LfmItem::class, [ |
||
| 123 | 'lfm' => (clone $this)->setName($this->helper->getNameFromPath($item_path)), |
||
| 124 | 'helper' => $this->helper, |
||
| 125 | 'isDirectory' => $isDirectory |
||
| 126 | ]); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function delete() |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Create folder if not exist. |
||
| 140 | * |
||
| 141 | * @param string $path Real path of a directory. |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function createFolder() |
||
| 145 | { |
||
| 146 | if ($this->storage->exists($this)) { |
||
| 147 | return false; |
||
| 148 | } |
||
| 149 | |||
| 150 | $this->storage->makeDirectory(0777, true, true); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function isDirectory() |
||
| 154 | { |
||
| 155 | $working_dir = $this->path('working_dir'); |
||
| 156 | $parent_dir = substr($working_dir, 0, strrpos($working_dir, '/')); |
||
| 157 | |||
| 158 | $parent_directories = array_map(function ($directory_path) { |
||
| 159 | return app(static::class)->translateToLfmPath($directory_path); |
||
| 160 | }, app(static::class)->dir($parent_dir)->directories()); |
||
| 161 | |||
| 162 | return in_array($this->path('url'), $parent_directories); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Check a folder and its subfolders is empty or not. |
||
| 167 | * |
||
| 168 | * @param string $directory_path Real path of a directory. |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | public function directoryIsEmpty() |
||
| 174 | } |
||
| 175 | |||
| 176 | public function normalizeWorkingDir() |
||
| 177 | { |
||
| 178 | $path = $this->working_dir |
||
| 179 | ?: $this->helper->input('working_dir') |
||
| 180 | ?: $this->helper->getRootFolder(); |
||
| 181 | |||
| 182 | if ($this->is_thumb) { |
||
| 183 | // Prevent if working dir is "/" normalizeWorkingDir will add double "//" that breaks S3 functionality |
||
| 184 | $path = rtrim($path, Lfm::DS) . Lfm::DS . $this->helper->getThumbFolderName(); |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($this->getName()) { |
||
| 188 | // Prevent if working dir is "/" normalizeWorkingDir will add double "//" that breaks S3 functionality |
||
| 189 | $path = rtrim($path, Lfm::DS) . Lfm::DS . $this->getName(); |
||
| 190 | } |
||
| 191 | |||
| 192 | return $path; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sort files and directories. |
||
| 197 | * |
||
| 198 | * @param mixed $arr_items Array of files or folders or both. |
||
| 199 | * @return array of object |
||
| 200 | */ |
||
| 201 | public function sortByColumn($arr_items) |
||
| 202 | { |
||
| 203 | $sort_by = $this->helper->input('sort_type'); |
||
| 204 | if (in_array($sort_by, ['name', 'time'])) { |
||
| 205 | $key_to_sort = $sort_by; |
||
| 206 | } else { |
||
| 207 | $key_to_sort = 'name'; |
||
| 208 | } |
||
| 209 | |||
| 210 | uasort($arr_items, function ($a, $b) use ($key_to_sort) { |
||
| 211 | return strcasecmp($a->{$key_to_sort}, $b->{$key_to_sort}); |
||
| 212 | }); |
||
| 213 | |||
| 214 | return $arr_items; |
||
| 215 | } |
||
| 216 | |||
| 217 | public function error($error_type, $variables = []) |
||
| 220 | } |
||
| 221 | |||
| 222 | // Upload section |
||
| 223 | public function upload($file) |
||
| 224 | { |
||
| 225 | $new_file_name = $this->getNewName($file); |
||
| 226 | $new_file_path = $this->setName($new_file_name)->path('absolute'); |
||
| 227 | |||
| 228 | event(new FileIsUploading($new_file_path)); |
||
| 229 | event(new ImageIsUploading($new_file_path)); |
||
| 230 | try { |
||
| 231 | $this->setName($new_file_name)->storage->save($file); |
||
| 232 | |||
| 233 | $this->generateThumbnail($new_file_name); |
||
| 234 | } catch (\Exception $e) { |
||
| 235 | \Log::info($e); |
||
| 236 | return $this->error('invalid'); |
||
| 237 | } |
||
| 238 | event(new FileWasUploaded($new_file_path)); |
||
| 239 | event(new ImageWasUploaded($new_file_path)); |
||
| 240 | |||
| 241 | return $new_file_name; |
||
| 242 | } |
||
| 243 | |||
| 244 | public function validateUploadedFile($file) |
||
| 269 | } |
||
| 270 | |||
| 271 | private function getNewName($file) |
||
| 308 | } |
||
| 309 | |||
| 310 | public function generateThumbnail($file_name) |
||
| 341 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths