Client   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 169
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 8 1
A buildBaseUrl() 0 3 1
A cleanPagination() 0 7 1
A setClient() 0 3 1
A initiateHttpClient() 0 11 1
A __construct() 0 11 2
A get() 0 8 1
A handleResponse() 0 6 1
A applyPagination() 0 7 2
A getApiKey() 0 3 1
1
<?php
2
3
namespace PomeloPHP;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\Psr7\Response;
7
8
class Client
9
{
10
    const POMELO_API_VERSION = '2.0';
11
    const POMELO_APP_VERSION = 'pomelo-php';
12
    const POMELO_SIGN_METHOD = 'sha1';
13
    const POMELO_SANDBOX_ENDPOINT = 'https://api.sandbox.pomelopay.com/';
14
    const POMELO_PRODUCTION_ENDPOINT = 'https://api.pomelopay.com/';
15
16
    /**
17
     * @var \GuzzleHttp\Client
18
     */
19
    private $httpClient;
20
21
    /**
22
     * @var string
23
     */
24
    private $apiKey;
25
26
    /**
27
     * @var string
28
     */
29
    private $appId;
30
31
    /**
32
     * @var string
33
     */
34
    private $mode;
35
36
    /**
37
     * @var array
38
     */
39
    private $clientOptions;
40
41
    /**
42
     * @var string
43
     */
44
    private $baseUrl;
45
46
    /**
47
     * @var Transactions
48
     */
49
    public $transactions;
50
51
52
    /**
53
     * Client constructor.
54
     * @param string $apiKey
55
     * @param string $appId
56
     * @param string $mode
57
     * @param array $clientOptions
58
     */
59
    public function __construct(string $apiKey, string $appId, $mode = 'production', array $clientOptions = [])
60
    {
61
        $this->apiKey = $apiKey;
62
        $this->appId = $appId;
63
        $this->mode = $mode;
64
        $this->baseUrl = ($mode === 'production' ? self::POMELO_PRODUCTION_ENDPOINT : self::POMELO_SANDBOX_ENDPOINT);
65
        $this->clientOptions = $clientOptions;
66
67
        $this->initiateHttpClient();
68
69
        $this->transactions = new Transactions($this);
70
    }
71
72
    /**
73
     * @param GuzzleClient $client
74
     */
75
    public function setClient(GuzzleClient $client)
76
    {
77
        $this->httpClient = $client;
78
    }
79
80
    /**
81
     * Initiates the HttpClient with required headers
82
     */
83
    private function initiateHttpClient()
84
    {
85
        $options = [
86
            'headers' => [
87
                'Content-Type' => 'application/json',
88
                'Accept' => 'application/json',
89
                'Authorization' =>  $this->apiKey,
90
            ]
91
        ];
92
93
        $this->httpClient = new GuzzleClient(array_replace_recursive($this->clientOptions, $options));
94
    }
95
96
    private function buildBaseUrl()
97
    {
98
        return $this->baseUrl;
99
    }
100
101
    /**
102
     * @param Response $response
103
     * @return mixed
104
     */
105
    private function handleResponse(Response $response)
106
    {
107
        $stream = \GuzzleHttp\Psr7\stream_for($response->getBody());
108
        $data = json_decode($stream);
109
110
        return $data;
111
    }
112
113
    /**
114
     * @param string $endpoint
115
     * @param array $json
116
     * @return mixed
117
     * @throws \GuzzleHttp\Exception\GuzzleException
118
     */
119
    public function post($endpoint, $json)
120
    {
121
        $json['apiVersion'] = self::POMELO_API_VERSION;
122
        $json['appVersion'] = self::POMELO_APP_VERSION;
123
        $json['signMethod'] = self::POMELO_SIGN_METHOD;
124
125
        $response = $this->httpClient->request('POST', $this->buildBaseUrl().$endpoint, ['json' => $json]);
126
        return $this->handleResponse($response);
127
    }
128
129
    /**
130
     * @param string $endpoint
131
     * @param array $pagination
132
     * @return mixed
133
     */
134
    public function get(string $endpoint, array $pagination = [])
135
    {
136
        $response = $this->httpClient->request(
137
            'GET',
138
            $this->applyPagination($this->buildBaseUrl().$endpoint, $pagination)
139
        );
140
141
        return $this->handleResponse($response);
142
    }
143
144
    /**
145
     * @param string $url
146
     * @param array $pagination
147
     * @return string
148
     */
149
    private function applyPagination(string $url, array $pagination)
150
    {
151
        if (count($pagination)) {
152
            return $url.'?'.http_build_query($this->cleanPagination($pagination));
153
        }
154
155
        return $url;
156
    }
157
158
    /**
159
     * @param array $pagination
160
     * @return array
161
     */
162
    private function cleanPagination(array $pagination)
163
    {
164
        $allowed = [
165
            'page',
166
        ];
167
168
        return array_intersect_key($pagination, array_flip($allowed));
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getApiKey()
175
    {
176
        return $this->apiKey;
177
    }
178
}
179