Conditions | 11 |
Paths | 15 |
Total Lines | 50 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Tests | 27 |
CRAP Score | 11.2597 |
Changes | 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 |
||
27 | 69 | public function call(Micro $api) |
|
28 | { |
||
29 | 69 | $router = $api->getService('router'); |
|
30 | 69 | $request = $api->getService('request'); |
|
31 | |||
32 | 69 | if ($this->isValidCheck($request, $api)) { |
|
33 | // explode() by / , postiion #1 is always the controller , so its the resource ^.^ |
||
34 | 61 | $matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern()); |
|
35 | |||
36 | 61 | $resource = ucfirst(isset($matchRouter[2]) ? $matchRouter[2] : $matchRouter[1]); //2 is alwasy the controller of the router |
|
37 | 61 | $userData = $api->getService('userData'); |
|
38 | |||
39 | 61 | $action = null; |
|
40 | // GET -> read |
||
41 | // PUT -> update |
||
42 | // DELETE -> delete |
||
43 | // POST -> create |
||
44 | |||
45 | 61 | if (!Subscription::getPaymentStatus()) { |
|
46 | throw new ServerErrorHttpException('Subscription is not active.Please contact your admin'); |
||
47 | } |
||
48 | |||
49 | 61 | switch (strtolower($request->getMethod())) { |
|
50 | 61 | case 'get': |
|
51 | 38 | $action = 'list'; |
|
52 | 38 | if (preg_match("/\/([0-9]+)(?=[^\/]*$)/", $request->getURI())) { |
|
53 | 7 | $action = 'read'; |
|
54 | } |
||
55 | 38 | break; |
|
56 | 43 | case 'post': |
|
57 | 21 | $action = 'create'; |
|
58 | 21 | break; |
|
59 | 22 | case 'delete': |
|
60 | 8 | $action = 'delete'; |
|
61 | 8 | break; |
|
62 | 14 | case 'put': |
|
63 | case 'patch': |
||
64 | 14 | $action = 'update'; |
|
65 | 14 | break; |
|
66 | default: |
||
67 | throw new ServerErrorHttpException('No Permission define for this action'); |
||
68 | break; |
||
69 | } |
||
70 | //do you have permision |
||
71 | 61 | if (!$userData->can($resource . '.' . $action)) { |
|
72 | 1 | throw new PermissionException('You dont have permission to run this action ' . $action . ' at ' . $resource); |
|
73 | } |
||
74 | } |
||
75 | |||
76 | 69 | return true; |
|
77 | } |
||
79 |