Passed
Push — develop ( 878b75...3d3778 )
by Nikolay
04:58
created

GetLicenseInfoAction::main()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 36
rs 8.8657
cc 6
nc 5
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\Providers\ManagedCacheProvider;
24
use MikoPBX\Common\Providers\MarketPlaceProvider;
25
use MikoPBX\Common\Providers\TranslationProvider;
26
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
27
use Phalcon\Di\Di;
28
use MikoPBX\Common\Library\Text;
29
use SimpleXMLElement;
30
use Phalcon\Di\Injectable;
31
32
/**
33
 * Class GetLicenseInfoAction
34
 * Returns license info from license server by key.
35
 * @package MikoPBX\PBXCoreREST\Lib\License
36
 */
37
class GetLicenseInfoAction extends 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(PbxSettings::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["success"]) {
61
                    if ($licenseInfo["result"] instanceof SimpleXMLElement) {
62
                        $res->success = true;
63
                        $res->data['licenseInfo'] = json_encode($licenseInfo["result"]);
64
                    }
65
                    // Check not often than every 2 minutes
66
                    $managedCache->set($cacheKey, $res->data['licenseInfo'], 120);
67
                } else {
68
                    $res->success = false;
69
                    $res->messages[] = $licenseInfo["error"];
70
                }
71
            } else {
72
                $res->data['licenseInfo'] = $lastGetLicenseInfo;
73
                $res->success = true;
74
            }
75
        } else {
76
            $translation = $di->get(TranslationProvider::SERVICE_NAME);
77
            $res->messages['error'][] = $translation->_('lic_WrongLicenseKeyOrEmpty');
78
        }
79
        return $res;
80
    }
81
}