| Total Complexity | 63 |
| Total Lines | 389 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FolderManager 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 FolderManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class FolderManager |
||
| 35 | { |
||
| 36 | private $lang; |
||
| 37 | private $settings; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor |
||
| 41 | */ |
||
| 42 | public function __construct($lang) |
||
| 43 | { |
||
| 44 | $this->lang = $lang; |
||
| 45 | $this->loadSettings(); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Load settings |
||
| 50 | */ |
||
| 51 | private function loadSettings() |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Create a new folder |
||
| 63 | * |
||
| 64 | * @param array $params |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function createNewFolder(array $params): array |
||
| 68 | { |
||
| 69 | // Décomposer les paramètres pour une meilleure lisibilité |
||
| 70 | extract($params); |
||
| 71 | |||
| 72 | if ($this->isTitleNumeric($title)) { |
||
| 73 | return $this->errorResponse($this->lang->get('error_only_numbers_in_folder_name')); |
||
| 74 | } |
||
| 75 | |||
| 76 | if (!$this->isParentFolderAllowed($parent_id, $user_accessible_folders, $user_is_admin)) { |
||
| 77 | return $this->errorResponse($this->lang->get('error_folder_not_allowed_for_this_user')); |
||
| 78 | } |
||
| 79 | |||
| 80 | if (!$this->checkDuplicateFolderAllowed($title) && $personal_folder == 0) { |
||
| 81 | return $this->errorResponse($this->lang->get('error_group_exist')); |
||
| 82 | } |
||
| 83 | |||
| 84 | $parentFolderData = $this->getParentFolderData($parent_id); |
||
| 85 | |||
| 86 | $parentComplexity = $this->checkComplexityLevel($parentFolderData, $complexity, $parent_id); |
||
| 87 | if (isset($parentComplexity ['error']) && $parentComplexity['error'] === true) { |
||
| 88 | return $this->errorResponse($this->lang->get('error_folder_complexity_lower_than_top_folder') . " [<b>{$this->settings['TP_PW_COMPLEXITY'][$parentComplexity['valeur']][1]}</b>]"); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $this->createFolder($params, array_merge($parentFolderData, $parentComplexity)); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Check if title is numeric |
||
| 96 | * |
||
| 97 | * @param string $title |
||
| 98 | * @return boolean |
||
| 99 | */ |
||
| 100 | private function isTitleNumeric($title) |
||
| 101 | { |
||
| 102 | return is_numeric($title); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Check if parent folder is allowed |
||
| 107 | * |
||
| 108 | * @param integer $parent_id |
||
| 109 | * @param array $user_accessible_folders |
||
| 110 | * @param boolean $user_is_admin |
||
| 111 | * @return boolean |
||
| 112 | */ |
||
| 113 | private function isParentFolderAllowed($parent_id, $user_accessible_folders, $user_is_admin) |
||
| 114 | { |
||
| 115 | if (in_array($parent_id, $user_accessible_folders) === false |
||
| 116 | && (int) $user_is_admin !== 1 |
||
| 117 | ) { |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Check if duplicate folder is allowed |
||
| 125 | * |
||
| 126 | * @param string $title |
||
| 127 | * @return boolean |
||
| 128 | */ |
||
| 129 | private function checkDuplicateFolderAllowed($title) |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get parent folder data |
||
| 152 | * |
||
| 153 | * @param integer $parent_id |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | private function getParentFolderData($parent_id) |
||
| 157 | { |
||
| 158 | //check if parent folder is personal |
||
| 159 | $data = DB::queryfirstrow( |
||
| 160 | 'SELECT personal_folder, bloquer_creation, bloquer_modification |
||
| 161 | FROM ' . prefixTable('nested_tree') . ' |
||
| 162 | WHERE id = %i', |
||
| 163 | $parent_id |
||
| 164 | ); |
||
| 165 | |||
| 166 | // inherit from parent the specific settings it has |
||
| 167 | if (DB::count() > 0) { |
||
| 168 | $parentBloquerCreation = $data['bloquer_creation']; |
||
| 169 | $parentBloquerModification = $data['bloquer_modification']; |
||
| 170 | } else { |
||
| 171 | $parentBloquerCreation = 0; |
||
| 172 | $parentBloquerModification = 0; |
||
| 173 | } |
||
| 174 | |||
| 175 | return [ |
||
| 176 | 'isPersonal' => null !== $data['personal_folder'] ? $data['personal_folder'] : 0, |
||
| 177 | 'parentBloquerCreation' => $parentBloquerCreation, |
||
| 178 | 'parentBloquerModification' => $parentBloquerModification, |
||
| 179 | ]; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check complexity level |
||
| 184 | * |
||
| 185 | * @param array $data |
||
| 186 | * @param integer $complexity |
||
| 187 | * @param integer $parent_id |
||
| 188 | * @return array|boolean |
||
| 189 | */ |
||
| 190 | private function checkComplexityLevel( |
||
| 214 | ]; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Create folder |
||
| 219 | * |
||
| 220 | * @param array $params |
||
| 221 | * @param array $parentFolderData |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | private function createFolder($params, $parentFolderData) |
||
| 413 | ); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | private function errorResponse($message, $newIdSuffix = "") |
||
| 423 | ]; |
||
| 424 | } |
||
| 425 | } |