| Conditions | 3 |
| Paths | 1 |
| Total Lines | 68 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 49 | public function init(Service $service) |
||
| 50 | { |
||
| 51 | // get all configurations |
||
| 52 | $service->get( |
||
| 53 | '/config/', |
||
| 54 | function (Request $request, TokenInfo $tokenInfo) { |
||
| 55 | $userId = $request->getUrl()->getQueryParameter('user_id'); |
||
| 56 | if (!is_null($userId)) { |
||
| 57 | self::requireScope($tokenInfo, ['config_get', 'config_get_user']); |
||
| 58 | InputValidation::userId($userId); |
||
| 59 | } else { |
||
| 60 | self::requireScope($tokenInfo, ['config_get']); |
||
| 61 | } |
||
| 62 | |||
| 63 | $response = new JsonResponse(); |
||
| 64 | $response->setBody( |
||
| 65 | [ |
||
| 66 | 'items' => $this->configStorage->getAllConfig($userId), |
||
| 67 | ] |
||
| 68 | ); |
||
| 69 | |||
| 70 | return $response; |
||
| 71 | } |
||
| 72 | ); |
||
| 73 | |||
| 74 | // get configuration for a particular common_name |
||
| 75 | $service->get( |
||
| 76 | '/config/:commonName', |
||
| 77 | function (Request $request, TokenInfo $tokenInfo, $commonName) { |
||
| 78 | self::requireScope($tokenInfo, ['config_get']); |
||
| 79 | |||
| 80 | InputValidation::commonName($commonName); |
||
| 81 | |||
| 82 | $response = new JsonResponse(); |
||
| 83 | $response->setBody( |
||
| 84 | $this->configStorage->getConfig($commonName)->toArray() |
||
| 85 | ); |
||
| 86 | |||
| 87 | return $response; |
||
| 88 | } |
||
| 89 | ); |
||
| 90 | |||
| 91 | // set configuration for a particular common_name |
||
| 92 | $service->put( |
||
| 93 | '/config/:commonName', |
||
| 94 | function (Request $request, TokenInfo $tokenInfo, $commonName) { |
||
| 95 | self::requireScope($tokenInfo, ['config_update']); |
||
| 96 | |||
| 97 | // XXX check content type |
||
| 98 | // XXX allow for disconnect as well when updating config |
||
| 99 | |||
| 100 | InputValidation::commonName($commonName); |
||
| 101 | |||
| 102 | $configData = new ConfigData(Json::decode($request->getBody())); |
||
| 103 | if (!in_array($configData->getPool(), $this->allowedPools)) { |
||
| 104 | throw new BadRequestException('invalid "pool"'); |
||
| 105 | } |
||
| 106 | $this->configStorage->setConfig($commonName, $configData); |
||
| 107 | |||
| 108 | $response = new JsonResponse(); |
||
| 109 | $response->setBody( |
||
| 110 | ['ok' => true] |
||
| 111 | ); |
||
| 112 | |||
| 113 | return $response; |
||
| 114 | } |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 129 |