astpp.functions.php ➔ do_reseller_cdr_process()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 8
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
###############################################################################
3
# ASTPP - Open Source VoIP Billing Solution
4
#
5
# Copyright (C) 2016 iNextrix Technologies Pvt. Ltd.
6
# Samir Doshi <[email protected]>
7
# ASTPP Version 3.0 and above
8
# License https://www.gnu.org/licenses/agpl-3.0.html
9
#
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU Affero General Public License as
12
# published by the Free Software Foundation, either version 3 of the
13
# License, or (at your option) any later version.
14
# 
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU Affero General Public License for more details.
19
# 
20
# You should have received a copy of the GNU Affero General Public License
21
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
###############################################################################
23
24
25
//Parse user and rates array which we got in cdr xml
26
function parse_rates_array($xml_rate, $constant_array, $logger) {
0 ignored issues
show
Unused Code introduced by
The parameter $logger is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
    $rates_array = array();
28
29
    //decode string using urldecode
30
    $xml_rate = urldecode($xml_rate);
31
    $xml_rate_array = explode("||", $xml_rate);
32
33
    foreach ($xml_rate_array as $rate_key => $rate_value) {
34
        $rates_array = explode("|", $rate_value);
35
36
        $user_id_param = $rates_array[count($rates_array) - 1];
37
        $user_id = (substr($user_id_param, 0, 3) == 'UID') ? substr($user_id_param, 3) : 0;
38
39
        foreach ($rates_array as $key => $value) {
40
            $rates_array_info[$user_id][$constant_array[substr($value, 0, 3)]] = substr($value, 3);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$rates_array_info was never initialized. Although not strictly required by PHP, it is generally a good practice to add $rates_array_info = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
41
        }
42
    }
43
    return $rates_array_info;
0 ignored issues
show
Bug introduced by
The variable $rates_array_info does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
44
}
45
46
//Process package 
47
function process_package($xml_cdr, $user_id, $rates_array, $logger, $db) {
48
    $duration = $xml_cdr->variables->duration;
49
    $xml_cdr->variables->package_id = 0;
50
    $flag = false;
51
    if ($duration > 0 && $xml_cdr->variables->call_direction == 'outbound') {
52
        $destination_number = $xml_cdr->variables->effective_destination_number;
53
54
        $number_len = strlen($destination_number);
55
        $number_loop_str = '(';
56
        while ($number_len > 0) {
57
            $number_loop_str .= " code='".substr($destination_number, 0, $number_len)."' OR ";
58
            $number_len -= 1;
59
        }
60
        $number_loop_str .= " code='--')";
61
62
        $query = "SELECT A.id as package_id,code,includedseconds FROM tbl_package AS A ,tbl_package_codes AS B WHERE ".$number_loop_str." AND B.package_id = A.id AND A.ratecard_id=".$rates_array['ratecard_id']." AND A.status=0 AND A.is_del=0 ORDER BY length(code) DESC";
63
        $logger->log("Package Query : ".$query);
64
        $res_package = $db->run($query);
65
66
        foreach ($res_package as $res_package_key => $package_info) {
67
            if (isset($package_info['package_id'])) {
68
                $query = "SELECT SUM(used_seconds) as used_seconds FROM tbl_package_usage WHERE code=".$package_info['code']." AND package_id=".$package_info['package_id']." AND user_id=".$user_id;
69
                $logger->log("Package usage Query : ".$query);
70
                $res_pkg_usg = $db->run($query);
71
                $package_usage_info = $res_pkg_usg[0];
72
73
                $used_seconds = (isset($package_usage_info['used_seconds'])) ? $package_usage_info['used_seconds'] : 0;
74
75
                $logger->log("Included seconds : ".$package_info['includedseconds'].", Used seconds : ".$used_seconds);
76
                if ($package_info['includedseconds'] > $used_seconds) {
77
                    $remaining_seconds = $package_info['includedseconds'] - ($duration + $used_seconds);
78
                    if ($remaining_seconds > 0) {
79
                        $dud_sec = $duration;
80
                        $duration = 0;
81
                    } else {
82
                        $dud_sec = $duration - abs($remaining_seconds);
83
                        $duration = abs($remaining_seconds);
84
                    }
85
                    $flag = true;
86
                    $xml_cdr->variables->package_id = $package_info['package_id'];
87
88
                    $query = "INSERT INTO tbl_package_usage (package_id,user_id,code,used_seconds) VALUES (".$package_info['package_id'].",".$user_id.",'".$package_info['code']."',".$dud_sec.") ON DUPLICATE KEY UPDATE used_seconds=used_seconds+".$dud_sec;
89
                    $logger->log("Package Usage Query : ".$query);
90
                    $db->run($query);
91
92
                    break;
93
                }
94
            }
95
        }
96
    }
97
    return array($duration, $flag);
98
}
99
100
//Process user/vendor cdr
101
function do_cdr_process($xml_cdr, $debit, $cost, $vendor_cost, $rates_array, $parent_id = 0, $parent_rates, $carrier_rates_array, $logger, $db) {
102
    $query_string = "'".$xml_cdr->variables->uuid."','".$xml_cdr->variables->user_id."','".$xml_cdr->variables->entity_id."','".urldecode($xml_cdr->variables->effective_caller_id_name)."','".$xml_cdr->variables->effective_caller_id_number."','".$xml_cdr->variables->effective_destination_number."',".$xml_cdr->variables->duration.",'".$xml_cdr->variables->carrier_id."','".$xml_cdr->callflow[0]->caller_profile->originatee->originatee_caller_profile->network_addr."','".$xml_cdr->variables->sip_contact_host."','".$xml_cdr->variables->hangup_cause."','".urldecode($xml_cdr->variables->start_stamp)."',".$debit.",".$cost.",'".$xml_cdr->variables->vendor_id."',".$vendor_cost.",".$rates_array['ratecard_id'].",".$xml_cdr->variables->package_id.",'".$rates_array['code']."','".$rates_array['destination']."','".$rates_array['cost']."','".$parent_id."','".@$parent_rates['code']."','".@$parent_rates['destination']."','".@$parent_rates['cost']."','".@$carrier_rates_array['code']."','".@$carrier_rates_array['destination']."','".@$carrier_rates_array['cost']."','".$xml_cdr->variables->call_direction."','".urldecode($xml_cdr->variables->profile_start_stamp)."','".urldecode($xml_cdr->variables->answer_stamp)."','".urldecode($xml_cdr->variables->bridge_stamp)."','".urldecode($xml_cdr->variables->progress_stamp)."','".urldecode($xml_cdr->variables->progress_media_stamp)."','".urldecode($xml_cdr->variables->end_stamp)."',".$xml_cdr->variables->billmsec.",".$xml_cdr->variables->answermsec.",".$xml_cdr->variables->waitmsec.",".$xml_cdr->variables->progress_mediamsec.",".$xml_cdr->variables->flow_billmsec;
103
104
    $query = "INSERT INTO tbl_cdrs (uniqueid,user_id,entity_id,callerid_name,callerid_number,dstnum,duration,carrier_id,carrierip,callerip,disposition,start_stamp,debit,cost,vendor_id,vendor_cost,ratecard_id,package_id,rate_code,rate_code_destination,rate_cost,parent_id,parent_code,parent_code_destination,parent_cost,carrier_code,carrier_code_destination ,carrier_cost,call_direction,profile_start_stamp,answer_stamp,bridge_stamp,progress_stamp,progress_media_stamp,end_stamp,billmsec,answermsec,waitmsec,progress_mediamsec,flow_billmsec) values ($query_string)";
105
106
    $logger->log("CDR Query : ".$query);
107
    $db->run($query);
108
}
109
110
//Process reseller cdr
111
function do_reseller_cdr_process($xml_cdr, $debit, $cost, $rates_array, $parent_id = 0, $parent_rates, $logger, $db) {
112
    $query_string = "'".$xml_cdr->variables->uuid."','".$xml_cdr->variables->user_id."','".urldecode($xml_cdr->variables->effective_caller_id_name)."','".$xml_cdr->variables->effective_caller_id_number."','".$xml_cdr->variables->effective_destination_number."',".$xml_cdr->variables->duration.",'".$xml_cdr->variables->hangup_cause."','".urldecode($xml_cdr->variables->start_stamp)."',".$debit.",".$cost.",".$rates_array['ratecard_id'].",".$xml_cdr->variables->package_id.",'".$rates_array['code']."','".$rates_array['destination']."','".$rates_array['cost']."','".$parent_id."','".@$parent_rates['code']."','".@$parent_rates['destination']."','".@$parent_rates['cost']."','".$xml_cdr->variables->call_direction."'";
113
114
    $query = "INSERT INTO tbl_cdrs_reseller (uniqueid,reseller_id,callerid_name,callerid_number,dstnum,duration,disposition,start_stamp,debit,cost,ratecard_id,package_id,rate_code,rate_code_destination,rate_cost,parent_id,parent_code,parent_code_destination,parent_cost,call_direction) values ($query_string)";
115
116
    $logger->log("CDR Query : ".$query);
117
    $db->run($query);
118
}
119
120
//Update user balance
121
/**
122
 * @param integer $entity_id
123
 */
124 View Code Duplication
function update_balance($user_id, $amount, $entity_id, $logger, $db) {
0 ignored issues
show
Best Practice introduced by
The function update_balance() has been defined more than once; this definition is ignored, only the first definition in freeswitch/fs/lib/astpp.cdr.php (L329-334) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    if ($amount > 0) {
126
        $math_sign = ($entity_id == 0 || $entity_id == 1) ? '-' : '+';
127
        $query = "UPDATE tbl_users SET credit=credit$math_sign".$amount." WHERE id=".$user_id;
128
        $logger->log("Balance update : ".$query);
129
        $db->run($query);
130
    }
131
}
132
133
//Get user info
134
function get_user_info($parent_id, $db) {
135
    $query = "SELECT * FROM tbl_users WHERE id=".$parent_id;
136
    $res_user = $db->run($query);
137
    return $res_user[0];
138
}
139
140
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
141