| Conditions | 8 |
| Paths | 28 |
| Total Lines | 51 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 33 | public function authorizeAction() |
||
| 34 | { |
||
| 35 | $request = symfonyRequest::createFromGlobals(); |
||
| 36 | $requestMethod = $request->getMethod(); |
||
| 37 | $strErrorDesc = $responseData = $strErrorHeader = ''; |
||
| 38 | |||
| 39 | if (strtoupper($requestMethod) === 'POST') { |
||
| 40 | require API_ROOT_PATH . "/Model/AuthModel.php"; |
||
| 41 | try { |
||
| 42 | $authModel = new AuthModel(); |
||
| 43 | |||
| 44 | // Leer el JSON del body del POST |
||
| 45 | $body = $request->getContent(); |
||
| 46 | $data = json_decode($body, true); |
||
| 47 | |||
| 48 | $login = $data['login'] ?? null; |
||
| 49 | $password = $data['password'] ?? null; |
||
| 50 | $apikey = $data['apikey'] ?? null; |
||
| 51 | |||
| 52 | // Validación mínima para evitar null |
||
| 53 | if (!$login || !$password || !$apikey) { |
||
| 54 | throw new Exception('Missing parameter'); |
||
| 55 | } |
||
| 56 | |||
| 57 | $arrUser = $authModel->getUserAuth($login, $password, $apikey); |
||
| 58 | |||
| 59 | if (array_key_exists("token", $arrUser)) { |
||
| 60 | $responseData = json_encode($arrUser); |
||
| 61 | } else { |
||
| 62 | $strErrorDesc = $arrUser['error'] . " (" . $arrUser['info'] . ")"; |
||
| 63 | $strErrorHeader = 'HTTP/1.1 401 Unauthorized'; |
||
| 64 | } |
||
| 65 | } catch (Error|Exception $e) { |
||
| 66 | $strErrorDesc = $e->getMessage().' Something went wrong! Please contact support.2'; |
||
| 67 | $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; |
||
| 68 | } |
||
| 69 | } else { |
||
| 70 | $strErrorDesc = 'Method '.$requestMethod.' not supported'; |
||
| 71 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 72 | } |
||
| 73 | |||
| 74 | // send output |
||
| 75 | if (empty($strErrorDesc) === true) { |
||
| 76 | $this->sendOutput( |
||
| 77 | $responseData, |
||
| 78 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 79 | ); |
||
| 80 | } else { |
||
| 81 | $this->sendOutput( |
||
| 82 | json_encode(['error' => $strErrorDesc]), |
||
| 83 | ['Content-Type: application/json', $strErrorHeader] |
||
| 84 | ); |
||
| 88 |