Completed
Push — master ( 388b89...cbc64f )
by Florian
06:14
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the GNU General Public License (GPL 3)
8
 * that is bundled with this package in the file LICENSE.txt
9
 *
10
 * DISCLAIMER
11
 *
12
 * Do not edit or add to this file if you wish to upgrade Payone to newer
13
 * versions in the future. If you wish to customize Payone for your
14
 * needs please refer to http://www.payone.de for more information.
15
 *
16
 * @category        Payone
17
 * @package         Payone_Api
18
 * @subpackage      Response
19
 * @author          Ronny Schröder
20
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
21
 */
22
class Payone_Api_Response_Genericpayment_Approved extends Payone_Api_Response_Genericpayment_Abstract {
23
    
24
    /**
25
     * add_paydata[workorderid] = workorderid from payone
26
     * add_paydata[...] = delivery data
27
     * @var Payone_Api_Response_Parameter_Paydata_Paydata
28
     */
29
    protected $paydata = NULL;
30
31
    /**
32
     * @param array $params
33
     */
34
    function __construct(array $params = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
        parent::__construct($params);
36
        $this->initPaydata($params);
37
    }
38
39
    protected function initPaydata($param) {
40
41
        $payData = new Payone_Api_Response_Parameter_Paydata_Paydata($param);
42
43
        if ($payData->hasItems()) {
44
            $this->setPaydata($payData);
45
        } else {
46
            $this->setPaydata(NULL);
0 ignored issues
show
Documentation introduced by
NULL is of type null, but the function expects a object<Payone_Api_Respon...ameter_Paydata_Paydata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
        }
48
    }
49
50
51
    /**
52
     * usage:
53
     * $request = new Payone_Api_Request_Genericpayment(array_merge($this->getAccountData(), $requestData));
54
     * $builder = $this->getPayoneBuilder();
55
     *
56
     * $service = $builder->buildServicePaymentGenericpayment();
57
     * $response = $service->request($request);
58
     * print_r($response->getPaydata()->toAssocArray());
59
     * 
60
     * you get an array like that:
61
     * 
62
     * Array
63
     * (
64
     *    [shipping_zip] => 79111
65
     *    [shipping_country] => DE
66
     *    [shipping_state] => Empty
67
     *    [shipping_city] => Freiburg
68
     *    [shipping_street] => ESpachstr. 1
69
     *    [shipping_firstname] => Max
70
     *    [shipping_lastname] => Mustermann
71
     * )
72
     * 
73
     * @return Payone_Api_Response_Parameter_Paydata_Paydata
74
     */
75
    public function getPaydata() {
76
        return $this->paydata;
77
    }
78
79
    /**
80
     * @param Payone_Api_Response_Parameter_Paydata_Paydata $paydata
81
     */
82
    public function setPaydata($paydata) {
83
        $this->paydata = $paydata;
84
    }
85
    
86
    /**
87
     * 
88
     * @return Payone_Api_Request_Parameter_Paydata_Paydata
89
     */
90
    public function getPayDataArray() {
91
        $aPayData = array();
92
        foreach($this->getPayData()->getItems() as $item) {
93
            $sCorrectedKey = strtolower($item->getKey());
94
            $sCorrectedKey = str_replace('-', '_', $sCorrectedKey);
95
            $aPayData[$sCorrectedKey] = $item->getData();
96
        }
97
        ksort($aPayData);
98
        return $aPayData;
99
    }
100
    
101
    public function getInstallmentData()
102
    {
103
        $aInstallmentData = array();
104
        
105
        $aPayData = $this->getPayDataArray();
106
        foreach ($aPayData as $sKey => $sValue) {
107
            $aSplit = explode('_', $sKey);
108
            for($i = count($aSplit); $i > 0; $i--) {
109
                if($i == count($aSplit)) {
110
                    $aTmp = array($aSplit[$i-1] => $sValue);
111
                } else {
112
                    $aTmp = array($aSplit[$i-1] => $aTmp);
0 ignored issues
show
Bug introduced by
The variable $aTmp 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...
113
                }
114
            }
115
            $aInstallmentData = array_replace_recursive($aInstallmentData, $aTmp);
116
        }
117
        
118
        if(isset($aInstallmentData['paymentdetails']) && count($aInstallmentData['paymentdetails']) > 0) {
119
            return $aInstallmentData['paymentdetails'];
120
        }
121
        return false;
122
    }
123
124
125
}
126