lightConnector   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setMethod() 0 3 1
A setKlarnaOrderId() 0 3 1
A setTransactionId() 0 3 1
A updateCustomerAddress() 0 3 1
A setCall() 0 15 2
1
<?php
2
3
/**
4
 * A lightweigt model for contacting Klarna API
5
 *
6
 * @author andre
7
 */
8
class lightConnector {
9
10
    /**
11
     * Connecting method
12
     * @var string
13
     */
14
    protected $method = null;
15
16
    /**
17
     * URI part for API calls
18
     * @var string
19
     */
20
    protected $uri = null;
21
22
    /**
23
     * Instance of shop configuration
24
     * @var object
25
     */
26
    protected $config = null;
27
28
    /**
29
     * Holds the klarna orderid 
30
     * @var string
31
     */
32
    protected $klarnaOrderId = null;
33
34
    /**
35
     * TransactionId of an existing order
36
     * @var string
37
     */
38
    protected $transactionId = null;
39
    
40
    protected $apiUrl = array('live'=>'https://api.klarna.com/','test'=>'https://api.playground.klarna.com/');
41
42
    /**
43
     * Creates an instance of light connector
44
     * 
45
     * @param type $config
46
     * @param type $method
0 ignored issues
show
Documentation introduced by
Should the type for parameter $method not be type|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
47
     */
48
    public function __construct($config, $method = null) {
49
        parent::__construct();
50
        $this->method = $method;
0 ignored issues
show
Documentation Bug introduced by
It seems like $method can also be of type object<type>. However, the property $method is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
        $this->config = $config;
52
    }
53
54
    /**
55
     * Sets method of requesting API
56
     * 
57
     * @param string $method
58
     * @return void
59
     */
60
    public function setMethod($method) {
61
        $this->method = $method;
62
    }
63
64
    /**
65
     * Sets klarna order id
66
     * 
67
     * @param string $klarnaOrderId
68
     * @return void
69
     */
70
    public function setKlarnaOrderId($klarnaOrderId) {
71
        $this->klarnaOrderId = $klarnaOrderId;
72
    }
73
74
    /**
75
     * Adds transactionId
76
     * 
77
     * @param string $transactionId
78
     */
79
    public function setTransactionId($transactionId) {
80
        $this->transactionId = $transactionId;
81
    }
82
83
    /**
84
     * Updates customer addresses
85
     * 
86
     * @param array $billingAddress
87
     * @param array $shippingAddress
0 ignored issues
show
Documentation introduced by
Should the type for parameter $shippingAddress not be false|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
88
     * @return void
89
     */
90
    public function updateCustomerAddress($billingAddress, $shippingAddress = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $billingAddress 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...
Unused Code introduced by
The parameter $shippingAddress 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...
91
        $aAddressParts = array('');
0 ignored issues
show
Unused Code introduced by
$aAddressParts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
    }
93
    
94
    /**
95
     * Performs an API-Call
96
     * 
97
     * @param array $data
98
     */
99
    protected function setCall($data) {
100
        $testdrive = $this->config->get('testDrive');
101
        $url = ($testdrive) ? $this->apiUrl['test'] : $this->apiUrl['live'];
102
        
103
        $data = json_encode($data);
104
        $headers = array('Content-Type: application/json');
105
        $curl = curl_init();
106
        curl_setopt($curl, CURLOPT_URL, $url);
107
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
108
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->method);
109
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
110
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
111
        $response = curl_exec($curl);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
        curl_close($curl);        
113
    }
114
115
}
116