Passed
Push — develop ( a56058...ddfce4 )
by Nikolay
04:39
created

GetModuleLinkAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 28
c 0
b 0
f 0
dl 0
loc 50
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A main() 0 41 4
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 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\Modules;
21
22
use MikoPBX\Common\Models\PbxSettings;
23
use MikoPBX\Common\Models\PbxSettingsConstants;
24
use MikoPBX\Core\System\Util;
25
use MikoPBX\PBXCoreREST\Http\Response;
26
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
27
use GuzzleHttp;
28
29
/**
30
 *  Class GetModuleLink
31
 *  Retrieves the installation link for a module.
32
 *
33
 * @package MikoPBX\PBXCoreREST\Lib\Modules
34
 */
35
class GetModuleLinkAction extends \Phalcon\Di\Injectable
36
{
37
    /**
38
     * Retrieves the installation link for a module.
39
     *
40
     * @param string $moduleReleaseId The module release unique id retrieved on getAvailableModules
41
     *
42
     * @return PBXApiResult
43
     */
44
    public static function main(string $moduleReleaseId): PBXApiResult
45
    {
46
        $res = new PBXApiResult();
47
        $res->processor = __METHOD__;
48
49
        $licenseKey = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_LICENSE);
50
51
        $client = new GuzzleHttp\Client();
52
        $body = '';
53
        try {
54
            $request = $client->request(
55
                'POST',
56
                'https://releases.mikopbx.com/releases/v1/mikopbx/getModuleLink',
57
                [
58
                    'headers' => [
59
                        'Content-Type' => 'application/json; charset=utf-8',
60
                    ],
61
                    'json' => [
62
                        'LICENSE' => $licenseKey,
63
                        'RELEASEID'=> $moduleReleaseId,
64
                    ],
65
                    'timeout' => 5,
66
                ]
67
            );
68
            $code = $request->getStatusCode();
69
            if ($code === Response::OK){
70
                $body = $request->getBody()->getContents();
71
            }
72
        } catch (\Throwable $e) {
73
            $code = Response::INTERNAL_SERVER_ERROR;
74
            Util::sysLogMsg(static::class, $e->getMessage());
75
            $res->messages[] = $e->getMessage();
76
        }
77
78
        if ($code !== Response::OK) {
79
            return $res;
80
        }
81
82
        $res->data = json_decode($body, true)??[];
83
        $res->success = true;
84
        return $res;
85
    }
86
}