Location::infoLocation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 7
cts 10
cp 0.7
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3.243
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\AccessToken;
12
use Ch0c01dxyz\InstaToken\Objects\LocationId;
13
use Ch0c01dxyz\InstaToken\Objects\Map;
14
use Ch0c01dxyz\InstaToken\Interfaces\LocationInterface;
15
use Ch0c01dxyz\InstaToken\Exceptions\LocationException;
16
17
/**
18
 * @author Egar Rizki <[email protected]>
19
 */
20
class Location implements LocationInterface
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 Location class
39
	 *
40
	 * @param \Http\Client\HttpClient|null $httpClient
41
	 * @param \Http\Message\RequestFactory|null $requestFactory
42
	 */
43 4
	public function __construct ( HttpClient $httpClient = null, RequestFactory $requestFactory = null )
44
	{
45 4
		$this->httpClient = $httpClient ?: HttpClientDiscovery::find ();
46
47 4
		$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find ();
48 4
	}
49
50
	/**
51
	 * Access Token Setter
52
	 *
53
	 * @param string $token
54
	 */
55 3
	public function setToken ( string $token )
56
	{
57 3
		$this->accessToken = new AccessToken ( $token );
58 3
	}
59
60
	/**
61
	 * Access Token Getter
62
	 *
63
	 * @return object AccessToken
64
	 */
65
	public function getToken () : AccessToken
66
	{
67
		return $this->accessToken;
68
	}
69
70
	/**
71
	 * Get Information about Location
72
	 *
73
	 * @return array
74
	 */
75 1
	public function infoLocation ( LocationId $locationId ) : array
76
	{
77 1
		if ( false === ( $locationId instanceof LocationId ) )
78
		{
79
			throw new LocationException ( "Current param isn't instance of LocationId." );
80
		}
81
82 1
		$uri = sprintf ( "https://api.instagram.com/v1/locations/%s?access_token=%s", $locationId, $this->accessToken );
83
84 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
85
86 1
		$response = $this->httpClient->sendRequest ( $request );
87
88 1
		if ( $response->getStatusCode () === 400 )
89
		{
90
			$body = json_decode ( ( string ) $response->getBody () );
91
92
			throw new LocationException ( $body->meta->error_message );
93
		}
94
95 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
96
	}
97
98
	/**
99
	 * Get list of Media object from given Location
100
	 *
101
	 * @return array
102
	 */
103 1
	public function listMediaLocation ( LocationId $locationId ) : array
104
	{
105 1
		if ( false === ( $locationId instanceof LocationId ) )
106
		{
107
			throw new LocationException ( "Current param isn't instce of LocationId." );
108
		}
109
110 1
		$uri = sprintf ( "https://api.instagram.com/v1/locations/%s/media/recent?access_token=%s", $locationId, $this->accessToken );
111
112 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
113
114 1
		$response = $this->httpClient->sendRequest ( $request );
115
116 1
		if ( $response->getStatusCode () === 400 )
117
		{
118
			$body = json_decode ( ( string ) $response->getBody () );
119
120
			throw new LocationException ( $body->meta->error_message );
121
		}
122
123 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
124
	}
125
126
	/**
127
	 * Search location by given Geographic coordinate
128
	 *
129
	 * @return array
130
	 */
131 1
	public function searchLocation ( Map $map ) : array
132
	{
133 1
		if ( false === ( $map instanceof Map ) )
134
		{
135
			throw new LocationException ( "Current param isn't instance of Map." );
136
		}
137
138 1
		$uri = sprintf ( "https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&distance=%s&access_token=%s", $map->getLat (), $map->getLng (), $map->getDistance (), $this->accessToken );
139
140 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
141
142 1
		$response = $this->httpClient->sendRequest ( $request );
143
144 1
		if ( $response->getStatusCode () === 400 )
145
		{
146
			$body = json_decode ( ( string ) $response->getBody () );
147
148
			throw new LocationException ( $body->meta->error_message );
149
		}
150
151 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
152
	}
153
}