Completed
Push — v2 ( eb8d0a...4518aa )
by Raza
07:17
created

PayPalRestAPI   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 17.54%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 156
ccs 10
cts 57
cp 0.1754
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 5
A setApiCredentials() 0 23 3
A setLocale() 0 8 2
A getLocale() 0 4 1
A accessToken() 0 24 1
A setResponse() 0 4 1
A getResponse() 0 4 1
1
<?php
2
3
namespace Srmklive\PayPal\Services;
4
5
use Srmklive\PayPal\Traits\PayPalHttpClient;
6
use Srmklive\PayPal\Traits\PayPalLocales;
7
8
class PayPalRestAPI
9
{
10
    use PayPalHttpClient, PayPalLocales;
11
12
    /**
13
     * @var array
14
     */
15
    protected $curlConfig;
16
17
    /**
18
     * @var bool
19
     */
20
    public $sandbox;
21
22
    /**
23
     * @var array
24
     */
25
    protected $credentials;
26
27
    /**
28
     * @var string
29
     */
30
    protected $endpoints;
31
32
    /**
33
     * @var string
34
     */
35
    protected $locale;
36
37
    /**
38
     * @var array
39
     */
40
    protected $response;
41
42
    /**
43
     * PayPalRestAPI constructor.
44
     *
45
     * @throws \Exception
46
     */
47 3
    public function __construct()
48
    {
49 3
        if (function_exists('config')) {
50
            $mode = config('paypal.mode');
51
            if (empty($mode) || !in_array($mode, ['sandbox', 'live'])) {
52
                $mode = 'live';
53
            }
54
55
            $sandbox = ($mode === 'sandbox') ? true : false;
56
            $credentials = config('paypal.'.$mode);
57
            $credentials['validate_ssl'] = config('paypal.validate_ssl');
58
            $credentials['locale'] = config('paypal.locale');
59
60
            $this->setApiCredentials($credentials, $sandbox);
61
        }
62 3
    }
63
64
    /**
65
     * Set PayPal Rest API credentials & endpoints.
66
     *
67
     * @param array $credentials
68
     * @param bool  $sandbox
69
     *
70
     * @throws \Exception
71
     */
72
    public function setApiCredentials($credentials, $sandbox)
73
    {
74
        if (!empty($credentials['locale'])) {
75
            $this->setLocale($credentials['locale']);
76
        }
77
78
        $this->credentials = $credentials;
79
        $this->sandbox = $sandbox;
80
81
        if ($this->sandbox === true) {
82
            $this->endpoints = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('rest' => 'https:/...ww.sandbox.paypal.com') of type array<string,string,{"re...","redirect":"string"}> is incompatible with the declared type string of property $endpoints.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83
                'rest'     => 'https://api.sandbox.paypal.com/',
84
                'redirect' => 'https://www.sandbox.paypal.com',
85
            ];
86
        } else {
87
            $this->endpoints = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('rest' => 'https:/...ttps://www.paypal.com') of type array<string,string,{"re...","redirect":"string"}> is incompatible with the declared type string of property $endpoints.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
88
                'rest'     => 'https://api.paypal.com/',
89
                'redirect' => 'https://www.paypal.com',
90
            ];
91
        }
92
93
        $this->setHttpClientConfiguration();
94
    }
95
96
    /**
97
     * Set default locale.
98
     *
99
     * @param string $locale
100
     *
101
     * @throws \Exception
102
     */
103 2
    public function setLocale($locale)
104
    {
105 2
        if (!in_array($locale, $this->locales())) {
106 1
            throw new \Exception('Invalid locale');
107
        }
108
109 1
        $this->locale = $locale;
110 1
    }
111
112
    /**
113
     * @return string
114
     */
115 1
    public function getLocale()
116
    {
117 1
        return $this->locale;
118
    }
119
120
    /**
121
     * @throws \Exception
122
     */
123
    protected function accessToken()
124
    {
125
        $this->apiUrl = $this->endpoints['rest'].'v1/oauth2/token';
126
127
        $this->params = [
128
            'auth' => [
129
                $this->credentials['client'],
130
                $this->credentials['secret'],
131
            ],
132
            'headers' => [
133
                'Accept'          => 'application/json',
134
                'Accept-Language' => $this->locale,
135
            ],
136
            'form_params' => [
137
                'grant_type' => 'client_credentials',
138
            ],
139
        ];
140
141
        $this->setResponse(
142
            $this->sendRequest()
0 ignored issues
show
Bug introduced by
It seems like $this->sendRequest() targeting Srmklive\PayPal\Traits\P...tpClient::sendRequest() can also be of type object<Psr\Http\Message\StreamInterface>; however, Srmklive\PayPal\Services...lRestAPI::setResponse() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
143
        );
144
145
        $this->accessToken = $this->response['access_token'];
146
    }
147
148
    /**
149
     * @param array $response
150
     */
151
    protected function setResponse($response)
152
    {
153
        $this->response = $response;
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    public function getResponse()
160
    {
161
        return $this->response;
162
    }
163
}
164