Passed
Push — develop ( abe420...af4c47 )
by Nikolay
04:36
created

deleteOrphanedQueueMembers()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 4
nc 3
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\Core\System\Upgrade\UpgradeSystemConfigInterface;
27
use Phalcon\Di\Injectable;
28
use MikoPBX\Core\System\MikoPBXConfig;
29
use Phalcon\Config as ConfigAlias;
30
31
class UpdateConfigsUpToVer2020362 extends Injectable implements UpgradeSystemConfigInterface
32
{
33
  	public const PBX_VERSION = '2020.3.62';
34
35
	private ConfigAlias $config;
36
    private MikoPBXConfig $mikoPBXConfig;
37
38
	/**
39
     * Class constructor.
40
     */
41
    public function __construct()
42
    {
43
        $this->config = $this->getDI()->getShared('config');
44
        $this->mikoPBXConfig = new MikoPBXConfig();
45
    }
46
47
	/**
48
     * Main function
49
     */
50
    public function processUpdate():void
51
    {
52
        if ($this->isLiveCD) {
0 ignored issues
show
Bug Best Practice introduced by
The property isLiveCD does not exist on MikoPBX\Core\System\Upgr...teConfigsUpToVer2020362. Since you implemented __get, consider adding a @property annotation.
Loading history...
53
            return;
54
        }
55
        $this->deleteOrphanedProviders();
56
        $this->deleteOrphanedQueueMembers();
57
        $this->deleteOrphanedIVRMenuActions();
58
    }
59
60
    /**
61
     * Deletes m_Sip and m_Iax records without links to m_Providers and m_Extensions
62
     */
63
    private function deleteOrphanedProviders():void
64
    {
65
        $sipRecords = Sip::find();
66
        foreach ($sipRecords as $sipRecord){
67
            if ($sipRecord->Providers===null && $sipRecord->Extensions===null){
68
                $sipRecord->delete();
69
            }
70
        }
71
72
        $iaxRecords = Iax::find();
73
        foreach ($iaxRecords as $iaxRecord){
74
            if ($iaxRecord->Providers===null){
75
                $iaxRecord->delete();
76
            }
77
        }
78
79
    }
80
81
    /**
82
     * Deletes m_CallQueueMembers records without links to m_CallQueue and m_Extensions
83
     */
84
    private function deleteOrphanedQueueMembers():void
85
    {
86
        $records = CallQueueMembers::find();
87
        foreach ($records as $record){
88
            if ($record->Extensions===null
89
                || $record->CallQueues===null
90
             ){
91
                $record->delete();
92
            }
93
        }
94
    }
95
96
    /**
97
     * Deletes m_IvrMenuActions records without links to m_IvrMenu and m_Extensions
98
     */
99
    private function deleteOrphanedIVRMenuActions():void
100
    {
101
        $records = IvrMenuActions::find();
102
        foreach ($records as $record){
103
            if ($record->Extensions===null
104
                || $record->IvrMenu===null
105
            ){
106
                $record->delete();
107
            }
108
        }
109
    }
110
111
}