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

GetLicenseInfoAction::main()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 30
rs 9.2408
cc 5
nc 4
nop 0
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
use SimpleXMLElement;
31
32
/**
33
 * Class GetLicenseInfoAction
34
 * Returns license info from license server by key.
35
 * @package MikoPBX\PBXCoreREST\Lib\Modules
36
 */
37
class GetLicenseInfoAction extends \Phalcon\Di\Injectable
38
{
39
    /**
40
     * Returns license info from license server by key.
41
     *
42
     * @return PBXApiResult An object containing the result of the API call.
43
     */
44
    public static function main(): PBXApiResult
45
    {
46
        $res = new PBXApiResult();
47
        $res->processor = __METHOD__;
48
        $di = Di::getDefault();
49
        // Retrieve the last get license request from the cache
50
        $licenseKey = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_LICENSE);
51
        if ((strlen($licenseKey) === 28
52
            && Text::startsWith($licenseKey, 'MIKO-')
53
        )) {
54
            $cacheKey = 'PBXCoreREST:LicenseManagementProcessor:getLicenseInfoAction:'.$licenseKey;
55
            $managedCache = $di->get(ManagedCacheProvider::SERVICE_NAME);
56
            $lastGetLicenseInfo = $managedCache->get($cacheKey);
57
            if ($lastGetLicenseInfo === null) {
58
                $license = $di->get(MarketPlaceProvider::SERVICE_NAME);
59
                $licenseInfo =  $license->getLicenseInfo($licenseKey);
60
                if ($licenseInfo instanceof SimpleXMLElement) {
61
                    $res->success = true;
62
                    $res->data['licenseInfo'] = json_encode($licenseInfo);
63
                }
64
                $managedCache->set($cacheKey, $res->data['licenseInfo'], 120); // Check not often than every 2 minutes
65
            } else {
66
                $res->data['licenseInfo']=$lastGetLicenseInfo;
67
                $res->success = true;
68
            }
69
        } else {
70
            $translation = $di->get(TranslationProvider::SERVICE_NAME);
71
            $res->messages['error'][] = $translation->_('lic_WrongLicenseKeyOrEmpty');
72
        }
73
        return $res;
74
    }
75
}