Passed
Push — develop ( 719c22...1c19cc )
by Nikolay
12:41 queued 11s
created

IncomingRoutingTable::resetDefaultRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 12
rs 9.9666
cc 2
nc 2
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\Common\Models;
21
22
use Phalcon\Mvc\Model\Relation;
23
24
/**
25
 * Class IncomingRoutingTable
26
 *
27
 * @package MikoPBX\Common\Models
28
 */
29
class IncomingRoutingTable extends ModelsBase
30
{
31
    /**
32
     * @Primary
33
     * @Identity
34
     * @Column(type="integer", nullable=false)
35
     */
36
    public $id;
37
38
    /**
39
     * @Column(type="string", nullable=true)
40
     */
41
    public ?string $rulename = '';
42
43
    /**
44
     * @Column(type="string", nullable=true)
45
     */
46
    public ?string $number = '';
47
48
    /**
49
     * @Column(type="string", nullable=true)
50
     */
51
    public ?string $extension = '';
52
53
    /**
54
     * @Column(type="string", nullable=true)
55
     */
56
    public ?string $provider = '';
57
58
    /**
59
     * @Column(type="integer", nullable=true)
60
     */
61
    public ?string $priority = '0';
62
63
    /**
64
     * @Column(type="integer", nullable=true)
65
     */
66
    public ?string $timeout = '30';
67
68
    /**
69
     * @Column(type="string", nullable=true)
70
     */
71
    public ?string $action = '';
72
73
    /**
74
     * @Column(type="string", nullable=true)
75
     */
76
    public ?string $note = '';
77
78
79
    public function initialize(): void
80
    {
81
        $this->setSource('m_IncomingRoutingTable');
82
        parent::initialize();
83
        $this->belongsTo(
84
            'extension',
85
            Extensions::class,
86
            'number',
87
            [
88
                'alias'      => 'Extensions',
89
                'foreignKey' => [
90
                    'allowNulls' => false,
91
                    'action'     => Relation::NO_ACTION,
92
                ],
93
            ]
94
        );
95
96
        $this->belongsTo(
97
            'provider',
98
            Providers::class,
99
            'uniqid',
100
            [
101
                'alias'      => 'Providers',
102
                'foreignKey' => [
103
                    'allowNulls' => true,
104
                    'action'     => Relation::NO_ACTION,
105
                ],
106
            ]
107
        );
108
    }
109
110
    /**
111
     * Resets default rule to busy action
112
     */
113
    public static function resetDefaultRoute(): IncomingRoutingTable
114
    {
115
        $defaultRule = IncomingRoutingTable::findFirstById(1);
116
        if ($defaultRule === null) {
117
            $defaultRule     = new IncomingRoutingTable();
118
            $defaultRule->id = 1;
119
        }
120
        $defaultRule->action   = 'busy';
121
        $defaultRule->priority = 9999;
122
        $defaultRule->rulename = 'default action';
123
        $defaultRule->save();
124
        return $defaultRule;
125
    }
126
127
}
128
129