Completed
Push — master ( 55d906...05528d )
by Raza
01:37
created

PayPalHttpClient::setHttpClientConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
     * Set curl constants if not defined.
14
     *
15
     * @return void
16
     */
17
    protected function setCurlConstants()
18
    {
19
        if (!defined('CURLOPT_SSLVERSION')) {
20
            define('CURLOPT_SSLVERSION', 32);
21
        }
22
23
        if (!defined('CURL_SSLVERSION_TLSv1_2')) {
24
            define('CURL_SSLVERSION_TLSv1_2', 6);
25
        }
26
27
        if (!defined('CURLOPT_SSL_VERIFYPEER')) {
28
            define('CURLOPT_SSL_VERIFYPEER', 64);
29
        }
30
31
        if (!defined('CURLOPT_SSLCERT')) {
32
            define('CURLOPT_SSLCERT', 10025);
33
        }
34
    }
35
    
36
    /**
37
     * Function to initialize Http Client.
38
     *
39
     * @return void
40
     */
41
    protected function setClient()
42
    {
43
        $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...
44
            '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...
45
        ]);
46
    }
47
48
    /**
49
     * Function to set Http Client configuration.
50
     *
51
     * @return void
52
     */
53
    protected function setHttpClientConfiguration()
54
    {
55
        $this->defineCurlConstants();
0 ignored issues
show
Bug introduced by
It seems like defineCurlConstants() 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...
56
        
57
        $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...
58
            CURLOPT_SSLVERSION     => CURL_SSLVERSION_TLSv1_2,
59
            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...
60
        ];
61
62
        if (!empty($this->certificate)) {
63
            $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...
64
        }
65
66
        // Initialize Http Client
67
        $this->setClient();
68
69
        // Set default values.
70
        $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...
71
72
        // Set PayPal API Endpoint.
73
        $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...
74
75
        // Set PayPal IPN Notification URL
76
        $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...
77
    }
78
79
    /**
80
     * Perform PayPal API request & return response.
81
     *
82
     * @throws \Exception
83
     *
84
     * @return \Psr\Http\Message\StreamInterface
85
     */
86
    private function makeHttpRequest()
87
    {
88
        try {
89
            return $this->client->post($this->apiUrl, [
90
                $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...
91
            ])->getBody();
92
        } catch (HttpClientException $e) {
93
            throw new \Exception($e->getRequest().' '.$e->getResponse());
94
        } catch (HttpServerException $e) {
95
            throw new \Exception($e->getRequest().' '.$e->getResponse());
96
        } catch (HttpBadResponseException $e) {
97
            throw new \Exception($e->getRequest().' '.$e->getResponse());
98
        }
99
    }
100
101
    /**
102
     * Function To Perform PayPal API Request.
103
     *
104
     * @param string $method
105
     *
106
     * @throws \Exception
107
     *
108
     * @return array|\Psr\Http\Message\StreamInterface
109
     */
110
    private function doPayPalRequest($method)
111
    {
112
        // Setup PayPal API Request Payload
113
        $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...
114
115
        try {
116
            // Perform PayPal HTTP API request.
117
            $response = $this->makeHttpRequest();
118
119
            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...
120
        } catch (\Exception $e) {
121
            $message = collect($e->getTrace())->implode('\n');
122
        }
123
124
        return [
125
            'type'    => 'error',
126
            'message' => $message,
127
        ];
128
    }
129
}
130