Passed
Push — develop ( 558982...38f712 )
by Nikolay
06:07
created

DeleteRecord::main()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
c 1
b 0
f 1
dl 0
loc 34
rs 9.2728
cc 5
nc 5
nop 1
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\PBXCoreREST\Lib\DialplanApplications;
21
22
use MikoPBX\Common\Models\DialplanApplications;
23
use MikoPBX\Common\Providers\MainDatabaseProvider;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
use Phalcon\Di;
26
27
/**
28
 *  Class DeleteRecord
29
 *  Delete the dialplan application and all its dependencies.
30
 *
31
 * @package MikoPBX\PBXCoreREST\Lib\DialplanApplications
32
 */
33
class DeleteRecord extends \Phalcon\Di\Injectable
34
{
35
36
    /**
37
     * Deletes the dialplan application record with its dependent tables.
38
     *
39
     * @param string $id The ID of the dialplan application to be deleted.
40
     * @return PBXApiResult Result of the delete operation.
41
     */
42
    public static function main(string $id): PBXApiResult
43
    {
44
        $res = new PBXApiResult();
45
        $res->processor = __METHOD__;
46
        $res->success = true;
47
48
        $di = Di::getDefault();
49
        $db = $di->get(MainDatabaseProvider::SERVICE_NAME);
50
51
        // Find the queue by ID
52
        $record = DialplanApplications::findFirstByUniqid($id);
53
        if ($record===null){
54
            $res->messages['error'][] = 'Dialplan application with id '.$id.' does not exist';
55
            $res->success = false;
56
            return  $res;
57
        }
58
59
        $db->begin();
60
61
        // Delete associated extensions
62
        $extension = $record->Extensions;
63
        if ($extension!==null && !$extension->delete()) {
64
            $res->messages['error'][] = implode(PHP_EOL, $extension->getMessages());
65
            $res->success = false;
66
        }
67
68
        if (!$res->success) {
69
            $db->rollback();
70
        } else {
71
            $db->commit();
72
        }
73
74
        $res->data['id'] = $id;
75
        return $res;
76
    }
77
78
}