Passed
Push — develop ( a24ba0...f685b5 )
by Nikolay
05:57 queued 35s
created

UpdateAll   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
dl 0
loc 32
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A main() 0 25 3
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\Common\Handlers\CriticalErrorsHandler;
23
use MikoPBX\Common\Models\PbxExtensionModules;
24
use MikoPBX\Core\System\Util;
25
26
/**
27
 * Class UpdateAll
28
 *
29
 * @package MikoPBX\PBXCoreREST\Lib\Modules
30
 */
31
class UpdateAll extends \Phalcon\Di\Injectable
32
{
33
34
    /**
35
     * Update all installed modules
36
     * @return void
37
     */
38
    public static function main(): void
39
    {
40
        // Get a list of installed modules
41
        $parameters=[
42
            'columns'=>['uniqid']
43
        ];
44
        $installedModules = PbxExtensionModules::find($parameters);
45
46
        // Calculate total mutex timeout and extra 5 seconds to prevent installing the same module in the second thread
47
        $installationTimeout = InstallFromRepo::DOWNLOAD_TIMEOUT+InstallFromRepo::INSTALLATION_TIMEOUT+5;
48
        $mutexTimeout = $installedModules->count()*($installationTimeout);
49
        // Create a mutex to ensure synchronized access
50
        $mutex = Util::createMutex('UpdateAll', 'singleThread', $mutexTimeout);
51
52
        // Synchronize the update process
53
        try{
54
            $mutex->synchronized(
55
                function () use ($installedModules): void {
56
                    // Cycle by them and call install from repository
57
                    foreach ($installedModules as $module) {
58
                        InstallFromRepo::main($module['uniqid']);
59
                    }
60
                });
61
        } catch (\Throwable $e) {
62
            CriticalErrorsHandler::handleExceptionWithSyslog($e);
63
        }
64
    }
65
}