Like::deleteLike()   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 Http\Message\MultipartStream\MultipartStreamBuilder;
12
use Ch0c01dxyz\InstaToken\Objects\AccessToken;
13
use Ch0c01dxyz\InstaToken\Objects\MediaId;
14
use Ch0c01dxyz\InstaToken\Interfaces\LikeInterface;
15
use Ch0c01dxyz\InstaToken\Exceptions\LikeException;
16
17
/**
18
 * @author Egar Rizki <[email protected]>
19
 */
20
class Like implements LikeInterface
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
	 * @var \Http\Message\MultipartStream\MultipartStreamBuilder
39
	 */
40
	protected $builder;
41
42
	/**
43
	 * New instance of Like class
44
	 *
45
	 * @param \Http\Client\HttpClient|null $httpClient
46
	 * @param \Http\Message\RequestFactory|null $requestFactory
47
	 */
48 4
	public function __construct ( HttpClient $httpClient = null, RequestFactory $requestFactory = null )
49
	{
50 4
		$this->httpClient = $httpClient ?: HttpClientDiscovery::find ();
51
52 4
		$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find ();
53
54 4
		$this->builder = new MultipartStreamBuilder ();
55 4
	}
56
57
	/**
58
	 * Access Token Setter
59
	 *
60
	 * @param string $token
61
	 */
62 3
	public function setToken ( string $token )
63
	{
64 3
		$this->accessToken = new AccessToken ( $token );
65 3
	}
66
67
	/**
68
	 * Access Token Getter
69
	 *
70
	 * @return object AccessToken
71
	 */
72
	public function getToken () : AccessToken
73
	{
74
		return $this->accessToken;
75
	}
76
77
	/**
78
	 * Get list User who have likes defined Media
79
	 *
80
	 * @param object MediaId $mediaId
81
	 * @return array
82
	 */
83 1
	public function listLike ( MediaId $mediaId ) : array
84
	{
85 1
		if ( false === ( $mediaId instanceof MediaId ) )
86
		{
87
			throw new LikeException ( "Current param isn't instance of MediaId." );
88
		}
89
90 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/%s/likes?access_token=%s", $mediaId, $this->accessToken );
91
92 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
93
94 1
		$response = $this->httpClient->sendRequest ( $request );
95
96 1
		if ( $response->getStatusCode () === 400 )
97
		{
98
			$body = json_decode ( ( string ) $response->getBody () );
99
100
			throw new LikeException ( $body->meta->error_message );
101
		}
102
103 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
104
	}
105
106
	/**
107
	 * Send like to defined Media
108
	 *
109
	 * @param object MediaId $mediaId
110
	 * @return array
111
	 */
112 1
	public function sendLike ( MediaId $mediaId ) : array
113
	{
114 1
		if ( false === ( $mediaId instanceof MediaId ) )
115
		{
116
			throw new LikeException ( "Current param isn't instance of MediaId." );
117
		}
118
119 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/%s/likes", $mediaId );
120
121 1
		$this->builder->addResource ( "access_token", $this->accessToken );
122
123 1
		$request = $this->requestFactory->createRequest ( "POST", $uri, [
124 1
			"Content-Type" => 'multipart/form-data; boundary="' . $this->builder->getBoundary () . '"'
125 1
		], ( string ) $this->builder->build () );
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 LikeException ( $body->meta->error_message );
134
		}
135
136 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
137
	}
138
139
	/**
140
	 * Remove like from defined Media
141
	 *
142
	 * @param object MediaId $mediaId
143
	 * @return array
144
	 */
145 1
	public function deleteLike ( MediaId $mediaId ) : array
146
	{
147 1
		if ( false === ( $mediaId instanceof MediaId ) )
148
		{
149
			throw new LikeException ( "Current param isn't instance of MediaId." );
150
		}
151
152 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/%s/likes?access_token=%s", $mediaId->__toString (), $this->accessToken );
153
154 1
		$request = $this->requestFactory->createRequest ( "DELETE", $uri );
155
156 1
		$response = $this->httpClient->sendRequest ( $request );
157
158 1
		if ( $response->getStatusCode () === 400 )
159
		{
160
			$body = json_decode ( ( string ) $response->getBody () );
161
162
			throw new LikeException ( $body->meta->error_message );
163
		}
164
165 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
166
	}
167
}