GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 65dc27...4e2c47 )
by Lanre
05:32
created

FetchAllPlans   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 32 4
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