Completed
Push — master ( 8e1cce...aa0857 )
by Raza
08:07
created

PayPalHttpClient::doPayPalRequest()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 2
eloc 10
nc 3
nop 1
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
     * Function to initialize Http Client.
14
     *
15
     * @return void
16
     */
17
    protected function setClient()
18
    {
19
        $this->client = new HttpClient([
0 ignored issues
show
Bug introduced by
The property client 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...
20
            'curl' => $this->httpClientConfig,
0 ignored issues
show
Bug introduced by
The property httpClientConfig does not seem to exist. Did you mean client?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
21
        ]);
22
    }
23
24
    /**
25
     * Function to set Http Client configuration.
26
     *
27
     * @return void
28
     */
29
    protected function setHttpClientConfiguration()
30
    {
31
        $this->httpClientConfig = [
0 ignored issues
show
Bug introduced by
The property httpClientConfig does not seem to exist. Did you mean client?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
32
            CURLOPT_SSLVERSION     => CURL_SSLVERSION_TLSv1_2,
33
            CURLOPT_SSL_VERIFYPEER => $this->validateSSL,
0 ignored issues
show
Bug introduced by
The property validateSSL 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...
34
        ];
35
36
        if (!empty($this->certificate)) {
37
            $this->httpClientConfig[CURLOPT_SSLCERT] = $this->certificate;
0 ignored issues
show
Bug introduced by
The property httpClientConfig does not seem to exist. Did you mean client?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property certificate 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...
38
        }
39
40
        // Initialize Http Client
41
        $this->setClient();
42
43
        // Set default values.
44
        $this->setDefaultValues();
0 ignored issues
show
Bug introduced by
It seems like setDefaultValues() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
46
        // Set PayPal API Endpoint.
47
        $this->apiUrl = $this->config['api_url'];
0 ignored issues
show
Bug introduced by
The property apiUrl 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...
Bug introduced by
The property config 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...
48
49
        // Set PayPal IPN Notification URL
50
        $this->notifyUrl = $this->config['notify_url'];
0 ignored issues
show
Bug introduced by
The property notifyUrl 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...
51
    }
52
53
    /**
54
     * Perform PayPal API request & return response.
55
     *
56
     * @throws \Exception
57
     *
58
     * @return \Psr\Http\Message\StreamInterface
59
     */
60
    private function makeHttpRequest()
61
    {
62
        try {
63
            return $this->client->post($this->apiUrl, [
64
                $this->httpBodyParam => $this->post->toArray(),
0 ignored issues
show
Bug introduced by
The property httpBodyParam 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...
Bug introduced by
The property post 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...
65
            ])->getBody();
66
        } catch (HttpClientException $e) {
67
            throw new \Exception($e->getRequest().' '.$e->getResponse());
68
        } catch (HttpServerException $e) {
69
            throw new \Exception($e->getRequest().' '.$e->getResponse());
70
        } catch (HttpBadResponseException $e) {
71
            throw new \Exception($e->getRequest().' '.$e->getResponse());
72
        }
73
    }
74
75
    /**
76
     * Function To Perform PayPal API Request.
77
     *
78
     * @param string $method
79
     *
80
     * @throws \Exception
81
     *
82
     * @return array|\Psr\Http\Message\StreamInterface
83
     */
84
    private function doPayPalRequest($method)
85
    {
86
        // Setup PayPal API Request Payload
87
        $this->createRequestPayload($method);
0 ignored issues
show
Bug introduced by
It seems like createRequestPayload() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88
89
        try {
90
            // Perform PayPal HTTP API request.
91
            $response = $this->makeHttpRequest();
92
93
            return $this->retrieveData($method, $response);
0 ignored issues
show
Bug introduced by
It seems like retrieveData() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
94
        } catch (\Exception $e) {
95
            $message = collect($e->getTrace())->implode('\n');
96
        }
97
98
        return [
99
            'type'    => 'error',
100
            'message' => $message,
101
        ];
102
    }
103
}