Passed
Push — develop ( a6a975...49e3f5 )
by Nikolay
04:45 queued 13s
created

GetMikoPBXFeatureStatusAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 33
c 1
b 0
f 0
dl 0
loc 45
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A main() 0 38 5
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\PBXCoreREST\Lib\License;
21
22
use MikoPBX\Common\Models\PbxSettings;
23
use MikoPBX\Common\Models\PbxSettingsConstants;
24
use MikoPBX\Common\Providers\ManagedCacheProvider;
25
use MikoPBX\Common\Providers\MarketPlaceProvider;
26
use MikoPBX\Common\Providers\TranslationProvider;
27
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
28
use Phalcon\Di;
29
use Phalcon\Text;
30
31
/**
32
 * Class GetMikoPBXFeatureStatusAction
33
 * Check for free MikoPBX base license.
34
 * @package MikoPBX\PBXCoreREST\Lib\Modules
35
 */
36
class GetMikoPBXFeatureStatusAction extends \Phalcon\Di\Injectable
37
{
38
    /**
39
     * Check for free MikoPBX base license.
40
     *
41
     * @return PBXApiResult An object containing the result of the API call.
42
     */
43
    public static function main(): PBXApiResult
44
    {
45
        $res = new PBXApiResult();
46
        $res->processor = __METHOD__;
47
        $licenseKey = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_LICENSE);
48
        $di = Di::getDefault();
49
        if ((strlen($licenseKey) === 28
50
            && Text::startsWith($licenseKey, 'MIKO-')
51
        )) {
52
            $cacheKey = 'PBXCoreREST:LicenseManagementProcessor:GetMikoPBXFeatureStatusAction:' . $licenseKey;
53
            $managedCache = $di->get(ManagedCacheProvider::SERVICE_NAME);
54
            $lastMikoPBXFeatureInfo = $managedCache->get($cacheKey);
55
            if ($lastMikoPBXFeatureInfo === null) {
56
                $lastMikoPBXFeatureInfo = [];
57
                $license = $di->get(MarketPlaceProvider::SERVICE_NAME);
58
                $checkBaseFeature = $license->featureAvailable(33);
59
                if ($checkBaseFeature['success'] === false) {
60
                    $lastMikoPBXFeatureInfo['success'] = false;
61
                    $textError = (string)($checkBaseFeature['error'] ?? '');
62
                    $lastMikoPBXFeatureInfo['messages']['license'][] = $license->translateLicenseErrorMessage($textError);
63
                    $cacheTimeout = 120;
64
                } else {
65
                    $lastMikoPBXFeatureInfo['success'] = true;
66
                    $cacheTimeout = 86400;
67
                }
68
                $res->success =  $lastMikoPBXFeatureInfo['success'];
69
                $res->messages = $lastMikoPBXFeatureInfo['messages'];
70
                $managedCache->set($cacheKey, $lastMikoPBXFeatureInfo, $cacheTimeout); // Check not often than every 2 minutes
71
            } else {
72
                $res->success = $lastMikoPBXFeatureInfo['success'];
73
                $res->messages = $lastMikoPBXFeatureInfo['messages'];
74
            }
75
        } else {
76
            $res->success = false;
77
            $translation = $di->get(TranslationProvider::SERVICE_NAME);
78
            $res->messages['license'][] = $translation->_('lic_WrongLicenseKeyOrEmpty');
79
        }
80
        return $res;
81
    }
82
}