| Total Complexity | 59 |
| Total Lines | 392 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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() |
||
| 52 | { |
||
| 53 | try { |
||
| 54 | include_once TEAMPASS_ROOT_PATH.'/includes/config/tp.config.php'; |
||
| 55 | $this->settings = $GLOBALS['SETTINGS']; |
||
| 56 | } catch (Exception $e) { |
||
| 57 | throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1); |
||
| 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)) { |
||
| 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, $user_is_admin, $user_is_manager, $user_can_manage_all_users); |
||
| 87 | if (!$parentComplexity) { |
||
| 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) |
||
| 130 | { |
||
| 131 | if ( |
||
| 132 | isset($this->settings['duplicate_folder']) === true |
||
| 133 | && (int) $this->settings['duplicate_folder'] === 0 |
||
| 134 | ) { |
||
| 135 | DB::query( |
||
| 136 | 'SELECT * |
||
| 137 | FROM ' . prefixTable('nested_tree') . ' |
||
| 138 | WHERE title = %s', |
||
| 139 | $title |
||
| 140 | ); |
||
| 141 | $counter = DB::count(); |
||
| 142 | if ($counter !== 0) { |
||
| 143 | return false; |
||
| 144 | } |
||
| 145 | return true; |
||
| 146 | } |
||
| 147 | return true; |
||
| 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' => $data['personal_folder'], |
||
| 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 | * @param boolean $user_is_admin |
||
| 189 | * @param boolean $user_is_manager |
||
| 190 | * @param boolean $user_can_manage_all_users |
||
| 191 | * @return array|boolean |
||
| 192 | */ |
||
| 193 | private function checkComplexityLevel( |
||
| 194 | $data, |
||
| 195 | $complexity, |
||
| 196 | $parent_id, |
||
| 197 | $user_is_admin, |
||
| 198 | $user_is_manager, |
||
| 199 | $user_can_manage_all_users |
||
| 200 | ) |
||
| 201 | { |
||
| 202 | if (isset($data) === false || (int) $data['isPersonal'] === 0) { |
||
| 203 | // get complexity level for this folder |
||
| 204 | $data = DB::queryfirstrow( |
||
| 205 | 'SELECT valeur |
||
| 206 | FROM ' . prefixTable('misc') . ' |
||
| 207 | WHERE intitule = %i AND type = %s', |
||
| 208 | $parent_id, |
||
| 209 | 'complex' |
||
| 210 | ); |
||
| 211 | if (isset($data['valeur']) === true && intval($complexity) < intval($data['valeur'])) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | return [ |
||
| 217 | 'parent_complexity' => isset($data['valeur']) === true ? $data['valeur'] : 0, |
||
| 218 | ]; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Create folder |
||
| 223 | * |
||
| 224 | * @param array $params |
||
| 225 | * @param array $parentFolderData |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | private function createFolder($params, $parentFolderData) |
||
| 416 | ); |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | private function errorResponse($message, $newIdSuffix = "") |
||
| 421 | { |
||
| 426 | ]; |
||
| 427 | } |
||
| 428 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.