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

ProcessUserRequestAction   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 49
c 1
b 0
f 0
dl 0
loc 64
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C main() 0 57 12
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\ModelsBase;
23
use MikoPBX\Common\Models\PbxSettings;
24
use MikoPBX\Common\Models\PbxSettingsConstants;
25
use MikoPBX\Common\Providers\MarketPlaceProvider;
26
use MikoPBX\Common\Providers\TranslationProvider;
27
use MikoPBX\Core\System\MikoPBXConfig;
28
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
29
use Phalcon\Di;
30
use Phalcon\Text;
31
use SimpleXMLElement;
32
33
/**
34
 * Class ProcessUserRequestAction
35
 * Check and update license key on database.
36
 * @package MikoPBX\PBXCoreREST\Lib\Modules
37
 */
38
class ProcessUserRequestAction extends \Phalcon\Di\Injectable
39
{
40
    /**
41
     * Check and update a license key on a database.
42
     *
43
     * @return PBXApiResult An object containing the result of the API call.
44
     */
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
}