User   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 73.02%

Importance

Changes 0
Metric Value
wmc 20
dl 0
loc 202
ccs 46
cts 63
cp 0.7302
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A getSelf() 0 16 2
A getMedia() 0 16 2
A searchUser() 0 21 3
A getLiked() 0 16 2
A readUserMedia() 0 21 3
A getInfo() 0 21 3
A getToken() 0 3 1
A setToken() 0 3 1
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 Ch0c01dxyz\InstaToken\Objects\Name;
12
use Ch0c01dxyz\InstaToken\Objects\UserId;
13
use Ch0c01dxyz\InstaToken\Objects\AccessToken;
14
use Ch0c01dxyz\InstaToken\Interfaces\UserInterface;
15
use Ch0c01dxyz\InstaToken\Exceptions\UserException;
16
17
/**
18
 * @author Egar Rizki <[email protected]>
19
 */
20
class User implements UserInterface
21
{
22
	/**
23
	 * @var \Ch0c01dxyz\InstaToken\Objects\AccessToken
24
	 */
25
	protected $accessToken;
26
27
	/**
28
	 * @var \Http\Client\HttpClient
29
	 */
30
	protected $httpClient;
31
32
	/**
33
	 * @var \Http\Message\RequestFactory
34
	 */
35
	protected $requestFactory;
36
37
	/**
38
	 * New instance of User class
39
	 *
40
	 * @param \Http\Client\HttpClient|null $httpClient
41
	 * @param \Http\Message\RequestFactory|null $requestFactory
42
	 */
43 7
	public function __construct ( HttpClient $httpClient = null, RequestFactory $requestFactory = null )
44
	{
45 7
		$this->httpClient = $httpClient ?: HttpClientDiscovery::find ();
46
47 7
		$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find ();
48 7
	}
49
50
	/**
51
	 * Access Token Setter
52
	 *
53
	 * @param string $token
54
	 */
55 6
	public function setToken ( $token )
56
	{
57 6
		$this->accessToken = new AccessToken ( $token );
58 6
	}
59
60
	/**
61
	 * Access Token Getter
62
	 *
63
	 * @return object Access Token
64
	 */
65
	public function getToken () : AccessToken
66
	{
67
		return $this->accessToken;
68
	}
69
70
	/**
71
	 * Get User Information from Access Token
72
	 *
73
	 * @return array
74
	 */
75 1
	public function getSelf () : array
76
	{
77 1
		$uri = sprintf ( "https://api.instagram.com/v1/users/self/?access_token=%s", $this->accessToken );
78
79 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
80
81 1
		$response = $this->httpClient->sendRequest ( $request );
82
83 1
		if ( $response->getStatusCode () === 400 )
84
		{
85
			$body = json_decode ( ( string ) $response->getBody () );
86
87
			throw new UserException ( $body->meta->error_message );
88
		}
89
90 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
91
	}
92
93
	/**
94
	 * Get User Information from UserId
95
	 *
96
	 * @return array
97
	 */
98 1
	public function getInfo ( UserId $userId ) : array
99
	{
100 1
		if ( false === ( $userId instanceof UserId ) )
101
		{
102
			throw new UserException ( "Current param isn't instance of UserId." );
103
		}
104
105 1
		$uri = sprintf ( "https://api.instagram.com/v1/users/%s/?access_token=%s", ( string ) $userId->__toInt(), $this->accessToken );
106
107 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
108
109 1
		$response = $this->httpClient->sendRequest ( $request );
110
111 1
		if ( $response->getStatusCode () === 400 )
112
		{
113
			$body = json_decode ( ( string ) $response->getBody () );
114
115
			throw new UserException ( $body->meta->error_message );
116
		}
117
118 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
119
	}
120
121
	/**
122
	 * Get User Media
123
	 *
124
	 * @return array
125
	 */
126 1
	public function getMedia () : array
127
	{
128 1
		$uri = sprintf ( "https://api.instagram.com/v1/users/self/media/recent?access_token=%s", $this->accessToken );
129
130 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
131
132 1
		$response = $this->httpClient->sendRequest ( $request );
133
134 1
		if ( $response->getStatusCode () === 400 )
135
		{
136
			$body = json_decode ( ( string ) $response->getBody () );
137
138
			throw new UserException ( $body->meta->error_message );
139
		}
140
141 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
142
	}
143
144
	/**
145
	 * Get Media liked by User
146
	 *
147
	 * @return array
148
	 */
149 1
	public function getLiked () : array
150
	{
151 1
		$uri = sprintf ( "https://api.instagram.com/v1/users/self/media/liked?access_token=%s", $this->accessToken );
152
153 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
154
155 1
		$response = $this->httpClient->sendRequest ( $request );
156
157 1
		if ( $response->getStatusCode () === 400 )
158
		{
159
			$body = json_decode ( ( string ) $response->getBody () );
160
161
			throw new UserException ( $body->meta->error_message );
162
		}
163
164 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
165
	}
166
167
	/**
168
	 * Search user by Name in Application
169
	 *
170
	 * @param object Name $name
171
	 * @return array
172
	 */
173 1
	public function searchUser ( Name $name ) : array
174
	{
175 1
		if ( false === ( $name instanceof Name ) )
176
		{
177
			throw new UserException ( "Current param isn't instance of Name Class." );
178
		}
179
180 1
		$uri = sprintf ( "https://api.instagram.com/v1/users/search?q=%s&access_token=%s", $name->__toString (), $this->accessToken );
181
182 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
183
184 1
		$response = $this->httpClient->sendRequest ( $request );
185
186 1
		if ( $response->getStatusCode () === 400 )
187
		{
188
			$body = json_decode ( ( string ) $response->getBody () );
189
190
			throw new UserException ( $body->meta->error_message );
191
		}
192
193 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
194
	}
195
196
	/**
197
	 * Get recent media from given User
198
	 *
199
	 * @return array
200
	 */
201 1
	public function readUserMedia ( UserId $userId ) : array
202
	{
203 1
		if ( false === ( $userId instanceof UserId ) )
204
		{
205
			throw new UserException ( "Current param isn't instance of UserId Class." );
206
		}
207
208 1
		$uri = sprintf ( "https://api.instagram.com/v1/users/%s/media/recent/?access_token=%s", $userId->__toInt (), $this->accessToken );
209
210 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
211
212 1
		$response = $this->httpClient->sendRequest ( $request );
213
214 1
		if ( $response->getStatusCode () === 400 )
215
		{
216
			$body = json_decode ( ( string ) $response->getBody () );
217
218
			throw new UserException ( $body->meta->error_message );
219
		}
220
221 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
222
	}
223
}
224