AbstractAPI::httpClientInitialize()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
namespace PayGo;
4
5
use GuzzleHttp;
6
use PayGo\Transactions\Constants\PayGoTransactionParameterConst;
7
8
/**
9
 * Class AbstractAPI
10
 * @package PayGo
11
 */
12
abstract class AbstractAPI
13
{
14
    /**
15
     * @var Client
16
     */
17
    protected $client;
18
19
    /**
20
     * @var GuzzleHttp\Client
21
     */
22
    protected $httpClient;
23
24
    /**
25
     * @var string
26
     */
27
    protected $url;
28
29
    /**
30
     * @var string
31
     */
32
    protected $endpoint;
33
34
    /**
35
     * @var array
36
     */
37
    protected $headers = [
38
        'Content-Type' => 'application/json; charset=utf-8',
39
        'Accept' => 'application/json',
40
    ];
41
42
    /**
43
     * AbstractAPI constructor.
44
     *
45
     * @param null $endpoint
46
     * @param Client|null $client
47
     * @throws \Exception
48
     */
49
    protected function __construct($endpoint = null, Client $client = null)
50
    {
51
        $this->httpClientInitialize($endpoint, $client);
52
    }
53
54
    /**
55
     * @param null $endpoint
56
     * @param Client|null $client
57
     * @throws \Exception
58
     */
59
    private function httpClientInitialize($endpoint = null, Client $client = null)
60
    {
61
        $this->endpoint = $endpoint;
62
        $this->client = $client;
63
64
        if(is_null($this->client))
65
            $this->client = new Client();
66
67
        $this->headers['x-application-key'] =  $this->client->getParameter(
68
            PayGoTransactionParameterConst::PAYGO_TRANSACTIONS_TOKEN);
69
70
        $baseUrl = $this->client->getParameter(
71
            PayGoTransactionParameterConst::PAYGO_TRANSACTIONS_HOST);
72
73
        if(!empty($endpoint))
74
            $baseUrl = sprintf("%s%s/", $baseUrl, $endpoint);
75
76
        $this->httpClient = new GuzzleHttp\Client([
77
            'base_url' => $baseUrl,
78
            'timeout' => $this->client->getParameter(
79
                PayGoTransactionParameterConst::PAYGO_TRANSACTIONS_TIMEOUT),
80
            'verify' => false,
81
            'defaults' => [
82
                'headers' => $this->headers,
83
                'exceptions' => false,
84
            ]
85
        ]);
86
    }
87
88
}