|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Haridarshan Gorana |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
namespace Haridarshan\Instagram; |
|
26
|
|
|
|
|
27
|
|
|
use GuzzleHttp\Client; |
|
28
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
29
|
|
|
use GuzzleHttp\Psr7\Response; |
|
30
|
|
|
use GuzzleHttp\Psr7\Stream; |
|
31
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramException; |
|
32
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramOAuthException; |
|
33
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramServerException; |
|
34
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* HelperFactory class |
|
38
|
|
|
* |
|
39
|
|
|
* @library instagram-php |
|
40
|
|
|
* |
|
41
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
42
|
|
|
* |
|
43
|
|
|
* @link http://github.com/haridarshan/instagram-php Class Documentation |
|
44
|
|
|
* @link http://instagram.com/developer/ API Documentation |
|
45
|
|
|
* |
|
46
|
|
|
* @author Haridarshan Gorana <[email protected]> |
|
47
|
|
|
* |
|
48
|
|
|
* @since May 09, 2016 |
|
49
|
|
|
* |
|
50
|
|
|
* @copyright Haridarshan Gorana |
|
51
|
|
|
* |
|
52
|
|
|
* @version 2.2.2 |
|
53
|
|
|
*/ |
|
54
|
|
|
class HelperFactory |
|
55
|
|
|
{ |
|
56
|
|
|
/** @var HelperFactory The reference to *HelperFactory* instance of this class */ |
|
57
|
|
|
private static $instance; |
|
58
|
|
|
|
|
59
|
|
|
/** @var Response|ResponseInterface $response */ |
|
60
|
|
|
protected static $response; |
|
61
|
|
|
|
|
62
|
|
|
/** @var Stream $stream */ |
|
63
|
|
|
protected static $stream; |
|
64
|
|
|
|
|
65
|
|
|
/** @var object $content */ |
|
66
|
|
|
protected static $content; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the *HelperFactory* instance of this class. |
|
70
|
|
|
* |
|
71
|
|
|
* @return HelperFactory The *HelperFactory* instance. |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getInstance() |
|
74
|
|
|
{ |
|
75
|
|
|
if (null === self::$instance) { |
|
76
|
|
|
self::$instance = new static(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return self::$instance; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Protected constructor to prevent creating a new instance of the |
|
84
|
|
|
* *HelperFactory* via the `new` operator from outside of this class. |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function __construct() |
|
87
|
|
|
{ |
|
88
|
|
|
// a factory constructor should never be invoked |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Factory Client method to create \GuzzleHttp\Client object |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $uri |
|
95
|
|
|
* |
|
96
|
|
|
* @return Client |
|
97
|
|
|
*/ |
|
98
|
|
|
public function client($uri) |
|
99
|
|
|
{ |
|
100
|
|
|
return new Client([ |
|
101
|
|
|
'base_uri' => $uri, |
|
102
|
|
|
]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Sends request to Instagram Api Endpoints |
|
107
|
|
|
* |
|
108
|
|
|
* @param Client $client |
|
109
|
|
|
* @param string $endpoint |
|
110
|
|
|
* @param array|string $options |
|
111
|
|
|
* @param string $method |
|
112
|
|
|
* |
|
113
|
|
|
* @throws InstagramOAuthException|InstagramException |
|
114
|
|
|
* |
|
115
|
|
|
* @return Response |
|
116
|
|
|
*/ |
|
117
|
|
|
public function request(Client $client, $endpoint, $options, $method = 'GET') |
|
118
|
|
|
{ |
|
119
|
|
|
try { |
|
120
|
|
|
return $client->request($method, $endpoint, [ |
|
121
|
|
|
'form_params' => $options, |
|
122
|
|
|
]); |
|
123
|
|
|
} catch (ClientException $exception) { |
|
124
|
|
|
static::throwException(static::extractOriginalExceptionMessage($exception), $exception); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Create body for Guzzle client request |
|
130
|
|
|
* |
|
131
|
|
|
* @param array|null|string $options |
|
132
|
|
|
* @param string $method GET|POST |
|
133
|
|
|
* |
|
134
|
|
|
* @return string|mixed |
|
135
|
|
|
*/ |
|
136
|
|
|
protected static function createBody($options, $method) |
|
137
|
|
|
{ |
|
138
|
|
|
return ('GET' !== $method) ? is_array($options) ? http_build_query($options) : ltrim($options, '&') : null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/* |
|
142
|
|
|
* Method to extract all exceptions for Guzzle ClientException |
|
143
|
|
|
* |
|
144
|
|
|
* @param ClientException $exception |
|
145
|
|
|
* |
|
146
|
|
|
* @return stdClass |
|
147
|
|
|
* |
|
148
|
|
|
* @throws InstagramServerException |
|
149
|
|
|
*/ |
|
150
|
|
|
protected static function extractOriginalExceptionMessage(ClientException $exception) |
|
151
|
|
|
{ |
|
152
|
|
|
self::$response = $exception->getResponse(); |
|
153
|
|
|
self::$stream = self::$response->getBody(); |
|
154
|
|
|
self::$content = self::$stream->getContents(); |
|
155
|
|
|
if (!self::$content) { |
|
156
|
|
|
throw new InstagramServerException( |
|
157
|
|
|
$exception->getMessage(), |
|
158
|
|
|
$exception->getCode(), |
|
159
|
|
|
$exception |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return json_decode(self::$content); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Throw required Exception |
|
168
|
|
|
* |
|
169
|
|
|
* @param \stdClass $object |
|
170
|
|
|
* @param ClientException $exMessage |
|
171
|
|
|
* |
|
172
|
|
|
* @throws InstagramOAuthException|InstagramException |
|
173
|
|
|
*/ |
|
174
|
|
|
protected static function throwException(\stdClass $object, ClientException $exMessage) |
|
175
|
|
|
{ |
|
176
|
|
|
$exception = static::createExceptionMessage($object); |
|
177
|
|
|
if (stripos($exception['error_type'], 'oauth') !== false) { |
|
178
|
|
|
throw new InstagramOAuthException( |
|
179
|
|
|
json_encode(['Type' => $exception['error_type'], 'Message' => $exception['error_message']]), |
|
180
|
|
|
$exception['error_code'], |
|
181
|
|
|
$exMessage |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
throw new InstagramException( |
|
185
|
|
|
json_encode(['Type' => $exception['error_type'], 'Message' => $exception['error_message']]), |
|
186
|
|
|
$exception['error_code'], |
|
187
|
|
|
$exMessage |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Creates Exception Message |
|
193
|
|
|
* |
|
194
|
|
|
* @param \stdClass $object |
|
195
|
|
|
* |
|
196
|
|
|
* @return array |
|
197
|
|
|
*/ |
|
198
|
|
|
protected static function createExceptionMessage(\stdClass $object) |
|
199
|
|
|
{ |
|
200
|
|
|
$message = []; |
|
201
|
|
|
$message['error_type'] = isset($object->meta) ? $object->meta->error_type : $object->error_type; |
|
202
|
|
|
$message['error_message'] = isset($object->meta) ? $object->meta->error_message : $object->error_message; |
|
203
|
|
|
$message['error_code'] = isset($object->meta) ? $object->meta->code : $object->code; |
|
204
|
|
|
|
|
205
|
|
|
return $message; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Private clone method to prevent cloning of the instance of the |
|
210
|
|
|
* *HelperFactory* instance. |
|
211
|
|
|
*/ |
|
212
|
|
|
private function __clone() |
|
213
|
|
|
{ |
|
214
|
|
|
// a factory clone should never be invoked |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Private unserialize method to prevent unserializing of the *HelperFactory * |
|
219
|
|
|
* instance. |
|
220
|
|
|
*/ |
|
221
|
|
|
private function __wakeup() |
|
222
|
|
|
{ |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|