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.

PaystackAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 15.12 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 13
loc 86
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A registerPlugins() 0 5 1
A charge() 0 8 1
A decodeResponse() 0 4 1
A getHttpClient() 0 4 1
A setHttpClient() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Gbowo\Adapter\Paystack;
4
5
use GuzzleHttp\Client;
6
use function Gbowo\env;
7
use Gbowo\Traits\Pluggable;
8
use Gbowo\Contract\Adapter\AdapterInterface;
9
use function GuzzleHttp\json_decode;
10
use Psr\Http\Message\ResponseInterface;
11
use Gbowo\Adapter\Paystack\Traits\Payable;
12
use Gbowo\Adapter\Paystack\Plugin\GetPaymentData;
13
use Gbowo\Adapter\Paystack\Plugin\ChargeWithToken;
14
use Gbowo\Adapter\Paystack\Traits\VerifyHttpStatusResponseCode;
15
16
/**
17
 * @method findCustomer(int $customerId)
18
 * @method getPaymentData(string $transactionReference)
19
 * @method chargeWithToken(array $data)
20
 * @author Lanre Adelowo <[email protected]>
21
 * Class PaystackAdapter
22
 * @package Gbowo\Adapter\Paystack
23
 */
24
class PaystackAdapter implements AdapterInterface
25
{
26
    use Pluggable, Payable, VerifyHttpStatusResponseCode;
27
28
    /**
29
     * @var string
30
     */
31
    const API_LINK = 'https://api.paystack.co';
32
33
    /**
34
     * @var \GuzzleHttp\Client
35
     */
36
    protected $httpClient;
37
38
    /**
39
     * The Api link for paystack
40
     * @var string
41
     */
42
    protected $baseUrl;
43
44
    /**
45
     * PaystackAdapter constructor.
46
     * @param \GuzzleHttp\Client|null $client
47
     */
48 25
    public function __construct(Client $client = null)
49
    {
50 25
        $this->baseUrl = self::API_LINK;
51 25
        $this->httpClient = $client ?? $this->setHttpClient(env("PAYSTACK_SECRET_KEY"));
52 25
        $this->registerPlugins();
53 25
    }
54
55 25
    protected function registerPlugins()
56
    {
57 25
        $this->addPlugin(new GetPaymentData($this->baseUrl))
58 25
            ->addPlugin(new ChargeWithToken($this->baseUrl));
59 25
    }
60
61
    /**
62
     * @param array $data
63
     * @return string The authorization url to render the secure payment gateway.
64
     */
65 2
    public function charge(array $data)
66
    {
67 2
        $response = $this->authorizeTransaction("/transaction/initialize", $data);
68
69 2
        $this->verifyResponse($response);
70
71 1
        return $this->decodeResponse($response)['data']['authorization_url'];
72
    }
73
74
    /**
75
     * @param \Psr\Http\Message\ResponseInterface $response
76
     * @return mixed
77
     */
78 1
    protected function decodeResponse(ResponseInterface $response)
79
    {
80 1
        return json_decode($response->getBody(), true);
81
    }
82
83
    /**
84
     * @return \GuzzleHttp\Client
85
     */
86 16
    public function getHttpClient(): Client
87
    {
88 16
        return $this->httpClient;
89
    }
90
91
    /**
92
     * @codeCoverageIgnore
93
     * @param string $token
94
     * @return \GuzzleHttp\Client
95
     */
96 View Code Duplication
    protected function setHttpClient(string $token) : Client
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        return new Client(
99
            [
100
            'base_uri' => $this->baseUrl,
101
            'headers' => [
102
                'Authorization' => 'Bearer ' . $token,
103
                'Content-Type' => 'application/json',
104
                'Accept' => 'application/json'
105
            ]
106
            ]
107
        );
108
    }
109
}
110