| Conditions | 5 |
| Paths | 1 |
| Total Lines | 125 |
| Code Lines | 64 |
| Lines | 16 |
| Ratio | 12.8 % |
| Changes | 10 | ||
| 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 |
||
| 44 | public function init(Service $service) |
||
| 45 | { |
||
| 46 | // get all configurations |
||
| 47 | $service->get( |
||
| 48 | '/config/common_names/', |
||
| 49 | function (Request $request, TokenInfo $tokenInfo) { |
||
| 50 | self::requireScope($tokenInfo, ['admin', 'portal']); |
||
| 51 | |||
| 52 | $userId = $request->getUrl()->getQueryParameter('user_id'); |
||
| 53 | if (is_null($userId)) { |
||
| 54 | self::requireScope($tokenInfo, ['admin']); |
||
| 55 | } else { |
||
| 56 | InputValidation::userId($userId); |
||
| 57 | } |
||
| 58 | |||
| 59 | $response = new JsonResponse(); |
||
| 60 | $response->setBody( |
||
| 61 | [ |
||
| 62 | 'items' => $this->configStorage->getAllCommonNameConfig($userId), |
||
| 63 | ] |
||
| 64 | ); |
||
| 65 | |||
| 66 | return $response; |
||
| 67 | } |
||
| 68 | ); |
||
| 69 | |||
| 70 | // get configuration for a particular common_name |
||
| 71 | $service->get( |
||
| 72 | '/config/common_names/:commonName', |
||
| 73 | function (Request $request, TokenInfo $tokenInfo, $commonName) { |
||
| 74 | self::requireScope($tokenInfo, ['admin', 'portal']); |
||
| 75 | |||
| 76 | InputValidation::commonName($commonName); |
||
| 77 | |||
| 78 | $response = new JsonResponse(); |
||
| 79 | $response->setBody( |
||
| 80 | $this->configStorage->getCommonNameConfig($commonName)->toArray() |
||
| 81 | ); |
||
| 82 | |||
| 83 | return $response; |
||
| 84 | } |
||
| 85 | ); |
||
| 86 | |||
| 87 | // set configuration for a particular common_name |
||
| 88 | $service->put( |
||
| 89 | '/config/common_names/:commonName', |
||
| 90 | function (Request $request, TokenInfo $tokenInfo, $commonName) { |
||
| 91 | self::requireScope($tokenInfo, ['admin']); |
||
| 92 | |||
| 93 | InputValidation::commonName($commonName); |
||
| 94 | |||
| 95 | // XXX check content type |
||
| 96 | // XXX allow for disconnect as well when updating config |
||
| 97 | |||
| 98 | $configData = new CommonNameConfig(Json::decode($request->getBody())); |
||
| 99 | $this->configStorage->setCommonNameConfig($commonName, $configData); |
||
| 100 | |||
| 101 | $response = new JsonResponse(); |
||
| 102 | $response->setBody( |
||
| 103 | ['ok' => true] |
||
| 104 | ); |
||
| 105 | |||
| 106 | return $response; |
||
| 107 | } |
||
| 108 | ); |
||
| 109 | |||
| 110 | // get configuration for a particular user_id |
||
| 111 | $service->get( |
||
| 112 | '/config/users/:userId', |
||
| 113 | View Code Duplication | function (Request $request, TokenInfo $tokenInfo, $userId) { |
|
|
|
|||
| 114 | self::requireScope($tokenInfo, ['admin', 'portal']); |
||
| 115 | |||
| 116 | InputValidation::userId($userId); |
||
| 117 | |||
| 118 | $userConfig = $this->configStorage->getUserConfig($userId); |
||
| 119 | // we never want the OTP secret to leave the system |
||
| 120 | $userConfig->hideOtpSecret(); |
||
| 121 | |||
| 122 | $response = new JsonResponse(); |
||
| 123 | $response->setBody( |
||
| 124 | $userConfig->toArray() |
||
| 125 | ); |
||
| 126 | |||
| 127 | return $response; |
||
| 128 | } |
||
| 129 | ); |
||
| 130 | |||
| 131 | // set configuration for a particular user_id |
||
| 132 | $service->put( |
||
| 133 | '/config/users/:userId', |
||
| 134 | function (Request $request, TokenInfo $tokenInfo, $userId) { |
||
| 135 | self::requireScope($tokenInfo, ['admin', 'portal']); |
||
| 136 | |||
| 137 | InputValidation::userId($userId); |
||
| 138 | |||
| 139 | $userConfig = $this->configStorage->getUserConfig($userId); |
||
| 140 | $newUserConfig = new UserConfig(Json::decode($request->getBody())); |
||
| 141 | |||
| 142 | if ($userConfig->getDisable() !== $newUserConfig->getDisable()) { |
||
| 143 | // only 'admin' can change disable state of user |
||
| 144 | self::requireScope($tokenInfo, ['admin']); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (false !== $userConfig->getOtpSecret()) { |
||
| 148 | // currently an OTP secret it set, the value has to be 'true' |
||
| 149 | // if not, admin is required to be able to update it |
||
| 150 | if (true !== $newUserConfig->getOtpSecret()) { |
||
| 151 | self::requireScope($tokenInfo, ['admin']); |
||
| 152 | } else { |
||
| 153 | // we use the old value |
||
| 154 | $newUserConfig->setOtpSecret($userConfig->getOtpSecret()); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->configStorage->setUserConfig($userId, $newUserConfig); |
||
| 159 | |||
| 160 | $response = new JsonResponse(); |
||
| 161 | $response->setBody( |
||
| 162 | ['ok' => true] |
||
| 163 | ); |
||
| 164 | |||
| 165 | return $response; |
||
| 166 | } |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | |||
| 181 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.