1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
declare ( strict_types = 1 );
|
4
|
|
|
|
5
|
|
|
namespace Ch0c01dxyz\InstaToken\Endpoints;
|
6
|
|
|
|
7
|
|
|
use Http\Client\HttpClient;
|
8
|
|
|
use Http\Discovery\HttpClientDiscovery;
|
9
|
|
|
use Http\Discovery\MessageFactoryDiscovery;
|
10
|
|
|
use Http\Message\RequestFactory;
|
11
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder;
|
12
|
|
|
use Ch0c01dxyz\InstaToken\Objects\AccessToken;
|
13
|
|
|
use Ch0c01dxyz\InstaToken\Objects\UserId;
|
14
|
|
|
use Ch0c01dxyz\InstaToken\Objects\Action;
|
15
|
|
|
use Ch0c01dxyz\InstaToken\Interfaces\RelationInterface;
|
16
|
|
|
use Ch0c01dxyz\InstaToken\Exceptions\RelationException;
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* @author Egar Rizki <[email protected]>
|
20
|
|
|
*/
|
21
|
|
|
class Relation implements RelationInterface
|
22
|
|
|
{
|
23
|
|
|
/**
|
24
|
|
|
* @var \Ch0c01dxyz\InstaToken\Objects\AccessToken
|
25
|
|
|
*/
|
26
|
|
|
protected $accessToken;
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @var \Ch0c01dxyz\InstaToken\Objects\UserId
|
30
|
|
|
*/
|
31
|
|
|
protected $userId;
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* @var \Ch0c01dxyz\InstaToken\Objects\Username
|
35
|
|
|
*/
|
36
|
|
|
protected $username;
|
37
|
|
|
|
38
|
|
|
/**
|
39
|
|
|
* @var \Http\Client\HttpClient
|
40
|
|
|
*/
|
41
|
|
|
protected $httpClient;
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* @var \Http\Message\RequestFactory
|
45
|
|
|
*/
|
46
|
|
|
protected $requestFactory;
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* @var \Ch0c01dxyz\InstaToken\Objects\Action
|
50
|
|
|
*/
|
51
|
|
|
protected $action;
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* @var \Http\Message\MultipartStream\MultipartStreamBuilder
|
55
|
|
|
*/
|
56
|
|
|
protected $builder;
|
57
|
|
|
|
58
|
|
|
/**
|
59
|
|
|
* New instance of Relation class
|
60
|
|
|
*
|
61
|
|
|
* @param \Http\Client\HttpClient|null $httpClient
|
62
|
|
|
* @param \Http\Message\RequestFactory|null $requestFactory
|
63
|
|
|
*/
|
64
|
6 |
|
public function __construct ( HttpClient $httpClient = null, RequestFactory $requestFactory = null )
|
65
|
|
|
{
|
66
|
6 |
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find ();
|
67
|
|
|
|
68
|
6 |
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find ();
|
69
|
|
|
|
70
|
6 |
|
$this->builder = new MultipartStreamBuilder ();
|
71
|
6 |
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* Access Token Setter
|
75
|
|
|
*
|
76
|
|
|
* @param string $token
|
77
|
|
|
*/
|
78
|
5 |
|
public function setToken ( $token )
|
79
|
|
|
{
|
80
|
5 |
|
$this->accessToken = new AccessToken ( $token );
|
81
|
5 |
|
}
|
82
|
|
|
|
83
|
|
|
/**
|
84
|
|
|
* Access Token Getter
|
85
|
|
|
*
|
86
|
|
|
* @return object AccessToken
|
87
|
|
|
*/
|
88
|
|
|
public function getToken () : AccessToken
|
89
|
|
|
{
|
90
|
|
|
return $this->accessToken;
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* Get User Following
|
95
|
|
|
*
|
96
|
|
|
* @return array
|
97
|
|
|
*/
|
98
|
1 |
|
public function getFollow () : array
|
99
|
|
|
{
|
100
|
1 |
|
$uri = sprintf ( "https://api.instagram.com/v1/users/self/follows?access_token=%s", $this->accessToken );
|
101
|
|
|
|
102
|
1 |
|
$request = $this->requestFactory->createRequest ( "GET", $uri );
|
103
|
|
|
|
104
|
1 |
|
$response = $this->httpClient->sendRequest ( $request );
|
105
|
|
|
|
106
|
1 |
|
if ( $response->getStatusCode () === 400 )
|
107
|
|
|
{
|
108
|
|
|
$body = json_decode ( ( string ) $response->getBody () );
|
109
|
|
|
|
110
|
|
|
throw new RelationException ( $body->meta->error_message );
|
111
|
|
|
}
|
112
|
|
|
|
113
|
1 |
|
return json_decode ( ( string ) $response->getBody ()->getContents (), true );
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
/**
|
117
|
|
|
* Get User Follower
|
118
|
|
|
*
|
119
|
|
|
* @return array
|
120
|
|
|
*/
|
121
|
1 |
|
public function getFollowedBy () : array
|
122
|
|
|
{
|
123
|
1 |
|
$uri = sprintf ( "https://api.instagram.com/v1/users/self/followed-by?access_token=%s", $this->accessToken );
|
124
|
|
|
|
125
|
1 |
|
$request = $this->requestFactory->createRequest ( "GET", $uri );
|
126
|
|
|
|
127
|
1 |
|
$response = $this->httpClient->sendRequest ( $request );
|
128
|
|
|
|
129
|
1 |
|
if ( $response->getStatusCode () === 400 )
|
130
|
|
|
{
|
131
|
|
|
$body = json_decode ( ( string ) $response->getBody () );
|
132
|
|
|
|
133
|
|
|
throw new RelationException ( $body->meta->error_message );
|
134
|
|
|
}
|
135
|
|
|
|
136
|
1 |
|
return json_decode ( ( string ) $response->getBody ()->getContents (), true );
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
/**
|
140
|
|
|
* Get User Request Follow List
|
141
|
|
|
*
|
142
|
|
|
* @return array
|
143
|
|
|
*/
|
144
|
1 |
|
public function getRequestedBy () : array
|
145
|
|
|
{
|
146
|
1 |
|
$uri = sprintf ( "https://api.instagram.com/v1/users/self/requested-by?access_token=%s", $this->accessToken );
|
147
|
|
|
|
148
|
1 |
|
$request = $this->requestFactory->createRequest ( "GET", $uri );
|
149
|
|
|
|
150
|
1 |
|
$response = $this->httpClient->sendRequest ( $request );
|
151
|
|
|
|
152
|
1 |
|
if ( $response->getStatusCode () === 400 )
|
153
|
|
|
{
|
154
|
|
|
$body = json_decode ( ( string ) $response->getBody () );
|
155
|
|
|
|
156
|
|
|
throw new RelationException ( $body->meta->error_message );
|
157
|
|
|
}
|
158
|
|
|
|
159
|
1 |
|
return json_decode ( ( string ) $response->getBody ()->getContents (), true );
|
160
|
|
|
}
|
161
|
|
|
|
162
|
|
|
/**
|
163
|
|
|
* Get User Relationship
|
164
|
|
|
*
|
165
|
|
|
* @return array
|
166
|
|
|
*/
|
167
|
1 |
|
public function getRelation ( UserId $userId ) : array
|
168
|
|
|
{
|
169
|
1 |
|
if ( false === ( $userId instanceof UserId ) )
|
170
|
|
|
{
|
171
|
|
|
throw new RelationException ( "Current param isn't instance of UserId." );
|
172
|
|
|
}
|
173
|
|
|
|
174
|
1 |
|
$uri = sprintf ( "https://api.instagram.com/v1/users/%s/relationship?access_token=%s", $userId->__toInt (), $this->accessToken );
|
175
|
|
|
|
176
|
1 |
|
$request = $this->requestFactory->createRequest ( "GET", $uri );
|
177
|
|
|
|
178
|
1 |
|
$response = $this->httpClient->sendRequest ( $request );
|
179
|
|
|
|
180
|
1 |
|
if ( $response->getStatusCode () === 400 )
|
181
|
|
|
{
|
182
|
|
|
$body = json_decode ( ( string ) $response->getBody () );
|
183
|
|
|
|
184
|
|
|
throw new RelationException ( $body->meta->error_message );
|
185
|
|
|
}
|
186
|
|
|
|
187
|
1 |
|
return json_decode ( ( string ) $response->getBody ()->getContents (), true );
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
/**
|
191
|
|
|
* Change user Relationship
|
192
|
|
|
*
|
193
|
|
|
* @param object UserId $userId
|
194
|
|
|
* @param object Action $action
|
195
|
|
|
* @return array
|
196
|
|
|
*/
|
197
|
1 |
|
public function changeRelation ( UserId $userId, Action $action ) : array
|
198
|
|
|
{
|
199
|
1 |
|
if ( false === ( $action instanceof Action ) )
|
200
|
|
|
{
|
201
|
|
|
throw new RelationException ( "Current param isn't instance of Action." );
|
202
|
|
|
}
|
203
|
|
|
|
204
|
1 |
|
if ( false === ( $userId instanceof UserId ) )
|
205
|
|
|
{
|
206
|
|
|
throw new RelationException ( "Current param isn't instance of UserId." );
|
207
|
|
|
}
|
208
|
|
|
|
209
|
1 |
|
$uri = sprintf ( "https://api.instagram.com/v1/users/%s/relationship?access_token=%s", $userId->__toInt (), $this->accessToken );
|
210
|
|
|
|
211
|
1 |
|
$this->builder->addResource ( "action", $action );
|
212
|
|
|
|
213
|
1 |
|
$request = $this->requestFactory->createRequest ( "POST", $uri, [
|
214
|
1 |
|
"Content-Type" => 'multipart/form-data; boundary="' . $this->builder->getBoundary () . '"'
|
215
|
1 |
|
], ( string ) $this->builder->build () );
|
216
|
|
|
|
217
|
1 |
|
$response = $this->httpClient->sendRequest ( $request );
|
218
|
|
|
|
219
|
1 |
|
if ( $response->getStatusCode () === 400 )
|
220
|
|
|
{
|
221
|
|
|
$body = json_decode ( ( string ) $response->getBody () );
|
222
|
|
|
|
223
|
|
|
throw new RelationException ( $body->meta->error_message );
|
224
|
|
|
}
|
225
|
|
|
|
226
|
1 |
|
return json_decode ( ( string ) $response->getBody ()->getContents (), true );
|
227
|
|
|
}
|
228
|
|
|
} |