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

PingAction::main()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 26
rs 9.8666
cc 4
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\Providers\MarketPlaceProvider;
23
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
24
use Phalcon\Di;
25
26
/**
27
 * Class PingAction
28
 * Sends ping request to the license server.
29
 * @package MikoPBX\PBXCoreREST\Lib\Modules
30
 */
31
class PingAction extends \Phalcon\Di\Injectable
32
{
33
    /**
34
     * Sends ping request to the license server.
35
     *
36
     * @return PBXApiResult An object containing the result of the API call.
37
     */
38
    public static function main(): PBXApiResult
39
    {
40
        $res = new PBXApiResult();
41
        $res->processor = __METHOD__;
42
        $license = Di::getDefault()->get(MarketPlaceProvider::SERVICE_NAME);
43
44
        // Loop up to 3 attempts
45
        for ($attempt = 0; $attempt < 3; $attempt++) {
46
            $result = $license->ping();
47
48
            // Return success at the first successful ping
49
            if ($result['success'] === true) {
50
                $res->success = true;
51
                return $res;
52
            }
53
54
            // Wait for 3 seconds before the next attempt, if the previous one was unsuccessful
55
            // and it's not the last attempt
56
            if ($attempt < 2) {
57
                sleep(3);
58
            }
59
        }
60
61
        // If the code reaches this point, all three attempts have failed
62
        $res->success = false;
63
        return $res;
64
    }
65
}