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 Singleton The reference to *Singleton* instance of this class */ |
15
|
|
|
private static $instance; |
16
|
|
|
|
17
|
|
|
/** @var Response $response */ |
18
|
|
|
protected static $response; |
19
|
|
|
|
20
|
|
|
/** @var Stream $stream */ |
21
|
|
|
protected static $stream; |
22
|
|
|
|
23
|
|
|
/** @var object $content */ |
24
|
|
|
protected static $content; |
25
|
|
|
|
26
|
|
|
/* |
27
|
|
|
* Returns the *Singleton* instance of this class. |
28
|
|
|
* |
29
|
|
|
* @return Singleton The *Singleton* instance. |
30
|
|
|
*/ |
31
|
|
|
public static function getInstance() |
32
|
|
|
{ |
33
|
|
|
if (null === static::$instance) { |
|
|
|
|
34
|
|
|
static::$instance = new static(); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return static::$instance; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* Protected constructor to prevent creating a new instance of the |
42
|
|
|
* *Singleton* via the `new` operator from outside of this class. |
43
|
|
|
*/ |
44
|
|
|
protected function __construct() |
45
|
|
|
{ |
46
|
|
|
// a factory constructor should never be invoked |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* |
50
|
|
|
* Factory Client method to create \GuzzleHttp\Client object |
51
|
|
|
* @param string $uri |
52
|
|
|
* @return Client |
53
|
|
|
*/ |
54
|
|
|
public function client($uri) |
55
|
|
|
{ |
56
|
|
|
return new Client([ |
57
|
|
|
'base_uri' => $uri |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* @param Client $client |
63
|
|
|
* @param string $endpoint |
64
|
|
|
* @param array|string $options |
65
|
|
|
* @param string $method |
66
|
|
|
* @return Response |
67
|
|
|
* @throws InstagramOAuthException |
68
|
|
|
* @throws InstagramException |
69
|
|
|
*/ |
70
|
|
|
public function request(Client $client, $endpoint, $options, $method = 'GET') |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
|
|
return $client->request($method, $endpoint, [ |
74
|
|
|
'headers' => ['Accept' => 'application/json'], |
75
|
|
|
'body' => static::createBody($options, $method) |
76
|
|
|
]); |
77
|
|
|
} catch (ClientException $exception) { |
78
|
|
|
static::throwException(static::extractOriginalExceptionMessage($exception), $exception); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/* |
83
|
|
|
* Create body for Guzzle client request |
84
|
|
|
* @param array|null|string $options |
85
|
|
|
* @param string $method GET|POST |
86
|
|
|
* @return string|mixed |
87
|
|
|
*/ |
88
|
|
|
protected static function createBody($options, $method) |
89
|
|
|
{ |
90
|
|
|
return ('GET' !== $method) ? is_array($options) ? http_build_query($options) : ltrim($options, '&') : null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* |
94
|
|
|
* Method to extract all exceptions for Guzzle ClientException |
95
|
|
|
* @param ClientException $exception |
96
|
|
|
* @return |
97
|
|
|
*/ |
98
|
|
|
protected static function extractOriginalExceptionMessage(ClientException $exception) |
99
|
|
|
{ |
100
|
|
|
self::$response = $exception->getResponse(); |
101
|
|
|
self::$stream = self::$response->getBody(); |
102
|
|
|
self::$content = self::$stream->getContents(); |
103
|
|
|
if (empty(self::$content)) { |
104
|
|
|
throw new InstagramServerException( |
105
|
|
|
$exception->getMessage(), |
106
|
|
|
$exception->getCode(), |
107
|
|
|
$exception |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
return json_decode(self::$content); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/* |
114
|
|
|
* @param \stdClass $object |
115
|
|
|
* @param ClientException $exception |
|
|
|
|
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
protected static function throwException(\stdClass $object, ClientException $exMessage) |
119
|
|
|
{ |
120
|
|
|
$exception = static::getExceptionMessage($object); |
121
|
|
|
if (stripos($exception['error_type'], "oauth") !== false) { |
122
|
|
|
throw new InstagramOAuthException( |
123
|
|
|
json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])), |
124
|
|
|
$exception['error_code'], |
125
|
|
|
$exMessage |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
throw new InstagramException( |
129
|
|
|
json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])), |
130
|
|
|
$exception['error_code'], |
131
|
|
|
$exMessage |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/* |
136
|
|
|
* @param \stdClass $object |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
protected static function getExceptionMessage(\stdClass $object) |
140
|
|
|
{ |
141
|
|
|
$message = array(); |
142
|
|
|
$message['error_type'] = isset($object->meta) ? $object->meta->error_type : $object->error_type; |
143
|
|
|
$message['error_message'] = isset($object->meta) ? $object->meta->error_message : $object->error_message; |
144
|
|
|
$message['error_code'] = isset($object->meta) ? $object->meta->code : $object->code; |
145
|
|
|
return $message; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
* Private clone method to prevent cloning of the instance of the |
150
|
|
|
* *Singleton* instance. |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
private function __clone() |
155
|
|
|
{ |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: