|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Gbowo\Adapter\Paystack\Plugin; |
|
5
|
|
|
|
|
6
|
|
|
use function GuzzleHttp\json_decode; |
|
7
|
|
|
use Gbowo\Plugin\AbstractFetchAllPlans; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Gbowo\Exception\InvalidHttpResponseException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Lanre Adelowo <[email protected]> |
|
13
|
|
|
* Class FetchAllPlans |
|
14
|
|
|
* @package Gbowo\Adapter\Paystack\Plugin |
|
15
|
|
|
*/ |
|
16
|
|
|
class FetchAllPlans extends AbstractFetchAllPlans |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
const FETCH_ALL_PLANS_RELATIVE_LINK = "/plan"; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Gbowo\Contract\Adapter\AdapterInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $adapter; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $baseUrl; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* FetchAllPlans constructor. |
|
33
|
|
|
* @param string $baseUrl The base api link |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function __construct(string $baseUrl) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->baseUrl = $baseUrl; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $queryParams |
|
42
|
|
|
* @return mixed |
|
43
|
|
|
* @throws \Gbowo\Exception\InvalidHttpResponseException if the HTTP response status code is not 200 |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function handle(array $queryParams = []) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$params = "?"; |
|
48
|
|
|
|
|
49
|
2 |
|
foreach ($queryParams as $key => $value) { |
|
50
|
1 |
|
if (end($queryParams) !== $value) { |
|
51
|
1 |
|
$params .= "&{$key}={$value}"; |
|
52
|
|
|
} else { |
|
53
|
1 |
|
$params .= "&{$key}={$value}"; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
//the loop returns a string formatted as "?&page=1&clowns=yes". |
|
58
|
|
|
//Even though there is an initial `&` - after the query string . |
|
59
|
|
|
//It don't got no real meaning as it shouldn't affect the response. |
|
60
|
|
|
//Just to keep stuff neat, we stripping it out. |
|
61
|
2 |
|
$params = str_replace("?&", "?", $params); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var ResponseInterface $response |
|
65
|
|
|
*/ |
|
66
|
2 |
|
$response = $this->adapter->getHttpClient() |
|
67
|
2 |
|
->get($this->baseUrl . self::FETCH_ALL_PLANS_RELATIVE_LINK . $params); |
|
68
|
|
|
|
|
69
|
2 |
|
if (200 !== $response->getStatusCode()) { |
|
70
|
1 |
|
throw new InvalidHttpResponseException( |
|
71
|
1 |
|
"Expected 200. Got {$response->getStatusCode()} instead" |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
return json_decode($response->getBody(), true)['data']; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|