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.

UnsubscribeCustomer::getPluginAccessor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gbowo\Adapter\Amplifypay\Plugin;
4
5
use Gbowo\Plugin\AbstractPlugin;
6
use function GuzzleHttp\json_decode;
7
use function GuzzleHttp\json_encode;
8
use Gbowo\Adapter\Amplifypay\Traits\KeyVerifier;
9
use Gbowo\Exception\InvalidHttpResponseException;
10
use Gbowo\Exception\TransactionVerficationFailedException;
11
12
class UnsubscribeCustomer extends AbstractPlugin
13
{
14
    use KeyVerifier;
15
16
    const UN_SUBSCRIBE_LINK = "/subscription/cancel";
17
18
    /**
19
     * It's with a double `ll`
20
     * @see https://amplifypay.com/developers Unsubscribe a customer from plan
21
     * @var string
22
     */
23
    const STATUS_DESCRIPTION_SUCCESS = "Successfull Request";
24
25
    const STATUS_DESCRIPTION_FAILURE = "Unsuccessfull Request";
26
27
    protected $baseUrl;
28
29
    protected $apiKeys;
30
31 25
    public function __construct(string $baseUrl, array $apiKeys)
32
    {
33 25
        $this->baseUrl = $baseUrl;
34 25
        $this->apiKeys = $apiKeys;
35 25
    }
36
37 25
    public function getPluginAccessor() :string
38
    {
39 25
        return "unsubcribeCustomerFromPlan";
40
    }
41
42
    /**
43
     * @param array $args
44
     * @return mixed
45
     * @throws \Gbowo\Exception\TransactionVerficationFailedException
46
     * @throws \Gbowo\Exception\InvalidHttpResponseException if we don't get a 200 Status code
47
     */
48 3
    public function handle(array $args)
49
    {
50 3
        $link = $this->baseUrl . self::UN_SUBSCRIBE_LINK;
51
52 3
        $response = $this->adapter->getHttpClient()
53 3
            ->post(
54 3
                $link, [
55 3
                'body' => json_encode(array_merge($this->apiKeys, $args))
56
                ]
57
            );
58
59 3
        if ($response->getStatusCode() !== 200) {
60 1
            throw InvalidHttpResponseException::createFromResponse($response);
61
        }
62
63 2
        $res = json_decode($response->getBody(), true);
64
65 2
        $validated = false;
66
67 2
        if (strcmp($res['StatusDesc'], self::STATUS_DESCRIPTION_SUCCESS) === 0) {
68 1
            $validated = true;
69
        }
70
71 2
        if (false === $validated) {
72 1
            throw TransactionVerficationFailedException::createFromResponse($response);
73
        }
74
75
        //not consistent, some transaction with the Amplifypay API returns `ApiKey`, some `apiKey`.
76
77 1
        $this->verifyKeys($res['apiKey'], $this->apiKeys['apiKey']);
78
79 1
        return $res;
80
    }
81
}
82