ClientRest   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 26
eloc 99
c 1
b 0
f 1
dl 0
loc 159
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A cookie() 0 8 3
A error() 0 4 1
A put() 0 22 4
A delete() 0 21 4
A post() 0 27 5
B get() 0 36 7
1
<?php
2
3
/**
4
 * KNUT7 K7F (https://marciozebedeu.com/)
5
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @link      https://github.com/knut7/framework/ for the canonical source repository
12
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
13
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
14
 * @author    Marcio Zebedeu - [email protected]
15
 * @version   1.0.2
16
 */
17
18
namespace Ballybran\Core\Http\Client;
19
20
use \Ballybran\Core\Http\Encodes;
21
use Ballybran\Helpers\Http\Http;
22
23
class ClientRest extends Encodes
24
{
25
26
    private $cookies;
27
    private $headers = [];
28
    private $user_agent;
29
    private $compression;
30
    private $cookie_file;
31
    private $proxy;
32
33
    function __construct($cookies = true, $cookie = "", $compression = 'gzip', $proxy = '')
34
    {
35
        $this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
36
        $this->headers[] = 'Connection: Keep-Alive';
37
        $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
38
        $this->user_agent = Http::http_user_agent();
39
        $this->compression = $compression;
40
        $this->proxy = $proxy;
41
        $this->cookies = $cookies;
42
        if ($this->cookies == true) {
43
            $this->cookie($cookie);
44
        }
45
46
    }
47
48
    private function cookie($cookie_file)
49
    {
50
        if (file_exists($cookie_file)) {
51
                $this->cookie_file = $cookie_file;
52
        } else {
53
            fopen($cookie_file, 'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions');
54
            $this->cookie_file = $cookie_file;
55
            fclose($cookie_file);
56
        }
57
    }
58
59
    public function get($url, $fields = null, $httpheader = null)
60
    {
61
        $process = curl_init($url);
62
        curl_setopt($process, CURLOPT_HTTPHEADER, $httpheader);
63
        curl_setopt($process, CURLOPT_HEADER, 0);
64
        curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
65
        if ($this->cookies == true) {
66
            curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
67
        }
68
        if ($this->cookies == true) {
69
            curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
70
        }
71
        curl_setopt($process, CURLOPT_ENCODING, $this->compression);
72
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
73
        if ($this->proxy) {
74
            curl_setopt($process, CURLOPT_PROXY, $this->proxy);
75
        }
76
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
77
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
78
        $return = curl_exec($process);
79
80
81
        // Close the cURL resource, and free system resources
82
        curl_close($process);
83
        $decoded = json_decode($return);
0 ignored issues
show
Bug introduced by
It seems like $return can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $decoded = json_decode(/** @scrutinizer ignore-type */ $return);
Loading history...
84
85
        if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
86
            die('error occured: ' . $decoded->response->errormessage);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
87
        }
88
        if (json_last_error() === JSON_ERROR_NONE) {
89
            //if not joson, is xml'
90
            return ($decoded);
91
92
        } else {
93
            // if json true, return json
94
            return ($return);
95
96
        }
97
98
    }
99
100
    public function post($url, $data, $httpheader = null )
101
    {
102
        $process = curl_init($url);
103
        if(null == $httpheader){
104
            $httpheader = $this->headers;
105
        }
106
        curl_setopt($process, CURLOPT_HTTPHEADER, $httpheader);
107
        // curl_setopt($process, CURLOPT_HEADER, 1);
108
        curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
109
        if ($this->cookies == true) {
110
            curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
111
        }
112
        if ($this->cookies == true) {
113
            curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
114
        }
115
        curl_setopt($process, CURLOPT_ENCODING, $this->compression);
116
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
117
        if ($this->proxy) {
118
            curl_setopt($process, CURLOPT_PROXY, $this->proxy);
119
        }
120
        curl_setopt($process, CURLOPT_POST, 1);
121
        curl_setopt($process, CURLOPT_POSTFIELDS, $data);
122
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
123
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
124
        $return = curl_exec($process);
125
        curl_close($process);
126
        return $return;
127
    }
128
129
130
131
    public function put($url = '', array $data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

131
    public function put($url = '', /** @scrutinizer ignore-unused */ array $data)

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

Loading history...
132
    {
133
//        'http://example.com/api/conversations/cid123/status'
134
        $process = curl_init($url);
135
136
        curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
137
        curl_setopt($process, CURLOPT_CUSTOMREQUEST, "PUT");
138
        $data = array("status" => 'R');
139
        curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($data));
140
        $response = curl_exec($process);
141
        if (false === $response) {
142
            $info = curl_getinfo($process);
143
            curl_close($process);
144
            die('error occured during curl exec. Additioanl info: ' . var_export($info));
0 ignored issues
show
Bug introduced by
Are you sure the usage of var_export($info) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
145
        }
146
        curl_close($process);
147
        $decoded = json_decode($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
        $decoded = json_decode(/** @scrutinizer ignore-type */ $response);
Loading history...
148
        if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
149
            die('error occured: ' . $decoded->response->errormessage);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
150
        }
151
        echo 'response ok!';
152
        return ($decoded->response);
153
    }
154
155
    public function delete($value = '')
156
    {
157
//        'http://example.com/api/conversations/[CONVERSATION_ID]'
158
        $service_url = $value;
159
        $process = curl_init($service_url);
160
        curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
161
        curl_setopt($process, CURLOPT_CUSTOMREQUEST, "DELETE");
162
        curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($service_url));
163
        $curl_response = curl_exec($process);
164
        if (false === $curl_response) {
165
            $info = curl_getinfo($process);
166
            curl_close($process);
167
            die('error occured during curl exec. Additioanl info: ' . var_export($info));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug introduced by
Are you sure the usage of var_export($info) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
168
        }
169
        curl_close($process);
170
        $decoded = json_decode($curl_response);
0 ignored issues
show
Bug introduced by
It seems like $curl_response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

170
        $decoded = json_decode(/** @scrutinizer ignore-type */ $curl_response);
Loading history...
171
        if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
172
            die('error occured: ' . $decoded->response->errormessage);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
173
        }
174
        echo 'response ok!';
175
        return ($decoded->response);
176
    }
177
178
    function error($error)
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...
179
    {
180
        echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
181
        die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
182
    }
183
}
184