1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\ArtisanCloudflare; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Promise; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
10
|
|
|
|
11
|
|
|
use Illuminate\Support\Collection; |
12
|
|
|
use Illuminate\Contracts\Logging\Log; |
13
|
|
|
|
14
|
|
|
class Client |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Base URI. |
18
|
|
|
*/ |
19
|
|
|
const BASE_URI = 'https://api.cloudflare.com/client/v4/'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \GuzzleHttp\ClientInterface |
23
|
|
|
*/ |
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Illuminate\Contracts\Logging\Log |
28
|
|
|
*/ |
29
|
|
|
protected $logger; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
35
|
|
|
* @param \Illuminate\Contracts\Logging $logger |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ClientInterface $client, Log $logger) |
38
|
|
|
{ |
39
|
|
|
$this->client = $client; |
40
|
|
|
$this->logger = $logger; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Delete all the given zones with their options. |
45
|
|
|
* |
46
|
|
|
* All the requests are asynchronous and sent concurrently. |
47
|
|
|
* |
48
|
|
|
* The promise waits until all the promises have been resolved or rejected |
49
|
|
|
* and returns the results of each request. |
50
|
|
|
* |
51
|
|
|
* @param \Illuminate\Support\Collection $zones |
52
|
|
|
* @return \Illuminate\Support\Collection |
53
|
|
|
*/ |
54
|
|
|
public function purge(Collection $zones) |
55
|
|
|
{ |
56
|
|
|
$promises = $zones->map(function ($options, $identifier) { |
57
|
|
|
return $this->client->deleteAsync("zones/{$identifier}/purge_cache", [ |
58
|
|
|
\GuzzleHttp\RequestOptions::JSON => $options, |
59
|
|
|
]); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
return $this->settle($promises)->wait(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a promise that is fulfilled when all of the provided promises have |
67
|
|
|
* been fulfilled or rejected. |
68
|
|
|
* |
69
|
|
|
* The returned promise is fulfilled with a collection of results. |
70
|
|
|
* |
71
|
|
|
* @param Illuminate\Support\Collection $promises |
72
|
|
|
* @return GuzzleHttp\Promise\PromiseInterface |
73
|
|
|
*/ |
74
|
|
|
protected function settle(Collection $promises) |
75
|
|
|
{ |
76
|
|
|
$results = collect(); |
77
|
|
|
|
78
|
|
|
return Promise\each( |
79
|
|
|
$promises->toArray(), |
80
|
|
|
$this->onFulfilled($results), |
81
|
|
|
$this->onRejected($results) |
82
|
|
|
)->then(function () use (&$results) { |
83
|
|
|
return $results; |
84
|
|
|
}); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Put the body of the fulfilled promise into the results. |
89
|
|
|
* |
90
|
|
|
* @param \Illuminate\Support\Collection $results |
91
|
|
|
* @return Closure |
92
|
|
|
*/ |
93
|
|
|
protected function onFulfilled(Collection &$results) |
94
|
|
|
{ |
95
|
|
|
return function ($value, $identifier) use (&$results) { |
96
|
|
|
return $results->put($identifier, $this->getBody($value)); |
97
|
|
|
}; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Handle the rejected promise and put the errors into the results. |
102
|
|
|
* |
103
|
|
|
* @param \Illuminate\Support\Collection $results |
104
|
|
|
* @return Closure |
105
|
|
|
*/ |
106
|
|
|
protected function onRejected(Collection &$results) |
107
|
|
|
{ |
108
|
|
|
return function ($reason, $identifier) use (&$results) { |
109
|
|
|
if ($reason instanceof ClientException) { |
110
|
|
|
return $results->put($identifier, $this->getBody($reason->getResponse())); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->logger->error($reason); |
114
|
|
|
|
115
|
|
|
return $results->put($identifier, $this->handleException($reason)); |
116
|
|
|
}; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Transform a request exception into a response object. |
121
|
|
|
* |
122
|
|
|
* @param \GuzzleHttp\Exception\RequestException $e |
123
|
|
|
* @return object |
124
|
|
|
*/ |
125
|
|
|
protected function handleException(RequestException $e) |
126
|
|
|
{ |
127
|
|
|
if ($e->hasResponse()) { |
128
|
|
|
$message = (string) $e->getResponse()->getBody(); |
129
|
|
|
} else { |
130
|
|
|
$message = $e->getMessage(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$response = [ |
134
|
|
|
'success' => false, |
135
|
|
|
'errors' => [ |
136
|
|
|
(object) [ |
137
|
|
|
'code' => $e->getCode(), |
138
|
|
|
'message' => $message, |
139
|
|
|
], |
140
|
|
|
], |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
return (object) $response; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the response body. |
148
|
|
|
* |
149
|
|
|
* @param \GuzzleHttp\Psr7\Response $response |
150
|
|
|
* @return object |
151
|
|
|
*/ |
152
|
|
|
protected function getBody(Response $response) |
153
|
|
|
{ |
154
|
|
|
return json_decode($response->getBody(), false); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the Guzzle client. |
159
|
|
|
* |
160
|
|
|
* @return \GuzzleHttp\Client |
161
|
|
|
*/ |
162
|
|
|
public function getClient() |
163
|
|
|
{ |
164
|
|
|
return $this->client; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: