Passed
Push — develop ( d617cb...49fd37 )
by Nikolay
05:15 queued 36s
created

GetModuleLink   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

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

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