Passed
Push — develop ( 472d30...cd5635 )
by Портнов
05:23
created

CDRDatabaseProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 24
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A getCdr() 0 29 5
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
declare(strict_types=1);
21
22
namespace MikoPBX\Common\Providers;
23
24
use MikoPBX\Core\System\BeanstalkClient;
25
use MikoPBX\Core\System\Util;
26
use MikoPBX\Core\Workers\WorkerCdr;
27
use Phalcon\Di\DiInterface;
28
use Phalcon\Di\ServiceProviderInterface;
29
30
/**
31
 * CDR database connection is created based in the parameters defined in the configuration file
32
 */
33
class CDRDatabaseProvider extends DatabaseProviderBase implements ServiceProviderInterface
34
{
35
    public const SERVICE_NAME = 'dbCDR';
36
37
    /**
38
     * Register dbCDR service provider
39
     *
40
     * @param \Phalcon\Di\DiInterface $di
41
     */
42
    public function register(DiInterface $di): void
43
    {
44
        $dbConfig = $di->getShared(ConfigProvider::SERVICE_NAME)->get('cdrDatabase')->toArray();
45
        $this->registerDBService(self::SERVICE_NAME, $di, $dbConfig);
46
    }
47
48
    /**
49
     * Возвращает все завершенные временные CDR.
50
     * @param array $filter
51
     * @return array
52
     */
53
    public static function getCdr(array $filter = []):array
54
    {
55
        if(empty($filter)){
56
            $filter = [
57
                'work_completed<>1 AND endtime<>""'
58
            ];
59
        }
60
        $filter['miko_result_in_file'] = true;
61
        $filter['order'] = 'answer';
62
        $filter['columns'] = 'start,answer,src_num,dst_num,dst_chan,endtime,linkedid,recordingfile,dialstatus,UNIQUEID';
63
64
        $client = new BeanstalkClient(WorkerCdr::SELECT_CDR_TUBE);
65
        try {
66
            $result   = $client->request(json_encode($filter), 2);
67
            $filename = json_decode($result, true, 512, JSON_THROW_ON_ERROR);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type false; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            $filename = json_decode(/** @scrutinizer ignore-type */ $result, true, 512, JSON_THROW_ON_ERROR);
Loading history...
68
        }catch (\Throwable $e){
69
            $filename = '';
70
        }
71
        $result_data = [];
72
        if (file_exists($filename)) {
73
            try {
74
                $result_data = json_decode(file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR);
75
            }catch (\Throwable $e){
76
                Util::sysLogMsg('SELECT_CDR_TUBE', 'Error parse response.');
77
            }
78
            unlink($filename);
79
        }
80
81
        return $result_data;
82
    }
83
}