1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ThinkOne\LaravelSproutsocialsApi; |
5
|
|
|
|
6
|
|
|
use Illuminate\Http\Client\PendingRequest; |
7
|
|
|
use Illuminate\Http\Client\Response; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Facades\Http; |
10
|
|
|
use ThinkOne\LaravelSproutsocialsApi\RequestGroups\AbstractRequestGroup; |
11
|
|
|
|
12
|
|
|
class SproutsocialsApi |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var PendingRequest |
16
|
|
|
*/ |
17
|
|
|
protected PendingRequest $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string|mixed |
21
|
|
|
*/ |
22
|
|
|
protected string $urlPrefix = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int|null |
26
|
|
|
*/ |
27
|
|
|
protected ?int $defaultCustomerId = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected array $defaultPostMetrics = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AbstractRequestGroup[] |
36
|
|
|
*/ |
37
|
|
|
protected array $cachedGroups = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SproutsocialsApi constructor. |
41
|
|
|
* |
42
|
|
|
* @param array $configs |
43
|
|
|
*/ |
44
|
10 |
|
public function __construct(array $configs) |
45
|
|
|
{ |
46
|
10 |
|
$this->urlPrefix = $configs['api_version'] ?? ''; |
47
|
|
|
|
48
|
10 |
|
if ($customerId = (int)$configs['customer_id']) { |
49
|
9 |
|
$this->defaultCustomerId = $customerId; |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
$this->client = Http::baseUrl(Arr::get($configs, 'api_url')) |
53
|
10 |
|
->withToken(Arr::get($configs, 'api_token')) |
54
|
10 |
|
->withOptions(Arr::get($configs, 'request_options', [])); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return current instance |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
1 |
|
public function instance(): self |
62
|
|
|
{ |
63
|
1 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return current client |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
1 |
|
public function client(): PendingRequest |
71
|
|
|
{ |
72
|
1 |
|
return $this->client; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get endpoint group by classname |
77
|
|
|
* @param string $class |
78
|
|
|
* @return AbstractRequestGroup |
79
|
|
|
*/ |
80
|
5 |
|
public function endpointGroup(string $class): AbstractRequestGroup |
81
|
|
|
{ |
82
|
5 |
|
return ($this->cachedGroups[$class]) ?? ($this->cachedGroups[$class] = new $class($this)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* GET request |
87
|
|
|
* @param string $url |
88
|
|
|
* @param array $query |
89
|
|
|
* |
90
|
|
|
* @return Response |
91
|
|
|
* @throws SproutsocialsApiException |
92
|
|
|
*/ |
93
|
3 |
|
public function get(string $url, array $query = []): Response |
94
|
|
|
{ |
95
|
3 |
|
return $this->call(__FUNCTION__, $url, $query); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* HEAD request |
101
|
|
|
* @param string $url |
102
|
|
|
* @param array $query |
103
|
|
|
* |
104
|
|
|
* @return Response |
105
|
|
|
* @throws SproutsocialsApiException |
106
|
|
|
*/ |
107
|
1 |
|
public function head(string $url, array $query = []): Response |
108
|
|
|
{ |
109
|
1 |
|
return $this->call(__FUNCTION__, $url, $query); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* POST request |
114
|
|
|
* @param string $url |
115
|
|
|
* @param array $data |
116
|
|
|
* |
117
|
|
|
* @return Response |
118
|
|
|
* @throws SproutsocialsApiException |
119
|
|
|
*/ |
120
|
2 |
|
public function post(string $url, array $data = []): Response |
121
|
|
|
{ |
122
|
2 |
|
return $this->call(__FUNCTION__, $url, $data); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* PATCH request |
127
|
|
|
* @param string $url |
128
|
|
|
* @param array $data |
129
|
|
|
* |
130
|
|
|
* @return Response |
131
|
|
|
* @throws SproutsocialsApiException |
132
|
|
|
*/ |
133
|
1 |
|
public function patch(string $url, array $data = []): Response |
134
|
|
|
{ |
135
|
1 |
|
return $this->call(__FUNCTION__, $url, $data); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* PUT request |
140
|
|
|
* @param string $url |
141
|
|
|
* @param array $data |
142
|
|
|
* |
143
|
|
|
* @return Response |
144
|
|
|
* @throws SproutsocialsApiException |
145
|
|
|
*/ |
146
|
1 |
|
public function put(string $url, array $data = []): Response |
147
|
|
|
{ |
148
|
1 |
|
return $this->call(__FUNCTION__, $url, $data); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* DELETE request |
153
|
|
|
* @param string $url |
154
|
|
|
* @param array $data |
155
|
|
|
* |
156
|
|
|
* @return Response |
157
|
|
|
* @throws SproutsocialsApiException |
158
|
|
|
*/ |
159
|
1 |
|
public function delete(string $url, array $data = []): Response |
160
|
|
|
{ |
161
|
1 |
|
return $this->call(__FUNCTION__, $url, $data); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Call api endpoint |
166
|
|
|
* @param string $method |
167
|
|
|
* @param string $url |
168
|
|
|
* @param array $data |
169
|
|
|
* @return Response |
170
|
|
|
* @throws SproutsocialsApiException |
171
|
|
|
*/ |
172
|
4 |
|
protected function call(string $method, string $url, array $data = []): Response |
173
|
|
|
{ |
174
|
4 |
|
$callUrl = trim($url, '/'); |
175
|
4 |
|
if ($this->urlPrefix) { |
176
|
4 |
|
$callUrl = trim($this->urlPrefix, '/') . '/' . $callUrl; |
177
|
|
|
} |
178
|
|
|
|
179
|
4 |
|
$method = strtolower($method); |
180
|
4 |
|
$response = $this->client->{$method}($callUrl, $data); |
181
|
|
|
|
182
|
4 |
|
if ($response->ok()) { |
183
|
3 |
|
return $response; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
throw new SproutsocialsApiException($response); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get default customer ID from configuration |
191
|
|
|
* @return int |
192
|
|
|
* @throws NotSetCustomerException |
193
|
|
|
*/ |
194
|
5 |
|
public function defaultCustomerId(): int |
195
|
|
|
{ |
196
|
5 |
|
if (!$this->defaultCustomerId) { |
|
|
|
|
197
|
1 |
|
throw new NotSetCustomerException('Default Customer Id not set'); |
198
|
|
|
} |
199
|
|
|
|
200
|
4 |
|
return (int)$this->defaultCustomerId; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get default post metrics |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
1 |
|
public function defaultPostMetrics(): array |
208
|
|
|
{ |
209
|
1 |
|
if (empty($this->defaultPostMetrics)) { |
210
|
1 |
|
$this->defaultPostMetrics = array_values(array_unique(array_merge( |
211
|
1 |
|
config('sproutsocials-api.metrics.post.instagram'), |
212
|
1 |
|
config('sproutsocials-api.metrics.post.linkedin'), |
213
|
1 |
|
config('sproutsocials-api.metrics.post.facebook'), |
214
|
1 |
|
config('sproutsocials-api.metrics.post.twitter'), |
215
|
1 |
|
))); |
216
|
|
|
} |
217
|
|
|
|
218
|
1 |
|
return $this->defaultPostMetrics; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: