Passed
Push — master ( 29b75e...0afa38 )
by Simon
02:03
created

Client::applyPagination()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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