Passed
Push — develop ( be70e5...169ad6 )
by Портнов
05:09 queued 11s
created

UpdateConfigsUpToVer202221::processUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 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\SoundFiles;
25
use MikoPBX\Core\System\MikoPBXConfig;
26
use MikoPBX\Core\System\Processes;
27
use MikoPBX\Core\System\Storage;
28
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
29
use MikoPBX\Core\System\Util;
30
use Phalcon\Config as ConfigAlias;
31
use Phalcon\Di\Injectable;
32
use SQLite3;
33
use Throwable;
34
35
class UpdateConfigsUpToVer202221 extends Injectable implements UpgradeSystemConfigInterface
36
{
37
    public const PBX_VERSION = '2022.2.1';
38
    private bool $isLiveCD;
39
40
    /**
41
     * Class constructor.
42
     */
43
    public function __construct()
44
    {
45
        $this->isLiveCD      = file_exists('/offload/livecd');
46
    }
47
48
    /**
49
     * Updates system configuration according to new rules
50
     */
51
    public function processUpdate(): void
52
    {
53
        if ($this->isLiveCD) {
54
            return;
55
        }
56
        $this->updateCodecs();
57
    }
58
59
    /**
60
     * Update codecs.
61
     */
62
    private function updateCodecs():void
63
    {
64
        $availCodecs = [
65
            'g729'  => 'G.729',
66
        ];
67
        $this->addNewCodecs($availCodecs);
68
    }
69
70
    /**
71
     * Adds new codecs from $availCodecs array if it doesn't exist
72
     *
73
     * @param array $availCodecs
74
     */
75
    private function addNewCodecs(array $availCodecs): void
76
    {
77
        foreach ($availCodecs as $availCodec => $desc) {
78
            $codecData = Codecs::findFirst('name="' . $availCodec . '"');
79
            if ($codecData === null) {
80
                $codecData = new Codecs();
81
            } elseif ($codecData->description === $desc) {
82
                unset($codecData);
83
                continue;
84
            }
85
            $codecData->name = $availCodec;
86
            $codecData->type        = 'audio';
87
            $codecData->disabled    = '1';
88
            $codecData->description = $desc;
89
            if ( ! $codecData->save()) {
90
                Util::sysLogMsg(
91
                    __CLASS__,
92
                    'Can not update codec info ' . $codecData->name . ' from \MikoPBX\Common\Models\Codecs',
93
                    LOG_ERR
94
                );
95
            }
96
        }
97
    }
98
}