1
|
|
|
<?php |
2
|
|
|
namespace Haridarshan\Instagram; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use GuzzleHttp\Psr7\Stream; |
8
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramException; |
9
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramOAuthException; |
10
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramServerException; |
11
|
|
|
|
12
|
|
|
class HelperFactory |
13
|
|
|
{ |
14
|
|
|
/** @var Response $response */ |
15
|
|
|
protected static $response; |
16
|
|
|
|
17
|
|
|
/** @var Stream $stream */ |
18
|
|
|
protected static $stream; |
19
|
|
|
|
20
|
|
|
/** @var object $content */ |
21
|
|
|
protected static $content; |
22
|
|
|
|
23
|
|
|
private function __construct() |
24
|
|
|
{ |
25
|
|
|
// a factory constructor should never be invoked |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/* |
29
|
|
|
* Factory Client method to create \GuzzleHttp\Client object |
30
|
|
|
* @param string $uri |
31
|
|
|
* @return Client |
32
|
|
|
*/ |
33
|
|
|
public static function client($uri) |
34
|
|
|
{ |
35
|
|
|
return new Client([ |
36
|
|
|
'base_uri' => $uri |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* @param Client $client |
42
|
|
|
* @param string $endpoint |
43
|
|
|
* @param array|string $options |
44
|
|
|
* @param string $method |
45
|
|
|
* @return Response |
46
|
|
|
* @throws InstagramOAuthException |
47
|
|
|
* @throws InstagramException |
48
|
|
|
*/ |
49
|
|
|
public static function request(Client $client, $endpoint, $options, $method = 'GET') |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
return $client->request($method, $endpoint, [ |
53
|
|
|
'headers' => ['Accept' => 'application/json'], |
54
|
|
|
'body' => static::createBody($options, $method) |
55
|
|
|
]); |
56
|
|
|
} catch (ClientException $e) { |
57
|
|
|
static::throwException(static::extractOriginalExceptionMessage($e), $e); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* Create body for Guzzle client request |
63
|
|
|
* @param array|null|string $options |
64
|
|
|
* @param string $method GET|POST |
65
|
|
|
* @return string|mixed |
66
|
|
|
*/ |
67
|
|
|
protected static function createBody($options, $method) |
68
|
|
|
{ |
69
|
|
|
return ('GET' !== $method) ? is_array($options) ? http_build_query($options) : ltrim($options, '&') : null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* |
73
|
|
|
* Method to extract all exceptions for Guzzle ClientException |
74
|
|
|
* @param ClientException $e |
75
|
|
|
* @return |
76
|
|
|
*/ |
77
|
|
|
protected static function extractOriginalExceptionMessage(ClientException $e) |
78
|
|
|
{ |
79
|
|
|
self::$response = $e->getResponse(); |
80
|
|
|
self::$stream = self::$response->getBody(); |
81
|
|
|
self::$content = self::$stream->getContents(); |
82
|
|
|
if (empty(self::$content)) { |
83
|
|
|
throw new InstagramServerException( |
84
|
|
|
$e->getMessage(), |
85
|
|
|
$e->getCode(), |
86
|
|
|
$e |
87
|
|
|
); |
88
|
|
|
} else { |
89
|
|
|
return json_decode(self::$content); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* |
94
|
|
|
* @param \stdClass $object |
95
|
|
|
* @param ClientException $e |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
protected static function throwException(\stdClass $object, ClientException $e) |
99
|
|
|
{ |
100
|
|
|
$exception = static::getExceptionMessage($object); |
101
|
|
|
if (stripos($exception['error_type'], "oauth") !== false) { |
102
|
|
|
throw new InstagramOAuthException( |
103
|
|
|
json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])), |
104
|
|
|
$exception['error_code'], |
105
|
|
|
$e |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
throw new InstagramException( |
109
|
|
|
json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])), |
110
|
|
|
$exception['error_code'], |
111
|
|
|
$e |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
/* |
115
|
|
|
* @param \stdClass $object |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
protected static function getExceptionMessage(\stdClass $object) |
119
|
|
|
{ |
120
|
|
|
$message = array(); |
121
|
|
|
if (isset($object->meta)) { |
122
|
|
|
$message['error_type'] = $object->meta->error_type; |
123
|
|
|
$message['error_message'] = $object->meta->error_message; |
124
|
|
|
$message['error_code'] = $object->meta->code; |
125
|
|
|
} else { |
126
|
|
|
$message['error_type'] = $object->error_type; |
127
|
|
|
$message['error_message'] = $object->error_message; |
128
|
|
|
$message['error_code'] = $object->code; |
129
|
|
|
} |
130
|
|
|
return $message; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|