Passed
Push — develop ( 368e3d...115c7b )
by Nikolay
12:16
created

GetAdviceAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 25
c 0
b 0
f 0
dl 0
loc 40
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B main() 0 33 8
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2024 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\Advice;
21
22
23
use MikoPBX\Common\Providers\ManagedCacheProvider;
24
use MikoPBX\Common\Providers\TranslationProvider;
25
use MikoPBX\Core\Workers\WorkerPrepareAdvice;
26
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
27
use Phalcon\Di;
28
29
/**
30
 * Get active calls based on CDR data.
31
 *
32
 * @package MikoPBX\PBXCoreREST\Lib\Advice
33
 */
34
class GetAdviceAction extends \Phalcon\Di\Injectable
35
{
36
    /**
37
     * Generates a list of notifications about the system, firewall, passwords, and wrong settings.
38
     *
39
     * @return PBXApiResult An object containing the result of the API call.
40
     */
41
    public static function main(): PBXApiResult
42
    {
43
        $res = new PBXApiResult();
44
        $res->processor = __METHOD__;
45
        $res->success = true;
46
        $result = [];
47
        $di = Di::getDefault();
48
        $translation = $di->get(TranslationProvider::SERVICE_NAME);
49
        $managedCache = $di->getShared(ManagedCacheProvider::SERVICE_NAME);
50
        foreach (WorkerPrepareAdvice::ARR_ADVICE_TYPES as $adviceType) {
51
            $cacheKey = WorkerPrepareAdvice::getCacheKey($adviceType['type']);
52
            $advice = $managedCache->get($cacheKey) ?? [];
53
            foreach ($advice as $key => $messages) {
54
                if (!isset($result[$key])) {
55
                    $result[$key] = [];
56
                }
57
                foreach ($messages as $message) {
58
                    if (isset($message['messageTpl'])) {
59
                        if (array_key_exists('messageParams', $message)) {
60
                            $advice = $translation->_($message['messageTpl'], $message['messageParams']);
61
                        } else {
62
                            $advice = $translation->_($message['messageTpl']);
63
                        }
64
                        $result[$key] = array_merge($result[$key], [$advice]);
65
                    }
66
                }
67
                if ($key === 'needUpdate') {
68
                    $result[$key] = array_merge($result[$key], $messages);
69
                }
70
            }
71
        }
72
        $res->data['advice'] = $result;
73
        return $res;
74
    }
75
}