Client::curlGet()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 24

Duplication

Lines 5
Ratio 15.63 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 5
loc 32
rs 8.439
cc 5
eloc 24
nc 5
nop 3
1
<?php namespace SOSTheBlack\Moip;
2
3
use App;
4
5
/**
6
 * MoIP's API connection class
7
 *
8
 * @author Herberth Amaral
9
 * @author Paulo Cesar
10
 * @version 0.0.2
11
 * @license <a href="http://www.opensource.org/licenses/bsd-license.php">BSD License</a>
12
 */
13
class Client {
14
15
    /**
16
     * Method send()
17
     *
18
     * Send the request to API's server
19
     *
20
     * @param string $credentials Token and key to the authentication
21
     * @param string $xml The XML request
22
     * @param string $url The server's URL
23
     * @param string $method Method used to send the request
24
	 * @throws Exception
25
	 * @return \SOSTheBlack\Moip\Response
26
     */
27
    public function send($credentials, $xml, $url='https://desenvolvedor.moip.com.br/sandbox/ws/alpha/EnviarInstrucao/Unica', $method='POST') {
28
        $header[] = "Authorization: Basic " . base64_encode($credentials);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$header was never initialized. Although not strictly required by PHP, it is generally a good practice to add $header = 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...
29
        if (!function_exists('curl_init')){
30
            throw new Exception('This library needs cURL extension');
31
		}
32
        $curl = curl_init();
33
        curl_setopt($curl, CURLOPT_URL, $url);
34
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
35
        curl_setopt($curl, CURLOPT_USERPWD, $credentials);
36
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
37
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0");
38
39
        if ($method == 'POST') curl_setopt($curl, CURLOPT_POST, true);
40
41
		if ($xml != '') curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
42
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
43
        $ret = curl_exec($curl);
44
        $err = curl_error($curl);
45
        curl_close($curl);
46
47
        return App::make('\SOSTheBlack\Moip\Response', [['resposta' => $ret, 'erro' => $err]]);
48
    }
49
50
    /**
51
	 * @param string $credentials token / key authentication Moip
52
	 * @param string $xml url request
53
	 * @param string $url url request
54
	 * @param string $error errors
55
	 * @return \SOSTheBlack\Moip\Response
56
     */
57
    function curlPost($credentials, $xml, $url, $error=null) {
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...
58
59
        if (!$error) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
60
            $header[] = "Expect:";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$header was never initialized. Although not strictly required by PHP, it is generally a good practice to add $header = 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...
61
            $header[] = "Authorization: Basic " . base64_encode($credentials);
62
63
            $ch = curl_init();
64
            $options = array(CURLOPT_URL => $url,
65
                CURLOPT_HTTPHEADER => $header,
66
                CURLOPT_SSL_VERIFYPEER => false,
67
                CURLOPT_POST => true,
68
                CURLOPT_POSTFIELDS => $xml,
69
                CURLOPT_RETURNTRANSFER => true,
70
                CURLINFO_HEADER_OUT => true
71
            );
72
73
            curl_setopt_array($ch, $options);
74
            $ret = curl_exec($ch);
75
            $err = curl_error($ch);
76
            $info = curl_getinfo($ch);
77
            curl_close($ch);
78
79
80
            if ($info['http_code'] == "200")
81
                return App::make('\SOSTheBlack\Moip\Response', [['response' => true, 'error' => null, 'xml' => $ret]]);
82 View Code Duplication
            elseif ($info['http_code'] == "500")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
83
                return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Error processing XML', 'xml' => null]]);
84 View Code Duplication
            elseif ($info['http_code'] == "401")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
                return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Authentication failed', 'xml' => null]]);
86 View Code Duplication
            else
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
                return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $err, 'xml' => null]]);
88 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $error, 'xml' => null]]);
90
        }
91
    }
92
93
94
    /**
95
     * @param string $credentials token / key authentication Moip
96
     * @param string $url url request
97
     * @param string $error errors
98
     * @return \SOSTheBlack\Moip\Response
99
     */
100
    function curlGet($credentials, $url, $error=null) {
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...
101
102
        if (!$error) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
103
            $header[] = "Expect:";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$header was never initialized. Although not strictly required by PHP, it is generally a good practice to add $header = 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...
104
            $header[] = "Authorization: Basic " . base64_encode($credentials);
105
106
            $ch = curl_init();
107
            $options = array(CURLOPT_URL => $url,
108
                CURLOPT_HTTPHEADER => $header,
109
                CURLOPT_SSL_VERIFYPEER => false,
110
                CURLOPT_RETURNTRANSFER => true
111
                );
112
113
            curl_setopt_array($ch, $options);
114
            $ret = curl_exec($ch);
115
            $err = curl_error($ch);
116
            $info = curl_getinfo($ch);
117
            curl_close($ch);
118
119
120
            if ($info['http_code'] == "200")
121
                return App::make('\SOSTheBlack\Moip\Response', [['response' => true, 'error' => null, 'xml' => $ret]]);
122
            else if ($info['http_code'] == "500")
123
                return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Error processing XML', 'xml' => null]]);
124
            else if ($info['http_code'] == "401")
125
                return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => 'Authentication failed', 'xml' => null]]);
126 View Code Duplication
            else
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
127
                return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $err, 'xml' => null]]);
128 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
129
            return App::make('\SOSTheBlack\Moip\Response', [['response' => false, 'error' => $error, 'xml' => null]]);
130
        }
131
    }
132
133
}