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

PayPalHttpClient::sendRequest()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 8
cp 0
rs 9.7333
c 0
b 0
f 0
cc 2
nc 3
nop 0
crap 6
1
<?php
2
3
namespace Srmklive\PayPal\Traits;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\BadResponseException as HttpBadResponseException;
7
use GuzzleHttp\Exception\ClientException as HttpClientException;
8
use GuzzleHttp\Exception\ServerException as HttpServerException;
9
10
trait PayPalHttpClient
11
{
12
    /**
13
     * @var \GuzzleHttp\Client
14
     */
15
    public $client;
16
17
    /**
18
     * @var array
19
     */
20
    protected $params;
21
22
    /**
23
     * @var string
24
     */
25
    protected $apiUrl;
26
27
    /**
28
     * @var string
29
     */
30
    protected $accessToken;
31
32
    /**
33
     * Function to initialize Http Client.
34
     *
35
     * @return void
36
     */
37
    protected function setClient()
38
    {
39
        $this->client = new HttpClient([
40
            'curl' => $this->curlConfig,
0 ignored issues
show
Bug introduced by
The property curlConfig does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
        ]);
42
    }
43
44
    /**
45
     * Function to set Http Client configuration.
46
     *
47
     * @return void
48
     */
49
    protected function setHttpClientConfiguration()
50
    {
51
        $this->curlConfig = [
52
            CURLOPT_SSLVERSION     => CURL_SSLVERSION_TLSv1_2,
53
            CURLOPT_SSL_VERIFYPEER => $this->credentials['validate_ssl'],
0 ignored issues
show
Bug introduced by
The property credentials does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
        ];
55
56
        // Initialize Http Client
57
        $this->setClient();
58
    }
59
60
    /**
61
     * Perform PayPal API request & return response.
62
     *
63
     * @throws \Exception
64
     *
65
     * @return \Psr\Http\Message\StreamInterface
66
     */
67
    private function makeHttpRequest()
68
    {
69
        try {
70
            return $this->client->post($this->apiUrl, $this->params)->getBody();
71
        } catch (HttpClientException $e) {
72
            throw new \Exception($e->getRequest().' '.$e->getResponse());
73
        } catch (HttpServerException $e) {
74
            throw new \Exception($e->getRequest().' '.$e->getResponse());
75
        } catch (HttpBadResponseException $e) {
76
            throw new \Exception($e->getRequest().' '.$e->getResponse());
77
        }
78
    }
79
80
    /**
81
     * Function To Perform PayPal API Request.
82
     *
83
     * @throws \Exception
84
     *
85
     * @return array|\Psr\Http\Message\StreamInterface
86
     */
87
    public function sendRequest()
88
    {
89
        try {
90
            // Perform PayPal HTTP API request.
91
            $response = $this->makeHttpRequest();
92
93
            return \GuzzleHttp\json_decode($response, true);
94
        } catch (\Exception $e) {
95
            $message = collect($e->getTrace())->implode('\n');
96
        }
97
98
        return [
99
            'type'    => 'error',
100
            'message' => $message,
101
        ];
102
    }
103
}
104