Conditions | 13 |
Paths | 132 |
Total Lines | 91 |
Code Lines | 61 |
Lines | 14 |
Ratio | 15.38 % |
Changes | 3 | ||
Bugs | 1 | Features | 2 |
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 |
||
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()])), |
||
141 | SS_Log::WARN); |
||
142 | } catch(Exception $ex) { |
||
143 | // an unexpected exception was caught |
||
144 | $response->addHeader('Content-Type', $serializer->contentType()); |
||
145 | $response->setStatusCode("500"); |
||
146 | $body = [ |
||
147 | 'message' => $ex->getMessage(), |
||
148 | 'code' => $ex->getCode() |
||
149 | ]; |
||
150 | View Code Duplication | if(Director::isDev()) { |
|
151 | $body = array_merge($body, [ |
||
152 | 'file' => $ex->getFile(), |
||
153 | 'line' => $ex->getLine(), |
||
154 | 'trace' => $ex->getTrace() |
||
155 | ]); |
||
156 | } |
||
157 | // log all data and the trace to get a better understanding of the exception |
||
158 | SS_Log::log( |
||
159 | json_encode(array_merge( |
||
160 | $body, ['file' => $ex->getFile(), 'line' => $ex->getLine(),'trace' => $ex->getTrace()])), |
||
161 | SS_Log::ERR); |
||
162 | } |
||
163 | // serialize content and set body of response |
||
164 | $response->addHeader('Content-Type', $serializer->contentType()); |
||
165 | // TODO: body could be an exception; check it before the response is generated |
||
166 | $response->setBody($serializer->serialize($body)); |
||
167 | // set CORS header from config |
||
168 | $response = $this->addCORSHeaders($response); |
||
169 | return $response; |
||
170 | } |
||
171 | |||
231 |