| Conditions | 12 |
| Paths | 17 |
| Total Lines | 57 |
| Code Lines | 48 |
| 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 |
||
| 45 | public static function main(array $data): PBXApiResult |
||
| 46 | { |
||
| 47 | $mikoPBXConfig = new MikoPBXConfig(); |
||
| 48 | $res = new PBXApiResult(); |
||
| 49 | $res->processor = __METHOD__; |
||
| 50 | $di = Di::getDefault(); |
||
| 51 | $translation = $di->get(TranslationProvider::SERVICE_NAME); |
||
| 52 | $license = $di->get(MarketPlaceProvider::SERVICE_NAME); |
||
| 53 | if (strlen($data['licKey']) === 28 && Text::startsWith($data['licKey'], 'MIKO-')) { |
||
| 54 | ModelsBase::clearCache(PbxSettings::class); |
||
| 55 | $oldLicKey = $mikoPBXConfig->getGeneralSettings(PbxSettingsConstants::PBX_LICENSE); |
||
| 56 | if ($oldLicKey !== $data['licKey']) { |
||
| 57 | $licenseInfo = $license->getLicenseInfo($data['licKey']); |
||
| 58 | if ($licenseInfo instanceof SimpleXMLElement) { |
||
| 59 | $mikoPBXConfig->setGeneralSettings(PbxSettingsConstants::PBX_LICENSE, $data['licKey']); |
||
| 60 | $license->changeLicenseKey($data['licKey']); |
||
| 61 | $license->addTrial('11'); // MikoPBX forever license |
||
| 62 | $res->data[PbxSettingsConstants::PBX_LICENSE] = $data['licKey']; |
||
| 63 | $res->messages['info'][] = $translation->_('lic_SuccessfulActivation'); |
||
| 64 | $res->success = true; |
||
| 65 | } elseif (!empty($licenseInfo) && strpos($licenseInfo, '2026') !== false) { |
||
| 66 | $res->messages['license'][] = $translation->_('lic_FailedCheckLicense2026'); |
||
| 67 | $res->success = false; |
||
| 68 | } elseif (!empty($licenseInfo)) { |
||
| 69 | $res->messages['license'][] = $licenseInfo; |
||
| 70 | $res->success = false; |
||
| 71 | } else { |
||
| 72 | $res->messages['license'][] = $translation->_('lic_FailedCheckLicense'); |
||
| 73 | $res->success = false; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | if (!empty($data['coupon'])) { |
||
| 77 | $result = $license->activateCoupon($data['coupon']); |
||
| 78 | if ($result === true) { |
||
| 79 | $res->messages['info'][] = $translation->_('lic_SuccessfulCouponActivation'); |
||
| 80 | $res->success = true; |
||
| 81 | } else { |
||
| 82 | $res->messages['license'][] = $license->translateLicenseErrorMessage((string)$result); |
||
| 83 | $res->success = false; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } else { // Only add trial for a license key |
||
| 87 | $newLicenseKey = (string)$license->getTrialLicense($data); |
||
| 88 | if (strlen($newLicenseKey) === 28 |
||
| 89 | && Text::startsWith($newLicenseKey, 'MIKO-')) { |
||
| 90 | $mikoPBXConfig->setGeneralSettings(PbxSettingsConstants::PBX_LICENSE, $newLicenseKey); |
||
| 91 | $license->changeLicenseKey($newLicenseKey); |
||
| 92 | $res->success = true; |
||
| 93 | $res->data[PbxSettingsConstants::PBX_LICENSE] = $newLicenseKey; |
||
| 94 | $res->messages['info'] = $translation->_('lic_SuccessfulActivation'); |
||
| 95 | } else { |
||
| 96 | // No internet connection, or wrong data sent to license server, or something else |
||
| 97 | $res->messages['license'][] = $license->translateLicenseErrorMessage($newLicenseKey); |
||
| 98 | $res->success = false; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | return $res; |
||
| 102 | } |
||
| 103 | } |