Connection::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 24
rs 9.8333
1
<?php
2
3
namespace CodePhix\Asaas;
4
5
class Connection {
6
    public $http;
7
    public $api_key;
8
    public $api_status;
9
    public $base_url;
10
    public $headers;
11
12
    public function __construct($token, $status) {
13
14
        if($status == 'producao'){
15
            $this->api_status = false;
16
        }elseif($status == 'homologacao'){
17
            $this->api_status = 1;
18
        }else{
19
            die('Tipo de homologação invalida');
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...
20
        }
21
        $this->api_key = $token;
22
        //$this->api_status = PAYMENT['asaas']['status'];
23
        $this->base_url = "https://" . (($this->api_status) ? 'sandbox' : 'www');
24
25
        return $this;
26
    }
27
28
29
    public function get($url, $option = false, $custom = false )
30
    {
31
32
        $ch = curl_init();
33
        curl_setopt($ch, CURLOPT_URL, $this->base_url .'.asaas.com/api/v3'. $url.$option);
34
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
35
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
36
37
        if(!empty($custom)){
38
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $custom);
39
        }
40
41
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
42
            "Content-Type: application/json",
43
            "access_token: ".$this->api_key
44
        ));
45
46
        $response = curl_exec($ch);
47
        curl_close($ch);
48
        $response = 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

48
        $response = json_decode(/** @scrutinizer ignore-type */ $response);
Loading history...
49
50
        //$response = $this->http->request('GET', $this->base_url . $url);
51
52
        return $response;
53
    }
54
55
    public function post($url, $params)
56
    {
57
        $params = json_encode($params);
58
        $ch = curl_init();
59
60
        curl_setopt($ch, CURLOPT_URL, $this->base_url .'.asaas.com/api/v3'. $url);
61
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
62
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
63
64
        curl_setopt($ch, CURLOPT_POST, TRUE);
65
66
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
67
68
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
69
            "Content-Type: application/json",
70
            "access_token: ".$this->api_key
71
        ));
72
73
        $response = curl_exec($ch);
74
        curl_close($ch);
75
        $response = 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

75
        $response = json_decode(/** @scrutinizer ignore-type */ $response);
Loading history...
76
77
        return $response;
78
79
    }
80
    
81
}
82