| Total Complexity | 46 |
| Total Lines | 329 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ItemController 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 ItemController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ItemController extends BaseController |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Get user's private key from database |
||
| 33 | * |
||
| 34 | * Retrieves the user's private key by decrypting it from the database. |
||
| 35 | * The private key is stored ENCRYPTED in the database and is decrypted |
||
| 36 | * using the session_key from the JWT token. |
||
| 37 | * |
||
| 38 | * @param array $userData User data from JWT token (must include id, key_tempo, and session_key) |
||
| 39 | * @return string|null Decrypted private key or null if not found/invalid session |
||
| 40 | */ |
||
| 41 | private function getUserPrivateKey(array $userData): ?string |
||
| 42 | { |
||
| 43 | include_once API_ROOT_PATH . '/inc/jwt_utils.php'; |
||
| 44 | |||
| 45 | // Verify session_key exists in JWT payload |
||
| 46 | if (!isset($userData['session_key']) || empty($userData['session_key'])) { |
||
| 47 | error_log('getUserPrivateKey: Missing session_key in JWT token for user ID ' . $userData['id']); |
||
| 48 | return null; |
||
| 49 | } |
||
| 50 | |||
| 51 | $userKeys = get_user_keys( |
||
| 52 | (int) $userData['id'], |
||
| 53 | (string) $userData['key_tempo'], |
||
| 54 | (string) $userData['session_key'] // Session key from JWT for decryption |
||
| 55 | ); |
||
| 56 | |||
| 57 | if ($userKeys === null) { |
||
| 58 | return null; |
||
| 59 | } |
||
| 60 | |||
| 61 | return $userKeys['private_key']; |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Manage case inFolder - get items inside an array of folders |
||
| 67 | * |
||
| 68 | * @param array $userData |
||
| 69 | */ |
||
| 70 | public function inFoldersAction(array $userData): void |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | //end InFoldersAction() |
||
| 162 | |||
| 163 | private function checkNewItemData(array $arrQueryStringParams, array $userData): array |
||
| 164 | { |
||
| 165 | if (isset($arrQueryStringParams['label']) === true |
||
| 166 | && isset($arrQueryStringParams['folder_id']) === true |
||
| 167 | && isset($arrQueryStringParams['password']) === true |
||
| 168 | && isset($arrQueryStringParams['login']) === true |
||
| 169 | && isset($arrQueryStringParams['email']) === true |
||
| 170 | && isset($arrQueryStringParams['url']) === true |
||
| 171 | && isset($arrQueryStringParams['tags']) === true |
||
| 172 | && isset($arrQueryStringParams['anyone_can_modify']) === true |
||
| 173 | ) { |
||
| 174 | // |
||
| 175 | if (in_array($arrQueryStringParams['folder_id'], $userData['folders_list']) === false && $userData['user_can_create_root_folder'] === 0) { |
||
| 176 | return [ |
||
| 177 | 'error' => true, |
||
| 178 | 'strErrorDesc' => 'User is not allowed in this folder', |
||
| 179 | 'strErrorHeader' => 'HTTP/1.1 401 Unauthorized', |
||
| 180 | ]; |
||
| 181 | } else if (empty($arrQueryStringParams['label']) === true) { |
||
| 182 | return [ |
||
| 183 | 'error' => true, |
||
| 184 | 'strErrorDesc' => 'Label is mandatory', |
||
| 185 | 'strErrorHeader' => 'HTTP/1.1 401 Expected parameters not provided', |
||
| 186 | ]; |
||
| 187 | } else { |
||
| 188 | return [ |
||
| 189 | 'error' => false, |
||
| 190 | ]; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return [ |
||
| 195 | 'error' => true, |
||
| 196 | 'strErrorDesc' => 'All fields have to be provided even if empty (refer to documentation).', |
||
| 197 | 'strErrorHeader' => 'HTTP/1.1 401 Expected parameters not provided', |
||
| 198 | ]; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Manage case Add |
||
| 203 | * |
||
| 204 | * @param array $userData |
||
| 205 | */ |
||
| 206 | public function createAction(array $userData) |
||
| 207 | { |
||
| 208 | $request = symfonyRequest::createFromGlobals(); |
||
| 209 | $requestMethod = $request->getMethod(); |
||
| 210 | $strErrorDesc = $strErrorHeader = $responseData = ''; |
||
| 211 | |||
| 212 | if (strtoupper($requestMethod) === 'POST') { |
||
| 213 | // Is user allowed to create a folder |
||
| 214 | // We check if allowed_to_create |
||
| 215 | if ((int) $userData['allowed_to_create'] !== 1) { |
||
| 216 | $strErrorDesc = 'User is not allowed to create an item'; |
||
| 217 | $strErrorHeader = 'HTTP/1.1 401 Unauthorized'; |
||
| 218 | } else { |
||
| 219 | if (empty($userData['folders_list']) === false) { |
||
| 220 | $userData['folders_list'] = explode(',', $userData['folders_list']); |
||
| 221 | } else { |
||
| 222 | $userData['folders_list'] = []; |
||
| 223 | } |
||
| 224 | |||
| 225 | // get parameters |
||
| 226 | $arrQueryStringParams = $this->getQueryStringParams(); |
||
| 227 | |||
| 228 | // Check that the parameters are indeed an array before using them |
||
| 229 | if (is_array($arrQueryStringParams)) { |
||
| 230 | // check parameters |
||
| 231 | $arrCheck = $this->checkNewItemData($arrQueryStringParams, $userData); |
||
| 232 | |||
| 233 | if ($arrCheck['error'] === true) { |
||
| 234 | $strErrorDesc = $arrCheck['strErrorDesc']; |
||
| 235 | $strErrorHeader = $arrCheck['strErrorHeader']; |
||
| 236 | } else { |
||
| 237 | // launch |
||
| 238 | $itemModel = new ItemModel(); |
||
| 239 | $ret = $itemModel->addItem( |
||
| 240 | (int) $arrQueryStringParams['folder_id'], |
||
| 241 | (string) $arrQueryStringParams['label'], |
||
| 242 | (string) $arrQueryStringParams['password'], |
||
| 243 | (string) $arrQueryStringParams['description'], |
||
| 244 | (string) $arrQueryStringParams['login'], |
||
| 245 | (string) $arrQueryStringParams['email'], |
||
| 246 | (string) $arrQueryStringParams['url'], |
||
| 247 | (string) $arrQueryStringParams['tags'], |
||
| 248 | (string) $arrQueryStringParams['anyone_can_modify'], |
||
| 249 | (string) $arrQueryStringParams['icon'], |
||
| 250 | (int) $userData['id'], |
||
| 251 | (string) $userData['username'], |
||
| 252 | ); |
||
| 253 | $responseData = json_encode($ret); |
||
| 254 | } |
||
| 255 | |||
| 256 | } else { |
||
| 257 | // Gérer le cas où les paramètres ne sont pas un tableau |
||
| 258 | $strErrorDesc = 'Data not consistent'; |
||
| 259 | $strErrorHeader = 'Expected array, received ' . gettype($arrQueryStringParams); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | $strErrorDesc = 'Method not supported'; |
||
| 264 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 265 | } |
||
| 266 | |||
| 267 | // send output |
||
| 268 | if (empty($strErrorDesc) === true) { |
||
| 269 | $this->sendOutput( |
||
| 270 | $responseData, |
||
| 271 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 272 | ); |
||
| 273 | } else { |
||
| 274 | $this->sendOutput( |
||
| 275 | json_encode(['error' => $strErrorDesc]), |
||
| 276 | ['Content-Type: application/json', $strErrorHeader] |
||
| 277 | ); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | //end addAction() |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Manage case get - get an item |
||
| 285 | * |
||
| 286 | * @param array $userData |
||
| 287 | */ |
||
| 288 | public function getAction(array $userData): void |
||
| 357 | ); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | //end getAction() |
||
| 362 |