Passed
Push — develop ( c1c7b0...ceba44 )
by Nikolay
05:17
created

createParkingSlots()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 31
rs 9.2568
c 0
b 0
f 0
cc 5
nc 9
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\Core\System\Upgrade\Releases;
21
22
use MikoPBX\Common\Models\Codecs;
23
use MikoPBX\Common\Models\Extensions;
24
use MikoPBX\Common\Models\PbxSettings;
25
use MikoPBX\Common\Models\PbxSettingsConstants;
26
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
27
use MikoPBX\Core\System\Util;
28
use Phalcon\Di\Injectable;
29
30
class UpdateConfigsUpToVer202302161 extends Injectable implements UpgradeSystemConfigInterface
31
{
32
    public const PBX_VERSION = '2023.2.161';
33
34
    /**
35
     * Class constructor.
36
     */
37
    public function __construct()
38
    {
39
    }
40
41
    /**
42
     * https://github.com/mikopbx/Core/issues/269
43
     */
44
    public function processUpdate(): void
45
    {
46
        $availCodecs = [
47
            'JPEG' => 'jpeg',
48
            'H.261' => 'h261',
49
            'VP8' => 'vp8',
50
            'VP9' => 'vp9',
51
        ];
52
        $this->addNewCodecs($availCodecs, false);
53
54
55
        $this->createParkingSlots();
56
    }
57
58
59
    /**
60
     * Adds new codecs from $availCodecs array if it doesn't exist
61
     * @param array $availCodecs
62
     * @param bool $isAudio
63
     * @return void
64
     */
65
    private function addNewCodecs(array $availCodecs, bool $isAudio = true): void
66
    {
67
        foreach ($availCodecs as $availCodec => $desc) {
68
            $codecData = Codecs::findFirst('name="' . $availCodec . '"');
69
            if ($codecData === null) {
70
                $codecData = new Codecs();
71
            } elseif ($codecData->description === $desc) {
72
                unset($codecData);
73
                continue;
74
            }
75
            $codecData->name = $availCodec;
76
            $codecData->type = $isAudio ? 'audio' : 'video';
77
            $codecData->disabled = '1';
78
            $codecData->description = $desc;
79
            if (!$codecData->save()) {
80
                Util::sysLogMsg(
81
                    __CLASS__,
82
                    'Can not update codec info ' . $codecData->name . ' from \MikoPBX\Common\Models\Codecs',
83
                    LOG_ERR
84
                );
85
            }
86
        }
87
    }
88
89
    /**
90
     * Create parking extensions.
91
     *
92
     * @return void
93
     */
94
    private function createParkingSlots()
95
    {
96
        $messages = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $messages is dead and can be removed.
Loading history...
97
        // Delete all parking slots
98
        $currentSlots = Extensions::findByType(Extensions::TYPE_PARKING);
99
        foreach ($currentSlots as $currentSlot) {
100
            if (!$currentSlot->delete()) {
101
                Util::sysLogMsg(
102
                    __CLASS__,
103
                    'Can not delete extenison ' . $currentSlot->number . ' from \MikoPBX\Common\Models\Extensions ' . implode($currentSlot->getMessages()),
104
                    LOG_ERR
105
                );
106
            }
107
        }
108
109
        $startSlot = intval(PbxSettings::getValueByKey(PbxSettingsConstants::PBX_CALL_PARKING_START_SLOT));
110
        $endSlot = intval(PbxSettings::getValueByKey(PbxSettingsConstants::PBX_CALL_PARKING_END_SLOT));
111
        $reservedSlot = intval(PbxSettings::getValueByKey(PbxSettingsConstants::PBX_CALL_PARKING_EXT));
112
113
        // Create an array of new numbers
114
        $numbers = range($startSlot, $endSlot);
115
        $numbers[] = $reservedSlot;
116
        foreach ($numbers as $number) {
117
            $record = new Extensions();
118
            $record->type = Extensions::TYPE_PARKING;
119
            $record->number = $number;
120
            if (!$record->create()) {
121
                Util::sysLogMsg(
122
                    __CLASS__,
123
                    'Can not create extenison ' . $record->number . ' from \MikoPBX\Common\Models\Extensions ' . implode($record->getMessages()),
124
                    LOG_ERR
125
                );
126
            }
127
        }
128
    }
129
}