Completed
Push — master ( b3a506...dc1ac5 )
by Adriano
03:05
created

AbstractAPI::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 2
nc 1
nop 0
1
<?php
2
namespace Integracao\ControlPay;
3
4
use GuzzleHttp;
5
use Integracao\ControlPay\Constants\ControlPayParameter;
6
use Integracao\ControlPay\Impl\BasicAuthentication;
7
use Integracao\ControlPay\Impl\KeyQueryStringAuthentication;
8
9
/**
10
 * Class AbstractAPI
11
 * @package Integracao\NTKPedido
12
 */
13
abstract class AbstractAPI
14
{
15
    /**
16
     * @var Client
17
     */
18
    protected $_client;
19
20
    /**
21
     * @var Curl
22
     */
23
    protected $_httpClient;
24
25
    /**
26
     * @var string
27
     */
28
    protected $url;
29
30
    /**
31
     * @var string
32
     */
33
    protected $endpoint;
34
35
    /**
36
     * @var GuzzleHttp\Query
37
     */
38
    protected $query;
39
40
    /**
41
     * @var GuzzleHttp\Message\ResponseInterface
42
     */
43
    protected $response;
44
45
    /**
46
     * @var array
47
     */
48
    protected $headers = [
49
        'Content-Type' => 'application/json; charset=utf-8',
50
        'Cache-Control' => 'no-cache',
51
    ];
52
53
    /**
54
     * AbstractAPI constructor.
55
     * @param $endpoint
56
     * @param Client|null $client
57
     */
58
    protected function __construct($endpoint, Client $client = null)
59
    {
60
        $this->endpoint = $endpoint;
61
        $this->_client = $client;
62
63
        if(is_null($this->_client))
64
            $this->_client = new Client();
65
66
        $this->query = new GuzzleHttp\Query();
67
        $this->query->setEncodingType(false);
68
69
        switch ($this->_client->getParameter(ControlPayParameter::CONTROLPAY_OAUTH_TYPE))
70
        {
71
            case KeyQueryStringAuthentication::class:
72
                $this->query->set('key', $this->_client->getAuthentication());
73
                break;
74
            case BasicAuthentication::class:
75
                $this->headers['Authorization'] = $this->_client->getAuthentication();
76
                break;
77
            default:
78
                $this->query->set('key', $this->_client->getAuthentication());
79
                break;
80
        }
81
82
        $this->_httpClient = new GuzzleHttp\Client([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \GuzzleHttp\Client(a...ery' => $this->query))) of type object<GuzzleHttp\Client> is incompatible with the declared type object<Integracao\ControlPay\Curl> of property $_httpClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83
            'base_url' => sprintf("%s/%s/",
84
                $client->getParameter(ControlPayParameter::CONTROLPAY_HOST),
0 ignored issues
show
Bug introduced by
It seems like $client is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
85
                $this->endpoint
86
            ),
87
            'timeout' => $client->getParameter(ControlPayParameter::CONTROLPAY_TIMEOUT),
88
            'verify' => false,
89
            'defaults' => [
90
                'headers' => $this->headers,
91
                'query' => $this->query
92
            ]
93
        ]);
94
    }
95
96
    /**
97
     * Adiciona parâmetros mantendo os já inicializados como padrão
98
     *
99
     * @param array $params
100
     * @return string
101
     */
102
    protected function addQueryAdditionalParameters(array $params)
103
    {
104
        $this->query = new GuzzleHttp\Query();
105
106
        if(isset($this->_httpClient->getDefaultOption()['query']))
107
            $this->query = $this->_httpClient->getDefaultOption()['query'];
108
109
        foreach ($params as $key => $value)
110
            $this->query->set($key, $value);
111
112
        return $this->query;
113
    }
114
115
    /**
116
     * @return GuzzleHttp\Message\ResponseInterface
117
     */
118
    public function getResponse()
119
    {
120
        return $this->response;
121
    }
122
123
}