Passed
Push — develop ( 3ec4e8...f04914 )
by Портнов
04:58
created

IncomingRoutingTable::resetDefaultRoute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 0
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\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
     * Name of the routing rule
40
     *
41
     * @Column(type="string", nullable=true)
42
     */
43
    public ?string $rulename = '';
44
45
    /**
46
     * DID (Direct Inward Dialing) of the call.
47
     *
48
     * @Column(type="string", nullable=true)
49
     */
50
    public ?string $number = '';
51
52
    /**
53
     * Extension to which the call will be forwarded if this rule is triggered
54
     *
55
     * @Column(type="string", nullable=true)
56
     */
57
    public ?string $extension = '';
58
59
    /**
60
     * Provider associated with the routing rule
61
     *
62
     * @Column(type="string", nullable=true)
63
     */
64
    public ?string $provider = '';
65
66
    /**
67
     * Priority level of the routing rule
68
     *
69
     * @Column(type="integer", nullable=true)
70
     */
71
    public ?string $priority = '0';
72
73
    /**
74
     * Timeout, in seconds, during which the system will attempt to reach the internal extension.
75
     * After the timeout expires, the call will be redirected to a rule with a higher priority or to the default rule
76
     *
77
     * @Column(type="integer", nullable=true)
78
     */
79
    public ?string $timeout = '30';
80
81
    /**
82
     * Action to be taken for the routing rule
83
     *
84
     * @Column(type="string", nullable=true)
85
     */
86
    public ?string $action = '';
87
88
    /**
89
     * Additional note or description for the routing rule
90
     *
91
     * @Column(type="string", nullable=true)
92
     */
93
    public ?string $note = '';
94
95
    /**
96
     * Resets default rule to busy action
97
     */
98
    public static function resetDefaultRoute(): IncomingRoutingTable
99
    {
100
        $defaultRule = self::find('priority=9999');
101
        foreach ($defaultRule as $rule) {
102
            $rule->delete();
103
        }
104
        $defaultRule = self::findFirstById(1);
105
        if ($defaultRule === null) {
106
            $defaultRule = new self();
107
            $defaultRule->id = 1;
108
        }
109
        $defaultRule->action = 'busy';
110
        $defaultRule->priority = 9999;
111
        $defaultRule->rulename = 'default action';
112
        $defaultRule->save();
113
        return $defaultRule;
114
    }
115
116
    /**
117
     * Initialize the model.
118
     */
119
    public function initialize(): void
120
    {
121
        $this->setSource('m_IncomingRoutingTable');
122
        parent::initialize();
123
        $this->belongsTo(
124
            'extension',
125
            Extensions::class,
126
            'number',
127
            [
128
                'alias' => 'Extensions',
129
                'foreignKey' => [
130
                    'allowNulls' => false,
131
                    'action' => Relation::NO_ACTION,
132
                ],
133
            ]
134
        );
135
136
        $this->belongsTo(
137
            'provider',
138
            Providers::class,
139
            'uniqid',
140
            [
141
                'alias' => 'Providers',
142
                'foreignKey' => [
143
                    'allowNulls' => true,
144
                    'action' => Relation::NO_ACTION,
145
                ],
146
            ]
147
        );
148
149
        $this->hasOne(
150
            'id',
151
            OutWorkTimesRouts::class,
152
            'routId',
153
            [
154
                'alias' => 'OutWorkTimesRouts',
155
                'foreignKey' => [
156
                    'allowNulls' => false,
157
                    'action' => Relation::ACTION_CASCADE,
158
                ],
159
            ]
160
        );
161
    }
162
163
    /**
164
     * Returns the maximum priority value of +1
165
     * @return int
166
     */
167
    public static function getMaxNewPriority():int
168
    {
169
        $parameters = [
170
            'column' => 'priority',
171
            'conditions'=>'id!=1'
172
        ];
173
        return (int)IncomingRoutingTable::maximum($parameters)+1;
174
    }
175
176
}
177
178