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

Common   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getMetadataFromModuleFile() 0 26 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\PBXCoreREST\Lib\PBXApiResult;
23
use ZipArchive;
24
25
/**
26
 *  Common methods for module manipulation
27
 *
28
 * @package MikoPBX\PBXCoreREST\Lib\Modules
29
 */
30
class Common extends \Phalcon\Di\Injectable
31
{
32
    /**
33
     * Unpacks a module ZIP file and retrieves metadata information from the JSON config inside.
34
     *
35
     * @param string $filePath The file path of the module.
36
     *
37
     * @return PBXApiResult An object containing the result of the API call.
38
     */
39
    public static function getMetadataFromModuleFile(string $filePath): PBXApiResult
40
    {
41
        $res = new PBXApiResult();
42
        $res->processor = __METHOD__;
43
44
        if (file_exists($filePath)) {
45
            $moduleUniqueID = false;
46
            $zip = new ZipArchive();
47
            if ($zip->open($filePath) === TRUE) {
48
                $out = $zip->getFromName('module.json');
49
                $zip->close();
50
                $settings       = json_decode($out, true);
51
                $moduleUniqueID = $settings['moduleUniqueID'] ?? null;
52
            }
53
            if (!$moduleUniqueID) {
54
                $res->messages[] = 'The" moduleUniqueID " in the module file is not described.the json or file does not exist.';
55
                return $res;
56
            }
57
            $res->success = true;
58
            $res->data = [
59
                'filePath' => $filePath,
60
                'uniqid'   => $moduleUniqueID,
61
            ];
62
        }
63
64
        return $res;
65
    }
66
}