Passed
Push — develop ( 7c58c1...71706d )
by Портнов
05:03
created

CallDetailRecordsTmp::afterSave()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 25
rs 8.0555
cc 9
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\Common\Models;
21
22
use MikoPBX\Common\Providers\ManagedCacheProvider;
23
use MikoPBX\Core\System\Util;
24
25
/**
26
 * Class CallDetailRecordsTmp
27
 *
28
 * @package MikoPBX\Common\Models
29
 *
30
 * @Indexes(
31
 *     [name='UNIQUEID', columns=['UNIQUEID'], type=''],
32
 *     [name='start', columns=['start'], type=''],
33
 *     [name='src_chan', columns=['src_chan'], type=''],
34
 *     [name='dst_chan',columns=['dst_chan'], type=''],
35
 *     [name='src_num', columns=['src_num'], type=''],
36
 *     [name='dst_num', columns=['dst_num'], type=''],
37
 *     [name='linkedid', columns=['linkedid'], type='']
38
 * )
39
 */
40
class CallDetailRecordsTmp extends CallDetailRecordsBase
41
{
42
    public const CACHE_KEY = 'Workers:Cdr';
43
44
    public function initialize(): void
45
    {
46
        $this->setSource('cdr');
47
        parent::initialize();
48
        $this->useDynamicUpdate(true);
49
        $this->setConnectionService('dbCDR');
50
    }
51
52
    public function afterSave():void {
53
54
        $moveToGeneral = true;
55
        if( $this->disposition === 'ANSWERED' &&
56
            ($this->appname === 'interception' || $this->appname === 'originate') ){
57
            // Это успешный originate или перехват на ответственного.
58
            // Принудительно логировать такой вызов не следует.
59
            $moveToGeneral = false;
60
        }
61
62
        $work_completed = (string)$this->work_completed;
63
        if( $work_completed === '1' && $moveToGeneral){
64
            $newCdr = new CallDetailRecords();
65
            $vars   = $this->toArray();
66
            foreach ($vars as $key => $value){
67
                if( 'id' === $key){
68
                    continue;
69
                }
70
                if(property_exists($newCdr, $key)){
71
                    $newCdr->writeAttribute($key, $value);
72
                }
73
            }
74
            $newCdr->save();
75
        }
76
        $this->saveCdrCache();
77
    }
78
79
    public function afterDelete():void
80
    {
81
        $this->saveCdrCache(false);
82
    }
83
84
    /**
85
     * Храним в Redis текущие звонки.
86
     * @param bool $isSave
87
     * @return void
88
     */
89
    private function saveCdrCache(bool $isSave = true):void
90
    {
91
        try {
92
            $managedCache = $this->di->get(ManagedCacheProvider::SERVICE_NAME);
93
94
            $rowData = $this->toArray();
95
            $newKey  = self::CACHE_KEY.':'.$rowData['UNIQUEID'];
96
            $idsList = $managedCache->getKeys(self::CACHE_KEY);
97
            if($isSave && !in_array($newKey, $idsList, true)){
98
                $idsList[$rowData['UNIQUEID']] = true;
99
                $managedCache->set('Workers:Cdr:'.$rowData['UNIQUEID'], $rowData, 19200);
100
            }else{
101
                $managedCache->delete('Workers:Cdr:'.$rowData['UNIQUEID']);
102
            }
103
        }catch (\Throwable $e){
104
            Util::sysLogMsg(self::class, $e->getMessage());
105
            return;
106
        }
107
    }
108
109
}