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

GetAvailableModules::main()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 55
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 55
rs 8.6577
c 1
b 0
f 0
cc 6
nc 14
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MikoPBX\PBXCoreREST\Lib\Modules;
4
5
use MikoPBX\Common\Models\PbxSettings;
6
use MikoPBX\Common\Models\PbxSettingsConstants;
7
use MikoPBX\Common\Providers\ManagedCacheProvider;
8
use MikoPBX\Core\System\Util;
9
use MikoPBX\PBXCoreREST\Http\Response;
10
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
11
use Phalcon\Di;
12
use GuzzleHttp;
13
14
/**
15
 *  Class GetAvailableModules
16
 *  Request new modules form repository and store the information in local cache
17
 *
18
 * @package MikoPBX\PBXCoreREST\Lib\Modules
19
 */
20
class GetAvailableModules  extends \Phalcon\Di\Injectable
21
{
22
    /**
23
     * Retrieves available modules on MIKO repository.
24
     *
25
     * @return PBXApiResult
26
     */
27
    public static function main(): PBXApiResult
28
    {
29
        $res = new PBXApiResult();
30
        $res->processor = __METHOD__;
31
32
        $di = Di::getDefault();
33
        if ($di === null) {
34
            $res->success    = false;
35
            $res->messages[] = 'Dependency injector does not initialized';
36
            return $res;
37
        }
38
        $WebUiLanguage = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LANGUAGE);
39
        $cacheKey = "ModulesManagementProcessor:GetAvailableModules:$WebUiLanguage";
40
        $managedCache = $di->getShared(ManagedCacheProvider::SERVICE_NAME);
41
        if ($managedCache->has($cacheKey)){
42
            $body = $managedCache->get($cacheKey);
43
        } else {
44
            $PBXVersion = PbxSettings::getValueByKey('PBXVersion');
45
            $PBXVersion = (string)str_ireplace('-dev', '', $PBXVersion);
46
            $body = '';
47
            $client = new GuzzleHttp\Client();
48
            try {
49
                $request = $client->request(
50
                    'POST',
51
                    'https://releases.mikopbx.com/releases/v1/mikopbx/getAvailableModules',
52
                    [
53
                        'headers' => [
54
                            'Content-Type' => 'application/json; charset=utf-8',
55
                        ],
56
                        'json' => [
57
                            'PBXVER' => $PBXVersion,
58
                            'LANGUAGE'=> $WebUiLanguage,
59
                        ],
60
                        'timeout' => 5,
61
                    ]
62
                );
63
                $code = $request->getStatusCode();
64
                if ($code === Response::OK){
65
                    $body = $request->getBody()->getContents();
66
                    $managedCache->set($cacheKey, $body, 3600);
67
                }
68
            } catch (\Throwable $e) {
69
                $code = Response::INTERNAL_SERVER_ERROR;
70
                Util::sysLogMsg(static::class, $e->getMessage());
71
                $res->messages[] = $e->getMessage();
72
            }
73
74
            if ($code !== Response::OK) {
75
                return $res;
76
            }
77
        }
78
        $res->data = json_decode($body, true)??[];
79
        $res->success = true;
80
81
        return $res;
82
    }
83
84
}