Passed
Push — develop ( e4768a...6d9d8a )
by Nikolay
04:23
created

PBXConfModulesProvider::getExternalConfModules()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A PBXConfModulesProvider::recreateModulesProvider() 0 5 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 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
declare(strict_types=1);
21
22
namespace MikoPBX\Common\Providers;
23
24
use MikoPBX\Common\Models\PbxExtensionModules;
25
use MikoPBX\Core\System\Util;
26
use MikoPBX\Modules\Config\ConfigClass;
27
use Phalcon\Di;
28
use Phalcon\Di\DiInterface;
29
use Phalcon\Di\ServiceProviderInterface;
30
31
/**
32
 * Main database connection is created based in the parameters defined in the configuration file
33
 */
34
class PBXConfModulesProvider implements ServiceProviderInterface
35
{
36
    public const SERVICE_NAME = 'pbxConfModules';
37
38
    /**
39
     * Registers pbxConfModules service provider
40
     * Creates array of external installed modules
41
     *
42
     * @param \Phalcon\Di\DiInterface $di
43
     */
44
    public function register(DiInterface $di): void
45
    {
46
        $di->setShared(
47
            self::SERVICE_NAME,
48
            function (string $methodName=''){
49
                $additionalModules = [];
50
                $modules = PbxExtensionModules::getEnabledModulesArray();
51
                foreach ($modules as $value) {
52
                    $className      = str_replace('Module', '', $value['uniqid']);
53
                    $fullClassName = "\\Modules\\{$value['uniqid']}\\Lib\\{$className}Conf";
54
                    if (class_exists($fullClassName)) {
55
                        $object = new $fullClassName();
56
                        if ($object instanceof ConfigClass){
57
                            $additionalModules[] = $object;
58
                        }
59
                    }
60
                }
61
62
                // Sort the array based on the priority value for $methodName
63
                usort($additionalModules, function($a, $b) use ($methodName) {
64
                    return $a->getMethodPriority($methodName) - $b->getMethodPriority($methodName);
65
                });
66
                return  $additionalModules;
67
            }
68
        );
69
    }
70
71
    /**
72
     * Recreates modules service after enable or disable them
73
     */
74
    public static function recreateModulesProvider(): void
75
    {
76
        $di = Di::getDefault();
77
        $di->remove(self::SERVICE_NAME);
78
        $di->register(new self());
79
    }
80
81
    /**
82
     * Calls additional module method by name and returns array of results
83
     *
84
     * @param string $methodName
85
     * @param array  $arguments
86
     *
87
     * @return array
88
     */
89
    public static function hookModulesMethod(string $methodName, array $arguments = []): array
90
    {
91
        $result            = [];
92
        $di = Di::getDefault();
93
        $additionalModules = $di->getShared(PBXConfModulesProvider::SERVICE_NAME, ['methodName'=>$methodName]);
94
95
        foreach ($additionalModules as $configClassObj) {
96
            if ( ! method_exists($configClassObj, $methodName)) {
97
                continue;
98
            }
99
            try {
100
                $moduleMethodResponse = call_user_func_array([$configClassObj, $methodName], $arguments);
101
            } catch (\Throwable $e) {
102
                global $errorLogger;
103
                $errorLogger->captureException($e);
104
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
105
                continue;
106
            }
107
            if ( ! empty($moduleMethodResponse)) {
108
                if (is_a($configClassObj, ConfigClass::class)) {
109
                    $result[$configClassObj->moduleUniqueId] = $moduleMethodResponse;
110
                } else {
111
                    $result[] = $moduleMethodResponse;
112
                }
113
            }
114
        }
115
116
        return $result;
117
    }
118
119
}