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