Passed
Push — develop ( d5179e...bede24 )
by Портнов
05:56 queued 11s
created

SelectCDR::execute()   B

Complexity

Conditions 11
Paths 92

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 52
rs 7.3166
cc 11
nc 92
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2021 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\Workers\Libs\WorkerCallEvents;
21
22
23
use MikoPBX\Common\Models\CallDetailRecords;
24
use MikoPBX\Common\Models\CallDetailRecordsTmp;
25
use MikoPBX\Core\System\Util;
26
use Phalcon\Di;
27
use Throwable;
28
29
class SelectCDR
30
{
31
    public static function execute($filter):string
32
    {
33
34
        if(self::filterNotValid($filter)){
35
            return '[]';
36
        }
37
38
        $res    = null;
39
        try {
40
            if (isset($filter['miko_tmp_db'])) {
41
                $res = CallDetailRecordsTmp::find($filter);
42
            } else {
43
                $res = CallDetailRecords::find($filter);
44
            }
45
            $res_data = json_encode($res->toArray());
46
        } catch (Throwable $e) {
47
            $res_data = '[]';
48
        }
49
50
        if ($res && isset($filter['add_pack_query'])) {
51
            $arr = [];
52
            foreach ($res->toArray() as $row) {
53
                $arr[] = $row[$filter['columns']];
54
            }
55
            $filter['add_pack_query']['bind'][$filter['columns']] = $arr;
56
57
            if(self::filterNotValid($filter['add_pack_query'])){
58
                return '[]';
59
            }
60
61
            try {
62
                $res      = CallDetailRecords::find($filter['add_pack_query']);
63
                $res_data = json_encode($res->toArray(), JSON_THROW_ON_ERROR);
64
            } catch (Throwable $e) {
65
                $res_data = '[]';
66
            }
67
        }
68
69
        if (isset($filter['miko_result_in_file'])) {
70
            $di         = Di::getDefault();
71
            if($di){
72
                $dirsConfig = $di->getShared('config');
73
                $filename   = $dirsConfig->path('core.tempDir') . '/' . md5(microtime(true));
74
            }else{
75
                $filename = '/tmp/' . md5(microtime(true));
76
            }
77
            file_put_contents($filename, $res_data);
78
            Util::addRegularWWWRights($filename);
79
            $res_data = json_encode($filename);
80
        }
81
82
        return $res_data;
83
84
    }
85
86
    /**
87
     * Проверка фильтра на корректность bind параметров.
88
     *
89
     * @param $filter
90
     *
91
     * @return bool
92
     */
93
    private static function filterNotValid($filter):bool{
94
        $haveErrors = false;
95
        $validValue = ['0',''];
96
        if(isset($filter['bind'])){
97
            if(is_array($filter)){
98
                foreach ($filter['bind'] as $bindValue) {
99
                    if(empty($bindValue) && !in_array($bindValue, $validValue, true)){
100
                        $haveErrors = true;
101
                    }
102
                }
103
            }else{
104
                $haveErrors = true;
105
            }
106
        }
107
        return $haveErrors;
108
    }
109
}