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::handle()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 6
nop 1
dl 0
loc 32
ccs 13
cts 13
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
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