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

GetModuleInfo::main()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 56
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 56
rs 8.6417
c 1
b 0
f 0
cc 6
nc 14
nop 1

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