Passed
Push — develop ( 9c687a...5ee0bb )
by Nikolay
06:12 queued 13s
created

MusicOnHoldConf::checkMohFiles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 19
rs 9.7333
c 1
b 0
f 0
cc 4
nc 4
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\Asterisk\Configs;
21
22
use MikoPBX\Common\Models\SoundFiles;
23
use MikoPBX\Core\System\Processes;
24
use MikoPBX\Core\System\Util;
25
use MikoPBX\PBXCoreREST\Lib\SystemManagementProcessor;
26
27
class MusicOnHoldConf extends CoreConfigClass
28
{
29
    protected string $description = 'musiconhold.conf';
30
31
    protected function generateConfigProtected(): void
32
    {
33
        $mohpath    = $this->config->path('asterisk.mohdir');
34
35
        $conf = "[default]\n" .
36
            "mode=files\n" .
37
            "directory=$mohpath\n\n";
38
39
        Util::fileWriteContent($this->config->path('asterisk.astetcdir') . '/musiconhold.conf', $conf);
40
        $this->checkMohFiles();
41
    }
42
43
    /**
44
     * Проверка существования MOH файлов.
45
     */
46
    protected function checkMohFiles():void{
47
        $path    = $this->config->path('asterisk.mohdir');
48
        if( count(glob("{$path}/*")) !== 0 ){
49
            return;
50
        }
51
        $filesList    = glob("/offload/asterisk/sounds/moh/*.mp3");
52
        $cpPath       = Util::which('cp');
53
        foreach ($filesList as $srcFile){
54
            $resultMp3 = "{$path}/".basename($srcFile);
55
            Processes::mwExec("{$cpPath} $srcFile {$resultMp3}");
56
            SystemManagementProcessor::convertAudioFile($resultMp3);
57
            /** @var SoundFiles $sf */
58
            $sf = SoundFiles::findFirst("path='{$resultMp3}'");
59
            if(!$sf){
60
                $sf = new SoundFiles();
61
                $sf->category = SoundFiles::CATEGORY_MOH;
62
                $sf->name     = basename($resultMp3);
63
                $sf->path     = $resultMp3;
64
                $sf->save();
65
            }
66
        }
67
    }
68
}