Passed
Push — develop ( 31ebc6...f6a1e0 )
by Nikolay
04:27
created

AdvicesProcessor::checkStorage()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 23
rs 9.1111
c 0
b 0
f 0
cc 6
nc 8
nop 0
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;
21
22
use MikoPBX\Common\Providers\ManagedCacheProvider;
23
use MikoPBX\Core\Workers\WorkerPrepareAdvices;
24
use Phalcon\Di\Injectable;
25
26
27
/**
28
 * Class AdvicesProcessor
29
 *
30
 * @package MikoPBX\PBXCoreREST\Lib
31
 *
32
 * @property \MikoPBX\Common\Providers\MarketPlaceProvider license
33
 * @property \MikoPBX\Common\Providers\TranslationProvider translation
34
 * @property \Phalcon\Config config
35
 *
36
 */
37
class AdvicesProcessor extends Injectable
38
{
39
    /**
40
     * Processes the Advices request.
41
     *
42
     * @param array $request The request data.
43
     *
44
     * @return PBXApiResult An object containing the result of the API call.
45
     */
46
    public static function callBack(array $request): PBXApiResult
47
    {
48
        $res = new PBXApiResult();
49
        $res->processor = __METHOD__;
50
        $action = $request['action'];
51
        if ('getList' === $action) {
52
            $proc = new self();
53
            $res = $proc->getAdvicesAction();
54
        } else {
55
            $res->messages[] = "Unknown action - {$action} in advicesCallBack";
56
        }
57
        $res->function = $action;
58
        return $res;
59
    }
60
61
    /**
62
     * Generates a list of notifications about the system, firewall, passwords, and wrong settings.
63
     *
64
     * @return PBXApiResult An object containing the result of the API call.
65
     */
66
    private function getAdvicesAction(): PBXApiResult
67
    {
68
        $res = new PBXApiResult();
69
        $res->processor = __METHOD__;
70
        $res->success = true;
71
        $result = [];
72
        $managedCache = $this->di->getShared(ManagedCacheProvider::SERVICE_NAME);
73
        foreach (WorkerPrepareAdvices::ARR_ADVICE_TYPES as $adviceType) {
74
            $cacheKey = WorkerPrepareAdvices::getCacheKey($adviceType['type']);
75
            $advices = $managedCache->get($cacheKey) ?? [];
76
            foreach ($advices as $key => $messages) {
77
                if (!isset($result[$key])) {
78
                    $result[$key] = [];
79
                }
80
                foreach ($messages as $message) {
81
                    if (isset($message['messageTpl'])) {
82
                        if (array_key_exists('messageParams', $message)) {
83
                            $advice = $this->translation->_($message['messageTpl'], $message['messageParams']);
84
                        } else {
85
                            $advice = $this->translation->_($message['messageTpl']);
86
                        }
87
                        $result[$key] = array_merge($result[$key], [$advice]);
88
                    }
89
                }
90
                if ($key === 'needUpdate') {
91
                    $result[$key] = array_merge($result[$key], $messages);
92
                }
93
            }
94
        }
95
        $res->data['advices'] = $result;
96
        return $res;
97
    }
98
99
}