| Conditions | 13 |
| Paths | 132 |
| Total Lines | 92 |
| Code Lines | 61 |
| Lines | 14 |
| Ratio | 15.22 % |
| Changes | 16 | ||
| Bugs | 0 | Features | 9 |
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 |
||
| 48 | // set CORS header from config |
||
| 49 | $response = $this->addCORSHeaders($response); |
||
| 50 | $response->output(); |
||
| 51 | exit; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param SS_HTTPRequest $request |
||
| 57 | * @return null |
||
| 58 | * @throws RestUserException |
||
| 59 | */ |
||
| 60 | public function head(SS_HTTPRequest $request) { |
||
| 61 | if(method_exists($this, 'get')) { |
||
| 62 | $result = $this->get($request); |
||
| 63 | if($result instanceof SS_HTTPResponse) { |
||
| 64 | $result->setBody(null); |
||
| 65 | return $result; |
||
| 66 | } |
||
| 67 | return null; |
||
| 68 | } |
||
| 69 | throw new RestUserException("Endpoint doesn't have a GET implementation", 404); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * handleAction implementation for rest controllers. This handles the requested action differently then the standard |
||
| 74 | * implementation. |
||
| 75 | * |
||
| 76 | * @param SS_HTTPRequest $request |
||
| 77 | * @param string $action |
||
| 78 | * @return HTMLText|SS_HTTPResponse |
||
| 79 | */ |
||
| 80 | protected function handleAction($request, $action) { |
||
| 81 | foreach($request->latestParams() as $k => $v) { |
||
| 82 | if($v || !isset($this->urlParams[$k])) $this->urlParams[$k] = $v; |
||
| 83 | } |
||
| 84 | // set the action to the request method / for developing we could use an additional parameter to choose another method |
||
| 85 | $action = $this->getMethodName($request); |
||
| 86 | $this->action = $action; |
||
| 87 | $this->requestParams = $request->requestVars(); |
||
| 88 | $className = $this->class; |
||
| 89 | // create serializer |
||
| 90 | $serializer = SerializerFactory::create_from_request($request); |
||
| 91 | $response = $this->getResponse(); |
||
| 92 | // perform action |
||
| 93 | try { |
||
| 94 | if(!$this->hasAction($action)) { |
||
| 95 | // method couldn't found on controller |
||
| 96 | throw new RestUserException("Action '$action' isn't available on class $className.", 404); |
||
| 97 | } |
||
| 98 | if(!$this->checkAccessAction($action)) { |
||
| 99 | throw new RestUserException("Action '$action' isn't allowed on class $className.", 404, 401); |
||
| 100 | } |
||
| 101 | $actionResult = null; |
||
| 102 | if(method_exists($this, 'beforeCallActionHandler')) { |
||
| 103 | // call before action hook |
||
| 104 | $actionResult = $this->beforeCallActionHandler($request, $action); |
||
| 105 | } |
||
| 106 | // if action hook contains data it will be used as result, otherwise the action handler will be called |
||
| 107 | if(!$actionResult) { |
||
| 108 | // perform action |
||
| 109 | $actionResult = $this->$action($request); |
||
| 110 | } |
||
| 111 | $body = $actionResult; |
||
| 112 | } catch(RestUserException $ex) { |
||
| 113 | // a user exception was caught |
||
| 114 | $response->setStatusCode($ex->getHttpStatusCode()); |
||
| 115 | $body = [ |
||
| 116 | 'message' => $ex->getMessage(), |
||
| 117 | 'code' => $ex->getCode() |
||
| 118 | ]; |
||
| 119 | // log all data |
||
| 120 | SS_Log::log( |
||
| 121 | json_encode(array_merge($body, ['file' => $ex->getFile(), 'line' => $ex->getLine()])), |
||
| 122 | SS_Log::INFO); |
||
| 123 | } catch(RestSystemException $ex) { |
||
| 124 | // a system exception was caught |
||
| 125 | $response->addHeader('Content-Type', $serializer->contentType()); |
||
| 126 | $response->setStatusCode($ex->getHttpStatusCode()); |
||
| 127 | $body = [ |
||
| 128 | 'message' => $ex->getMessage(), |
||
| 129 | 'code' => $ex->getCode() |
||
| 130 | ]; |
||
| 131 | View Code Duplication | if(Director::isDev()) { |
|
| 132 | $body = array_merge($body, [ |
||
| 133 | 'file' => $ex->getFile(), |
||
| 134 | 'line' => $ex->getLine(), |
||
| 135 | 'trace' => $ex->getTrace() |
||
| 136 | ]); |
||
| 137 | } |
||
| 138 | // log all data |
||
| 139 | SS_Log::log( |
||
| 140 | json_encode(array_merge($body, ['file' => $ex->getFile(), 'line' => $ex->getLine()])), |
||
| 231 |