|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yabacon\Paystack\Http; |
|
4
|
|
|
|
|
5
|
|
|
use \Yabacon\Paystack\Contracts\RouteInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Request |
|
8
|
|
|
{ |
|
9
|
|
|
public $method; |
|
10
|
|
|
public $endpoint; |
|
11
|
|
|
public $body = ''; |
|
12
|
|
|
public $headers = []; |
|
13
|
|
|
protected $response; |
|
14
|
|
|
protected $paystackObj; |
|
15
|
|
|
|
|
16
|
7 |
|
public function __construct($paystackObj = null) |
|
17
|
|
|
{ |
|
18
|
7 |
|
$this->response = new Response(); |
|
19
|
7 |
|
$this->paystackObj = $paystackObj; |
|
20
|
7 |
|
$this->response->forApi = !is_null($paystackObj); |
|
21
|
7 |
|
if ($this->response->forApi) { |
|
22
|
3 |
|
$this->headers['Content-Type'] = 'application/json'; |
|
23
|
3 |
|
} |
|
24
|
7 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getResponse() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->response; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
protected function flattenedHeaders() |
|
32
|
|
|
{ |
|
33
|
1 |
|
$_ = []; |
|
34
|
1 |
|
foreach ($this->headers as $key => $value) { |
|
35
|
1 |
|
$_[] = $key . ": " . $value; |
|
36
|
1 |
|
} |
|
37
|
1 |
|
return $_; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function send() |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->attemptGuzzle(); |
|
43
|
1 |
|
if (!$this->response->okay) { |
|
44
|
1 |
|
$this->attemptCurl(); |
|
45
|
1 |
|
} |
|
46
|
1 |
|
if (!$this->response->okay) { |
|
47
|
1 |
|
$this->attemptFileGetContents(); |
|
48
|
|
|
} |
|
49
|
|
|
return $this->response; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function attemptGuzzle() |
|
53
|
|
|
{ |
|
54
|
1 |
|
if (!isset($this->paystackObj) || !$this->paystackObj->use_guzzle) { |
|
55
|
1 |
|
$this->response->okay = false; |
|
56
|
1 |
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
if (class_exists('\\GuzzleHttp\\Exception\\BadResponseException') |
|
59
|
|
|
&& class_exists('\\GuzzleHttp\\Exception\\ClientException') |
|
60
|
|
|
&& class_exists('\\GuzzleHttp\\Exception\\ConnectException') |
|
61
|
|
|
&& class_exists('\\GuzzleHttp\\Exception\\RequestException') |
|
62
|
|
|
&& class_exists('\\GuzzleHttp\\Exception\\ServerException') |
|
63
|
|
|
&& class_exists('\\GuzzleHttp\\Client') |
|
64
|
|
|
&& class_exists('\\GuzzleHttp\\Psr7\\Request') |
|
65
|
|
|
) { |
|
66
|
|
|
$request = new \GuzzleHttp\Psr7\Request( |
|
67
|
|
|
strtoupper($this->method), |
|
68
|
|
|
$this->endpoint, |
|
69
|
|
|
$this->headers, |
|
70
|
|
|
$this->body |
|
71
|
|
|
); |
|
72
|
|
|
$client = new \GuzzleHttp\Client(); |
|
73
|
|
|
try { |
|
74
|
|
|
$psr7response = $client->send($request); |
|
75
|
|
|
$this->response->body = $psr7response->getBody()->getContents(); |
|
76
|
|
|
$this->response->okay = true; |
|
77
|
|
|
} catch (\Exception $e) { |
|
78
|
|
|
if ($e->hasResponse()) { |
|
|
|
|
|
|
79
|
|
|
$psr7response = $e->getResponse(); |
|
|
|
|
|
|
80
|
|
|
$this->response->body = $psr7response->getBody()->getContents(); |
|
81
|
|
|
} |
|
82
|
|
|
if (($e instanceof \GuzzleHttp\Exception\BadResponseException |
|
83
|
|
|
|| $e instanceof \GuzzleHttp\Exception\ClientException |
|
84
|
|
|
|| $e instanceof \GuzzleHttp\Exception\ConnectException |
|
85
|
|
|
|| $e instanceof \GuzzleHttp\Exception\RequestException |
|
86
|
|
|
|| $e instanceof \GuzzleHttp\Exception\ServerException) |
|
87
|
|
|
) { |
|
88
|
|
|
$this->response->okay = true; |
|
89
|
|
|
} |
|
90
|
|
|
$this->response->messages[] = $e->getMessage(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public function attemptFileGetContents() |
|
96
|
|
|
{ |
|
97
|
1 |
|
$context = stream_context_create( |
|
98
|
|
|
[ |
|
99
|
|
|
'http'=>array( |
|
100
|
1 |
|
'method'=>$this->method, |
|
101
|
1 |
|
'header'=>$this->flattenedHeaders(), |
|
102
|
1 |
|
'content'=>$this->body, |
|
103
|
|
|
'ignore_errors' => true |
|
104
|
1 |
|
) |
|
105
|
1 |
|
] |
|
106
|
1 |
|
); |
|
107
|
1 |
|
$this->response->body = file_get_contents($this->endpoint, false, $context); |
|
108
|
|
|
if ($this->response->body === false) { |
|
109
|
|
|
$this->response->messages[] = 'file_get_contents failed with response: \'' . error_get_last() . '\'.'; |
|
110
|
|
|
} else { |
|
111
|
|
|
$this->response->okay = true; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
public function attemptCurl() |
|
116
|
|
|
{ |
|
117
|
|
|
//open connection |
|
118
|
1 |
|
$ch = \curl_init(); |
|
119
|
1 |
|
\curl_setopt($ch, \CURLOPT_URL, $this->endpoint); |
|
120
|
1 |
|
($this->method === RouteInterface::POST_METHOD) && \curl_setopt($ch, \CURLOPT_POST, true); |
|
121
|
1 |
|
($this->method === RouteInterface::PUT_METHOD) && \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
122
|
|
|
|
|
123
|
1 |
|
if ($this->method !== RouteInterface::GET_METHOD) { |
|
124
|
1 |
|
\curl_setopt($ch, \CURLOPT_POSTFIELDS, $this->body); |
|
125
|
1 |
|
} |
|
126
|
1 |
|
\curl_setopt($ch, \CURLOPT_HTTPHEADER, $this->flattenedHeaders()); |
|
127
|
1 |
|
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
|
128
|
1 |
|
$this->response->forApi && \curl_setopt($ch, \CURLOPT_SSLVERSION, 6); |
|
129
|
|
|
|
|
130
|
1 |
|
$this->response->body = \curl_exec($ch); |
|
131
|
|
|
|
|
132
|
1 |
|
if (\curl_errno($ch)) { |
|
133
|
1 |
|
$cerr = \curl_error($ch); |
|
134
|
1 |
|
$this->response->messages[] = 'Curl failed with response: \'' . $cerr . '\'.'; |
|
135
|
1 |
|
} else { |
|
136
|
|
|
$this->response->okay = true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
\curl_close($ch); |
|
140
|
1 |
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: