Passed
Push — v2.0 ( cf221d...00ccf5 )
by Raza
02:12
created

PayPalAPI::setAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Srmklive\PayPal\Traits;
4
5
trait PayPalAPI
6
{
7 1
    use PayPalAPI\Trackers;
8
    use PayPalAPI\CatalogProducts;
9
    use PayPalAPI\Disputes;
10
    use PayPalAPI\DisputesActions;
11
    use PayPalAPI\Invoices;
12
    use PayPalAPI\InvoicesSearch;
13
    use PayPalAPI\InvoicesTemplates;
14
    use PayPalAPI\PaymentAuthorizations;
15
    use PayPalAPI\PaymentCaptures;
16
    use PayPalAPI\PaymentRefunds;
17
    use PayPalAPI\BillingPlans;
18
    use PayPalAPI\Subscriptions;
19
    use PayPalAPI\Reporting;
20
    use PayPalAPI\WebHooks;
21
    use PayPalAPI\WebHooksVerification;
22
    use PayPalAPI\WebHooksEvents;
23
24
    /**
25
     * Login through PayPal API to get access token.
26
     *
27
     * @throws \Throwable
28
     *
29
     * @return array|\Psr\Http\Message\StreamInterface|string
30
     *
31
     * @see https://developer.paypal.com/docs/api/get-an-access-token-curl/
32
     * @see https://developer.paypal.com/docs/api/get-an-access-token-postman/
33
     */
34
    public function getAccessToken()
35
    {
36
        $this->apiEndPoint = 'v1/oauth2/token';
1 ignored issue
show
Bug Best Practice introduced by
The property apiEndPoint does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/');
1 ignored issue
show
Bug Best Practice introduced by
The property apiUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
39
        $this->options['auth'] = [$this->config['client_id'], $this->config['client_secret']];
1 ignored issue
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
        $this->options[$this->httpBodyParam] = [
41
            'grant_type' => 'client_credentials',
42
        ];
43
44 1
        $response = $this->doPayPalRequest();
1 ignored issue
show
Bug introduced by
It seems like doPayPalRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        $response = $this->doPayPalRequest();
Loading history...
45
46
        if (isset($response['access_token'])) {
47
            $this->setAccessToken($response);
48
49
            $this->setPayPalAppId($response);
50
        }
51
52
        return $response;
53
    }
54
55
    /**
56
     * Set PayPal Rest API access token.
57
     *
58
     * @param array $response
59
     *
60
     * @return void
61
     */
62
    public function setAccessToken($response)
63
    {
64
        $this->access_token = $response['access_token'];
1 ignored issue
show
Bug Best Practice introduced by
The property access_token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
65
66
        $this->options['headers']['Authorization'] = "{$response['token_type']} {$this->access_token}";
1 ignored issue
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
    }
68
69
    /**
70
     * Set PayPal App ID.
71
     *
72
     * @param array $response
73
     *
74
     * @return void
75
     */
76
    private function setPayPalAppId($response)
77
    {
78
        if (empty($this->config['app_id'])) {
79
            $this->config['app_id'] = $response['app_id'];
1 ignored issue
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
        }
81
    }
82
}
83