1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Api\Providers\BoardSections; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Pins; |
7
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Suggestions; |
8
|
|
|
use seregazhuk\PinterestBot\Api\Providers\User; |
9
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Auth; |
10
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Inbox; |
11
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Boards; |
12
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Topics; |
13
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Pinners; |
14
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Keywords; |
15
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Comments; |
16
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Password; |
17
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Interests; |
18
|
|
|
use seregazhuk\PinterestBot\Exceptions\WrongProvider; |
19
|
|
|
use seregazhuk\PinterestBot\Api\Contracts\HttpClient; |
20
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Core\Provider; |
21
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Core\ProviderWrapper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @property-read Pins $pins |
25
|
|
|
* @property-read Inbox $inbox |
26
|
|
|
* @property-read User $user |
27
|
|
|
* @property-read Boards $boards |
28
|
|
|
* @property-read Pinners $pinners |
29
|
|
|
* @property-read Keywords $keywords |
30
|
|
|
* @property-read Interests $interests |
31
|
|
|
* @property-read Topics $topics |
32
|
|
|
* @property-read Auth $auth |
33
|
|
|
* @property-read Comments $comments |
34
|
|
|
* @property-read Password $password |
35
|
|
|
* @property-read Suggestions $suggestions |
36
|
|
|
* @property-read BoardSections $boardSections |
37
|
|
|
* |
38
|
|
|
* Class ProvidersContainer |
39
|
|
|
* @package seregazhuk\PinterestBot\Api |
40
|
|
|
*/ |
41
|
|
|
class ProvidersContainer |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* References to the request that travels |
45
|
|
|
* through the application. |
46
|
|
|
* |
47
|
|
|
* @var Request |
48
|
|
|
*/ |
49
|
|
|
protected $request; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Response |
53
|
|
|
*/ |
54
|
|
|
protected $response; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* A array containing the cached providers. |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $providers = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Request $request |
65
|
|
|
* @param Response $response |
66
|
|
|
*/ |
67
|
|
|
public function __construct(Request $request, Response $response) |
68
|
|
|
{ |
69
|
|
|
$this->request = $request; |
70
|
|
|
$this->response = $response; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Magic method to access different providers from the container. |
75
|
|
|
* |
76
|
|
|
* @param string $provider |
77
|
|
|
* @return Provider |
78
|
|
|
* @throws WrongProvider |
79
|
|
|
*/ |
80
|
|
|
public function __get($provider) |
81
|
|
|
{ |
82
|
|
|
return $this->getProvider($provider); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets provider object by name. If there is no such provider |
88
|
|
|
* in providers array, it will try to create it, then save |
89
|
|
|
* it, and then return. |
90
|
|
|
* |
91
|
|
|
* @param string $provider |
92
|
|
|
* |
93
|
|
|
* @throws WrongProvider |
94
|
|
|
* |
95
|
|
|
* @return Provider |
96
|
|
|
*/ |
97
|
|
|
public function getProvider($provider) |
98
|
|
|
{ |
99
|
|
|
$provider = strtolower($provider); |
100
|
|
|
|
101
|
|
|
// Check if an instance has already been initiated. If not |
102
|
|
|
// build it and then add to the providers array. |
103
|
|
|
if (!isset($this->providers[$provider])) { |
104
|
|
|
$this->addProvider($provider); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->providers[$provider]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Creates provider by class name, and if success saves |
112
|
|
|
* it to providers array. Provider class must exist in PROVIDERS_NAMESPACE. |
113
|
|
|
* |
114
|
|
|
* @param string $provider |
115
|
|
|
* @throws WrongProvider |
116
|
|
|
*/ |
117
|
|
|
protected function addProvider($provider) |
118
|
|
|
{ |
119
|
|
|
$className = $this->resolveProviderClass($provider); |
120
|
|
|
|
121
|
|
|
$this->providers[$provider] = $this->buildProvider($className); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Build Provider object. |
126
|
|
|
* |
127
|
|
|
* @param string $className |
128
|
|
|
* @return ProviderWrapper |
129
|
|
|
*/ |
130
|
|
|
protected function buildProvider($className) |
131
|
|
|
{ |
132
|
|
|
$provider = new $className($this); |
133
|
|
|
|
134
|
|
|
return new ProviderWrapper($provider); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Proxies call to Response object and returns message from |
139
|
|
|
* the error object. |
140
|
|
|
* |
141
|
|
|
* @return string|null |
142
|
|
|
*/ |
143
|
|
|
public function getLastError() |
144
|
|
|
{ |
145
|
|
|
return $this->response->getLastErrorText(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns client context from Pinterest response. By default info returns from the last |
150
|
|
|
* Pinterest response. If there was no response before or the argument $reload is |
151
|
|
|
* true, we make a dummy request to the main page to update client context. |
152
|
|
|
* |
153
|
|
|
* @param bool $reload |
154
|
|
|
* @return array|null |
155
|
|
|
* @throws WrongProvider |
156
|
|
|
*/ |
157
|
|
|
public function getClientInfo($reload = false) |
158
|
|
|
{ |
159
|
|
|
$clientInfo = $this->response->getClientInfo(); |
160
|
|
|
|
161
|
|
|
if ($clientInfo === null || $reload) { |
162
|
|
|
/** @var User $userProvider */ |
163
|
|
|
$userProvider = $this->getProvider('user'); |
164
|
|
|
$userProvider->visitPage(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->response->getClientInfo(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns HttpClient object for setting user-agent string or |
172
|
|
|
* other CURL available options. |
173
|
|
|
* |
174
|
|
|
* @return HttpClient |
175
|
|
|
*/ |
176
|
|
|
public function getHttpClient() |
177
|
|
|
{ |
178
|
|
|
return $this->request->getHttpClient(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $provider |
183
|
|
|
* @return string |
184
|
|
|
* @throws WrongProvider |
185
|
|
|
*/ |
186
|
|
|
protected function resolveProviderClass($provider) |
187
|
|
|
{ |
188
|
|
|
$className = __NAMESPACE__ . '\\Providers\\' . ucfirst($provider); |
189
|
|
|
|
190
|
|
|
if (!$this->checkIsProviderClass($className)) { |
191
|
|
|
throw new WrongProvider("Provider $className not found."); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $className; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $className |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
protected function checkIsProviderClass($className) |
202
|
|
|
{ |
203
|
|
|
if (!class_exists($className)) { |
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return in_array(Provider::class, class_parents($className)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return Request |
212
|
|
|
*/ |
213
|
|
|
public function getRequest() |
214
|
|
|
{ |
215
|
|
|
return $this->request; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return Response |
220
|
|
|
*/ |
221
|
|
|
public function getResponse() |
222
|
|
|
{ |
223
|
|
|
return $this->response; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|