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

GetModuleInfoAction::main()   B

Complexity

Conditions 7
Paths 21

Size

Total Lines 62
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 46
c 0
b 0
f 0
dl 0
loc 62
rs 8.2448
cc 7
nc 21
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 GetModuleInfoAction  extends \Phalcon\Di\Injectable
21
{
22
    const WRONG_MODULE_INFO = 'Wrong module info';
23
24
25
    /**
26
     * Retrieves module information from repository and store the information in local cache
27
     *
28
     * @param string $moduleUniqueID
29
     * @return PBXApiResult
30
     */
31
    public static function main(string $moduleUniqueID): PBXApiResult
32
    {
33
        $res = new PBXApiResult();
34
        $res->processor = __METHOD__;
35
36
        $di = Di::getDefault();
37
        if ($di === null) {
38
            $res->success    = false;
39
            $res->messages[] = 'Dependency injector does not initialized';
40
            return $res;
41
        }
42
        $WebUiLanguage = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LANGUAGE);
43
        $cacheKey = "ModulesManagementProcessor:GetModuleInfo:$moduleUniqueID:$WebUiLanguage";
44
        $managedCache = $di->getShared(ManagedCacheProvider::SERVICE_NAME);
45
        if ($managedCache->has($cacheKey)){
46
            $body = $managedCache->get($cacheKey);
47
        } else {
48
            $PBXVersion = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_VERSION);
49
            $PBXVersion = (string)str_ireplace('-dev', '', $PBXVersion);
50
            $body = '';
51
            $client = new GuzzleHttp\Client();
52
            try {
53
                $request = $client->request(
54
                    'POST',
55
                    'https://releases.mikopbx.com/releases/v1/mikopbx/getModuleInfo',
56
                    [
57
                        'headers' => [
58
                            'Content-Type' => 'application/json; charset=utf-8',
59
                        ],
60
                        'json' => [
61
                            'PBXVER' => $PBXVersion,
62
                            'LANGUAGE'=> $WebUiLanguage,
63
                            'GUID'=> $moduleUniqueID,
64
                        ],
65
                        'timeout' => 5,
66
                    ]
67
                );
68
                $code = $request->getStatusCode();
69
                if ($code === Response::OK){
70
                    $body = $request->getBody()->getContents();
71
                    $managedCache->set($cacheKey, $body, 3600);
72
                }
73
            } catch (\Throwable $e) {
74
                $code = Response::INTERNAL_SERVER_ERROR;
75
                Util::sysLogMsg(static::class, $e->getMessage());
76
                $res->messages[] = $e->getMessage();
77
            }
78
79
            if ($code !== Response::OK) {
80
                return $res;
81
            }
82
        }
83
        $result =  json_decode($body, true);
84
        if (array_key_exists('data', $result)) {
85
            $res->data = $result['data'];
86
            $res->success = true;
87
        } else {
88
            $res->success    = false;
89
            $res->messages[] = self::WRONG_MODULE_INFO;
90
        }
91
92
        return $res;
93
    }
94
95
}