Passed
Push — develop ( 9c9440...cdbcb8 )
by Портнов
04:16
created

UpdateConfigsUpToVer2020362::updateSipHosts()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 20
rs 9.7666
c 1
b 0
f 0
cc 4
nc 4
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\Core\System\Upgrade\Releases;
21
22
use MikoPBX\Common\Models\CallQueueMembers;
23
use MikoPBX\Common\Models\Iax;
24
use MikoPBX\Common\Models\IvrMenuActions;
25
use MikoPBX\Common\Models\Sip;
26
use MikoPBX\Common\Models\SipHosts;
27
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
28
use MikoPBX\Core\System\Util;
29
use Phalcon\Di\Injectable;
30
use MikoPBX\Core\System\MikoPBXConfig;
31
use Phalcon\Config as ConfigAlias;
32
33
class UpdateConfigsUpToVer2020362 extends Injectable implements UpgradeSystemConfigInterface
34
{
35
  	public const PBX_VERSION = '2020.3.62';
36
37
	private ConfigAlias $config;
38
    private MikoPBXConfig $mikoPBXConfig;
39
    private bool $isLiveCD;
40
41
	/**
42
     * Class constructor.
43
     */
44
    public function __construct()
45
    {
46
        $this->config = $this->getDI()->getShared('config');
47
        $this->mikoPBXConfig = new MikoPBXConfig();
48
        $this->isLiveCD      = file_exists('/offload/livecd');
49
    }
50
51
	/**
52
     * Main function
53
     */
54
    public function processUpdate():void
55
    {
56
        if ($this->isLiveCD) {
57
            return;
58
        }
59
        $this->deleteOrphanedProviders();
60
        $this->deleteOrphanedQueueMembers();
61
        $this->deleteOrphanedIVRMenuActions();
62
        $this->updateSipHosts();
63
    }
64
65
    private function updateSipHosts():void{
66
        /** @var Sip $data */
67
        $db_data    = Sip::find("type = 'friend' AND ( disabled <> '1')");
68
        foreach ($db_data as $data){
69
            $parameters    = [
70
                'conditions' => 'provider_id=:provider: AND address=:address:',
71
                'bind'       => [
72
                    'provider' => $data->uniqid,
73
                    'address'  => $data->host,
74
                ],
75
            ];
76
            $hostData = SipHosts::findFirst($parameters);
77
            if($hostData){
78
                continue;
79
            }
80
            $hostData = new SipHosts();
81
            $hostData->provider_id = $data->uniqid;
82
            $hostData->address = $data->host;
83
            if(!$hostData->save()){
84
                Util::sysLogMsg(self::class, 'Error save SipHosts', LOG_ERR);
85
            }
86
        }
87
    }
88
89
    /**
90
     * Deletes m_Sip and m_Iax records without links to m_Providers and m_Extensions
91
     */
92
    private function deleteOrphanedProviders():void
93
    {
94
        $sipRecords = Sip::find();
95
        foreach ($sipRecords as $sipRecord){
96
            if ($sipRecord->Providers===null && $sipRecord->Extensions===null){
97
                $sipRecord->delete();
98
            }
99
        }
100
101
        $iaxRecords = Iax::find();
102
        foreach ($iaxRecords as $iaxRecord){
103
            if ($iaxRecord->Providers===null){
104
                $iaxRecord->delete();
105
            }
106
        }
107
108
    }
109
110
    /**
111
     * Deletes m_CallQueueMembers records without links to m_CallQueue and m_Extensions
112
     */
113
    private function deleteOrphanedQueueMembers():void
114
    {
115
        $records = CallQueueMembers::find();
116
        foreach ($records as $record){
117
            if ($record->Extensions===null
118
                || $record->CallQueues===null
119
             ){
120
                $record->delete();
121
            }
122
        }
123
    }
124
125
    /**
126
     * Deletes m_IvrMenuActions records without links to m_IvrMenu and m_Extensions
127
     */
128
    private function deleteOrphanedIVRMenuActions():void
129
    {
130
        $records = IvrMenuActions::find();
131
        foreach ($records as $record){
132
            if ($record->Extensions===null
133
                || $record->IvrMenu===null
134
            ){
135
                $record->delete();
136
            }
137
        }
138
    }
139
140
}