Passed
Pull Request — master (#16)
by Nikolay
13:10 queued 02:12
created

QueueConf::generateConfigProtected()   F

Complexity

Conditions 17
Paths 12289

Size

Total Lines 64
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 64
rs 1.0499
cc 17
nc 12289
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Copyright © MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Alexey Portnov, 9 2020
7
 */
8
9
namespace MikoPBX\Core\Asterisk\Configs;
10
11
use MikoPBX\Common\Models\CallQueues;
12
use MikoPBX\Common\Models\Extensions;
13
use MikoPBX\Modules\Config\ConfigClass;
14
use MikoPBX\Core\System\{Util};
15
16
class QueueConf extends ConfigClass
17
{
18
    protected string $description = 'queues.conf';
19
20
21
    /**
22
     * Создание конфига для очередей.
23
     *
24
     *
25
     * @return void
26
     */
27
    protected function generateConfigProtected() :void
28
    {
29
        $this->extensionGenInternal();
30
        // Генерация конфигурационных файлов.
31
        $q_conf = '';
32
        $db_data = $this->getQueueData();
33
        foreach ($db_data as $queue_data) {
34
            $joinempty        = (isset($queue_data['joinempty']) && $queue_data['joinempty'] == 1) ? 'yes' : 'no';
35
            $leavewhenempty   = (isset($queue_data['leavewhenempty']) && $queue_data['leavewhenempty'] == 1) ? 'yes' : 'no';
36
            $ringinuse        = ($queue_data['recive_calls_while_on_a_call'] == 1) ? 'yes' : 'no';
37
            $announceposition = ($queue_data['announce_position'] == 1) ? 'yes' : 'no';
38
            $announceholdtime = ($queue_data['announce_hold_time'] == 1) ? 'yes' : 'no';
39
40
            $timeout           = ($queue_data['seconds_to_ring_each_member'] == '') ? '60' : $queue_data['seconds_to_ring_each_member'];
41
            $wrapuptime        = ($queue_data['seconds_for_wrapup'] == '') ? '3' : $queue_data['seconds_for_wrapup'];
42
            $periodic_announce = '';
43
            if (trim($queue_data['periodic_announce']) != '') {
44
                $announce_file     = Util::trimExtensionForFile($queue_data['periodic_announce']);
45
                $periodic_announce = "periodic-announce={$announce_file} \n";
46
            }
47
            $periodic_announce_frequency = '';
48
            if (trim($queue_data['periodic_announce_frequency']) != '') {
49
                $periodic_announce_frequency = "periodic-announce-frequency={$queue_data['periodic_announce_frequency']} \n";
50
            }
51
            $announce_frequency = '';
52
            if ($announceposition !== 'no' || $announceholdtime !== 'no') {
53
                $announce_frequency .= "announce-frequency=30 \n";
54
            }
55
56
            // liner - под этой стратегией понимаем последовательный вызов агентов очереди.
57
            // Каждый новый звонок должен инициировать последовательный вызов начиная с первого агента.
58
            // $strategy = ('linear' === $queue_data['strategy']) ? 'ringall' : $queue_data['strategy'];
59
            $strategy = $queue_data['strategy'];
60
61
            $q_conf .= "[{$queue_data['uniqid']}]; {$queue_data['name']}\n";
62
            $q_conf .= "musicclass=default \n";
63
            $q_conf .= "strategy={$strategy} \n";
64
            $q_conf .= "timeout={$timeout} \n";
65
            $q_conf .= "retry=1 \n";
66
            $q_conf .= "wrapuptime={$wrapuptime} \n";
67
            $q_conf .= "ringinuse={$ringinuse} \n";
68
            $q_conf .= "$periodic_announce";
69
            $q_conf .= "$periodic_announce_frequency";
70
            $q_conf .= "joinempty={$joinempty} \n";
71
            $q_conf .= "leavewhenempty={$leavewhenempty} \n";
72
            $q_conf .= "announce-position={$announceposition} \n";
73
            $q_conf .= "announce-holdtime={$announceholdtime} \n";
74
            $q_conf .= "$announce_frequency";
75
76
            $penalty = 0;
77
            foreach ($queue_data['agents'] as $agent) {
78
                // if ('linear' === $queue_data['strategy']) {
79
                //     $penalty++;
80
                // }
81
                $hint = '';
82
                if ($agent['isExternal'] != true) {
83
                    $hint = ",hint:{$agent['agent']}@internal-hints";
84
                }
85
                $q_conf .= "member => Local/{$agent['agent']}@internal/n,{$penalty},\"{$agent['agent']}\"{$hint} \n";
86
            }
87
            $q_conf .= "\n";
88
        }
89
90
        Util::fileWriteContent($this->config->path('asterisk.astetcdir') . '/queues.conf', $q_conf);
91
92
    }
93
94
    /**
95
     * Получение настроек очередей.
96
     *
97
     * @return array
98
     */
99
    public function getQueueData(): array
100
    {
101
        $arrResult = [];
102
        $queues    = CallQueues::find();
103
        foreach ($queues as $queue) {
104
            $queueUniqid = $queue->uniqid; // идентификатор очереди
105
106
            $arrAgents = [];
107
            $agents    = $queue->CallQueueMembers;
108
            foreach ($agents as $agent) {
109
                $arrAgents[] =
110
                    [
111
                        'agent'      => $agent->extension,
112
                        'priority'   => $agent->priority,
113
                        'isExternal' => ($agent->Extensions->type === Extensions::TYPE_EXTERNAL),
114
                    ];
115
            }
116
            $arrResult[$queueUniqid]['agents'] = $arrAgents;
117
            $periodic_announce                 = '';
118
            if ($queue->SoundFiles != false) {
119
                $periodic_announce = $queue->SoundFiles->path;
120
            }
121
            $arrResult[$queueUniqid]['periodic_announce'] = $periodic_announce;
122
123
            foreach ($queue as $key => $value) {
124
                if ($key == 'callqueuemembers' || $key == "soundfiles") {
125
                    continue;
126
                } // эти параметры мы собрали по-своему
127
                $arrResult[$queueUniqid][$key] = $value;
128
            }
129
        }
130
131
        return $arrResult; // JSON_PRETTY_PRINT
132
    }
133
134
    /**
135
     * Возвращает дополнительные контексты для Очереди.
136
     *
137
     * @return string
138
     */
139
    public function extensionGenContexts(): string
140
    {
141
        // Генерация внутреннего номерного плана.
142
        $conf = "[queue_agent_answer]\n";
143
        $conf .= "exten => s,1,NoOp(--- Answer Queue ---)\n\t";
144
        $conf .= 'same => n,Gosub(queue_answer,${EXTEN},1)' . "\n\t";
145
        $conf .= "same => n,Return()\n\n";
146
147
        return $conf;
148
    }
149
150
    /**
151
     * Генерация хинтов.
152
     *
153
     * @return string
154
     */
155
    public function extensionGenHints(): string
156
    {
157
        $conf = '';
158
        $db_data = $this->getQueueData();
159
        foreach ($db_data as $queue) {
160
            $conf .= "exten => {$queue['extension']},hint,Custom:{$queue['extension']} \n";
161
        }
162
163
        return $conf;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function extensionGenInternalTransfer(): string
170
    {
171
        $conf = '';
172
        $db_data = $this->getQueueData();
173
        foreach ($db_data as $queue) {
174
            $conf .= 'exten => _' . $queue['extension'] . ',1,Set(__ISTRANSFER=transfer_)' . " \n\t";
175
            $conf .= 'same => n,Goto(internal,${EXTEN},1)' . " \n";
176
        }
177
        $conf .= "\n";
178
179
        return $conf;
180
    }
181
182
183
184
    /**
185
     * Возвращает номерной план для internal контекста.
186
     *
187
     * @return string
188
     */
189
    public function extensionGenInternal(): string
190
    {
191
        $queue_ext_conf = '';
192
        $db_data = $this->getQueueData();
193
        foreach ($db_data as $queue) {
194
            $calleridPrefix = preg_replace('/[^a-zA-Zа-яА-Я0-9 ]/ui', '', $queue['callerid_prefix']??'');
195
196
            $queue_ext_conf .= "exten => {$queue['extension']},1,NoOp(--- Start Queue ---) \n\t";
197
            $queue_ext_conf .= "same => n,Answer() \n\t";
198
            $queue_ext_conf .= 'same => n,Set(__QUEUE_SRC_CHAN=${CHANNEL})' . "\n\t";
199
            $queue_ext_conf .= 'same => n,ExecIf($["${CHANNEL(channeltype)}" == "Local"]?Gosub(set_orign_chan,s,1))' . "\n\t";
200
            $queue_ext_conf .= 'same => n,Set(CHANNEL(hangup_handler_wipe)=hangup_handler,s,1)' . "\n\t";
201
            $queue_ext_conf .= 'same => n,Gosub(queue_start,${EXTEN},1)' . "\n\t";
202
203
            $options = '';
204
            if (isset($queue['caller_hear']) && $queue['caller_hear'] === 'ringing') {
205
                $options .= 'r'; // Установить КПВ (гудки) вместо Музыки на Удержании для ожидающих в очереди
206
            }
207
            $ringlength     = (trim($queue['timeout_to_redirect_to_extension']) == '') ? 300 : $queue['timeout_to_redirect_to_extension'];
208
            if(!empty($calleridPrefix)){
209
                $queue_ext_conf .= "same => n,Set(CALLERID(name)={$calleridPrefix}:".'${CALLERID(name)}'.") \n\t";
210
            }
211
212
            $queue_ext_conf .= "same => n,Queue({$queue['uniqid']},CkT{$options},,,{$ringlength},,,queue_agent_answer) \n\t";
213
            // Оповестим о завершении работы очереди.
214
            $queue_ext_conf .= 'same => n,Gosub(queue_end,${EXTEN},1)' . "\n\t";
215
216
            if (trim($queue['timeout_extension']) !== '') {
217
                // Если по таймауту не ответили, то выполним переадресацию.
218
                $queue_ext_conf .= 'same => n,ExecIf($["${QUEUESTATUS}" == "TIMEOUT"]?Goto(internal,' . $queue['timeout_extension'] . ',1))' . " \n\t";
219
            }
220
            if (trim($queue['redirect_to_extension_if_empty']) !== '') {
221
                // Если пустая очередь, то выполним переадресацию.
222
                $exp            = '$["${QUEUESTATUS}" == "JOINEMPTY" || "${QUEUESTATUS}" == "LEAVEEMPTY" ]';
223
                $queue_ext_conf .= 'same => n,ExecIf(' . $exp . '?Goto(internal,' . $queue['redirect_to_extension_if_empty'] . ',1))' . " \n\t";
224
            }
225
            $queue_ext_conf .= "\n";
226
        }
227
228
        return $queue_ext_conf;
229
    }
230
231
    /**
232
     * Generates queue.conf and restart asterisk queue module
233
     */
234
    public static function queueReload(): void
235
    {
236
        $queue            = new self();
237
        $queue->generateConfig();
238
        $out = [];
239
        $asteriskPath = Util::which('asterisk');
240
        Util::mwExec("{$asteriskPath} -rx 'queue reload all '", $out);
241
    }
242
}