Passed
Push — develop ( a56058...ddfce4 )
by Nikolay
04:39
created

RemoveAudioFileAction::main()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 37
rs 9.536
cc 4
nc 4
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2024 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\PBXCoreREST\Lib\Files;
21
22
23
use MikoPBX\Core\System\Processes;
24
use MikoPBX\Core\System\Util;
25
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
26
27
/**
28
 * Class RemoveAudioFile
29
 * Delete file from disk by filepath
30
 *
31
 * @package MikoPBX\PBXCoreREST\Lib\Files
32
 */
33
class RemoveAudioFileAction extends \Phalcon\Di\Injectable
34
{
35
    /**
36
     * Delete file from disk by filepath
37
     *
38
     * @param string $filePath
39
     *
40
     * @return PBXApiResult An object containing the result of the API call.
41
     */
42
    public static function main(string $filePath): PBXApiResult
43
    {
44
        $res            = new PBXApiResult();
45
        $res->processor = __METHOD__;
46
        $extension      = Util::getExtensionOfFile($filePath);
47
        if ( ! in_array($extension, ['mp3', 'wav', 'alaw'])) {
48
            $res->success    = false;
49
            $res->messages[] = "It is forbidden to remove the file type $extension.";
50
51
            return $res;
52
        }
53
54
        if ( ! file_exists($filePath)) {
55
            $res->success         = true;
56
            $res->data['message'] = "File '{$filePath}' already deleted";
57
58
            return $res;
59
        }
60
61
        $out = [];
62
63
        $arrDeletedFiles = [
64
            escapeshellarg(Util::trimExtensionForFile($filePath) . ".wav"),
65
            escapeshellarg(Util::trimExtensionForFile($filePath) . ".mp3"),
66
            escapeshellarg(Util::trimExtensionForFile($filePath) . ".alaw"),
67
        ];
68
69
        $rmPath = Util::which('rm');
70
        Processes::mwExec("{$rmPath} -rf " . implode(' ', $arrDeletedFiles), $out);
71
        if (file_exists($filePath)) {
72
            $res->success  = false;
73
            $res->messages = $out;
74
        } else {
75
            $res->success = true;
76
        }
77
78
        return $res;
79
    }
80
}