Passed
Push — develop ( 578d35...1edb03 )
by Nikolay
13:40 queued 12s
created

DeleteRecord::main()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 41
rs 8.5866
cc 7
nc 9
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\Extensions;
21
22
use MikoPBX\Common\Models\Extensions;
23
use MikoPBX\Common\Providers\MainDatabaseProvider;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
use Phalcon\Di;
26
27
/**
28
 *  Class DeleteRecord
29
 *  Delete an internal number and all its dependencies including mobile and forwarding settings.
30
 *
31
 * @package MikoPBX\PBXCoreREST\Lib\Extensions
32
 */
33
class DeleteRecord extends \Phalcon\Di\Injectable
34
{
35
36
    /**
37
     * Deletes the extension record with its dependent tables.
38
     *
39
     * @param string $id ID of the user 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
        $extension = Extensions::findFirstById($id);
52
        if ($extension===null){
53
            $res->messages['error'][] = 'Extension with id '.$id.' does not exist';
54
            $res->success = false;
55
            return  $res;
56
        }
57
58
        $db->begin();
59
60
        // To avoid circular references, we first delete the forwarding settings
61
        // for this account, as it may refer to itself.
62
        $forwardingRights = $extension->ExtensionForwardingRights;
63
        if ($forwardingRights!==null && !$forwardingRights->delete()) {
64
            $res->messages['error'][] = implode(PHP_EOL,$forwardingRights->getMessages());
65
            $res->success = false;
66
        }
67
68
        // Delete User
69
        $user = $extension->Users;
70
        if ($user !==null && !$user->delete()) {
71
            $res->messages['error'][] = implode(PHP_EOL, $user->getMessages());
72
            $res->success = false;
73
        }
74
75
        if (!$res->success) {
76
            $db->rollback();
77
        } else {
78
            $db->commit();
79
        }
80
81
        $res->data['id'] = $id;
82
        return $res;
83
    }
84
85
}