| Conditions | 6 |
| Paths | 14 |
| Total Lines | 55 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | public static function main(): PBXApiResult |
||
| 28 | { |
||
| 29 | $res = new PBXApiResult(); |
||
| 30 | $res->processor = __METHOD__; |
||
| 31 | |||
| 32 | $di = Di::getDefault(); |
||
| 33 | if ($di === null) { |
||
| 34 | $res->success = false; |
||
| 35 | $res->messages[] = 'Dependency injector does not initialized'; |
||
| 36 | return $res; |
||
| 37 | } |
||
| 38 | $WebUiLanguage = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LANGUAGE); |
||
| 39 | $cacheKey = "ModulesManagementProcessor:GetAvailableModules:$WebUiLanguage"; |
||
| 40 | $managedCache = $di->getShared(ManagedCacheProvider::SERVICE_NAME); |
||
| 41 | if ($managedCache->has($cacheKey)){ |
||
| 42 | $body = $managedCache->get($cacheKey); |
||
| 43 | } else { |
||
| 44 | $PBXVersion = PbxSettings::getValueByKey('PBXVersion'); |
||
| 45 | $PBXVersion = (string)str_ireplace('-dev', '', $PBXVersion); |
||
| 46 | $body = ''; |
||
| 47 | $client = new GuzzleHttp\Client(); |
||
| 48 | try { |
||
| 49 | $request = $client->request( |
||
| 50 | 'POST', |
||
| 51 | 'https://releases.mikopbx.com/releases/v1/mikopbx/getAvailableModules', |
||
| 52 | [ |
||
| 53 | 'headers' => [ |
||
| 54 | 'Content-Type' => 'application/json; charset=utf-8', |
||
| 55 | ], |
||
| 56 | 'json' => [ |
||
| 57 | 'PBXVER' => $PBXVersion, |
||
| 58 | 'LANGUAGE'=> $WebUiLanguage, |
||
| 59 | ], |
||
| 60 | 'timeout' => 5, |
||
| 61 | ] |
||
| 62 | ); |
||
| 63 | $code = $request->getStatusCode(); |
||
| 64 | if ($code === Response::OK){ |
||
| 65 | $body = $request->getBody()->getContents(); |
||
| 66 | $managedCache->set($cacheKey, $body, 3600); |
||
| 67 | } |
||
| 68 | } catch (\Throwable $e) { |
||
| 69 | $code = Response::INTERNAL_SERVER_ERROR; |
||
| 70 | Util::sysLogMsg(static::class, $e->getMessage()); |
||
| 71 | $res->messages[] = $e->getMessage(); |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($code !== Response::OK) { |
||
| 75 | return $res; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | $res->data = json_decode($body, true)??[]; |
||
| 79 | $res->success = true; |
||
| 80 | |||
| 81 | return $res; |
||
| 82 | } |
||
| 84 | } |