Passed
Push — develop ( ab978f...578d35 )
by Портнов
04:42
created

UpdateConfigsUpToVer202302161::addNewCodecs()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 19
rs 9.1111
cc 6
nc 10
nop 2
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\FirewallRules;
25
use MikoPBX\Common\Models\NetworkFilters;
26
use MikoPBX\Common\Models\PbxSettings;
27
use MikoPBX\Common\Models\Sip;
28
use MikoPBX\Core\System\Processes;
29
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
30
use MikoPBX\Core\System\Util;
31
use Phalcon\Di\Injectable;
32
33
class UpdateConfigsUpToVer202302161 extends Injectable implements UpgradeSystemConfigInterface
34
{
35
  	public const PBX_VERSION = '2023.2.161';
36
37
	/**
38
     * Class constructor.
39
     */
40
    public function __construct()
41
    {
42
    }
43
44
    /**
45
     * https://github.com/mikopbx/Core/issues/269
46
     */
47
    public function processUpdate():void
48
    {
49
        $availCodecs = [
50
            'JPEG'      => 'jpeg',
51
            'H.261'     => 'h261',
52
            'VP8'       => 'vp8',
53
            'VP9'       => 'vp9',
54
        ];
55
        $this->addNewCodecs($availCodecs, false);
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
}