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

SendMetricsAction::main()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 43
rs 9.584
cc 3
nc 3
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\Extensions;
23
use MikoPBX\Common\Models\PbxSettings;
24
use MikoPBX\Common\Models\PbxSettingsConstants;
25
use MikoPBX\Common\Providers\ManagedCacheProvider;
26
use MikoPBX\Common\Providers\MarketPlaceProvider;
27
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
28
use Phalcon\Di;
29
30
/**
31
 * Class SendMetricsAction
32
 * Sends PBX metrics to the license server.
33
 * @package MikoPBX\PBXCoreREST\Lib\Modules
34
 */
35
class SendMetricsAction extends \Phalcon\Di\Injectable
36
{
37
    /**
38
     * Sends PBX metrics to the license server.
39
     *
40
     * @return PBXApiResult An object containing the result of the API call.
41
     */
42
    public static function main(): PBXApiResult
43
    {
44
        $res = new PBXApiResult();
45
        $res->processor = __METHOD__;
46
        $res->success = true;
47
        $di = Di::getDefault();
48
49
        $managedCache = $di->get(ManagedCacheProvider::SERVICE_NAME);
50
        $cacheKey = 'PBXCoreREST:LicenseManagementProcessor:sendMetricsAction';
51
52
        // Retrieve the last sent metrics timestamp from the cache
53
        $lastSend = $managedCache->get($cacheKey);
54
        if ($lastSend === null) {
55
56
            // License Key
57
            $licenseKey = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_LICENSE);
58
            if (empty($licenseKey)){
59
                return $res;
60
            }
61
62
            // Store the current timestamp in the cache to track the last repository check
63
            $managedCache->set($cacheKey, time(), 86400); // Not often than once a day
64
65
            $dataMetrics = [];
66
67
            // PBXVersion
68
            $dataMetrics['PBXname'] = 'MikoPBX@' . PbxSettings::getValueByKey(PbxSettingsConstants::PBX_VERSION);
69
70
            // SIP Extensions count
71
            $extensions = Extensions::find('type="' . Extensions::TYPE_SIP . '"');
72
            $dataMetrics['CountSipExtensions'] = $extensions->count();
73
74
            // Interface language
75
            $dataMetrics['WebAdminLanguage'] = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LANGUAGE);
76
77
            // PBX language
78
            $dataMetrics['PBXLanguage'] = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_LANGUAGE);
79
80
            $license = $di->get(MarketPlaceProvider::SERVICE_NAME);
81
            $license->sendLicenseMetrics($licenseKey, $dataMetrics);
82
        }
83
84
        return $res;
85
    }
86
}