Comment::sendComment()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 12
cts 16
cp 0.75
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 2
crap 4.25
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\Objects\CommentId;
15
use Ch0c01dxyz\InstaToken\Interfaces\CommentInterface;
16
use Ch0c01dxyz\InstaToken\Exceptions\CommentException;
17
18
/**
19
 * @author Egar Rizki <[email protected]>
20
 */
21
class Comment implements CommentInterface
22
{
23
	/**
24
	 * @var \Ch0c01dxyz\InstaToken\Objects\AccessToken
25
	 */
26
	protected $accessToken;
27
28
	/**
29
	 * @var \Http\Client\HttpClient
30
	 */
31
	protected $httpClient;
32
33
	/**
34
	 * @var \Http\Message\RequestFactory
35
	 */
36
	protected $requestFactory;
37
38
	/**
39
	 * @var \Http\Message\MultipartStream\MultipartStreamBuilder
40
	 */
41
	protected $builder;
42
43
	/**
44
	 * New instance of Comment class
45
	 *
46
	 * @param \Http\Client\HttpClient|null $httpClient
47
	 * @param \Http\Message\RequestFactory|null $requestFactory
48
	 */
49 4
	public function __construct ( HttpClient $httpClient = null, RequestFactory $requestFactory = null )
50
	{
51 4
		$this->httpClient = $httpClient ?: HttpClientDiscovery::find ();
52
53 4
		$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find ();
54
55 4
		$this->builder = new MultipartStreamBuilder ();
56 4
	}
57
58
	/**
59
	 * Access Token Setter
60
	 *
61
	 * @param string $token
62
	 */
63 3
	public function setToken ( string $token )
64
	{
65 3
		$this->accessToken = new AccessToken ( $token );
66 3
	}
67
68
	/**
69
	 * Access Token Getter
70
	 *
71
	 * @return object AccessToken
72
	 */
73
	public function getToken () : AccessToken
74
	{
75
		return $this->accessToken;
76
	}
77
78
	/**
79
	 * Get List recent comments on defined Media
80
	 *
81
	 * @return array
82
	 */
83 1
	public function listComment ( MediaId $mediaId ) : array
84
	{
85 1
		if ( false === ( $mediaId instanceof MediaId ) )
86
		{
87
			throw new CommentException ( "Current param isn't instance of MediaId." );
88
		}
89
90 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/%s/comments?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 CommentException ( $body->meta->error_message );
101
		}
102
103 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
104
	}
105
106
	/**
107
	 * Send comment to Media
108
	 *
109
	 * @return array
110
	 */
111 1
	public function sendComment ( MediaId $mediaId, string $comment ) : array
112
	{
113 1
		if ( false === ( $mediaId instanceof MediaId ) )
114
		{
115
			throw new CommentException ( "Current param isn't instance of MediaId." );
116
		}
117
118 1
		if ( empty ( $comment ) )
119
		{
120
			throw new CommentException ( "Comment required." );
121
		}
122
123 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/%s/comments", $mediaId->__toString () );
124
125 1
		$this->builder->addResource ( "access_token", $this->accessToken );
126 1
		$this->builder->addResource ( "text", $comment );
127
128 1
		$request = $this->requestFactory->createRequest ( "POST", $uri, [
129 1
			"Content-Type" => 'multipart/form-data; boundary="' . $this->builder->getBoundary () . '"'
130 1
		], ( string ) $this->builder->build () );
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 CommentException ( $body->meta->error_message );
139
		}
140
141 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
142
	}
143
144
	/**
145
	 * Delete comment on Media
146
	 *
147
	 * @return array
148
	 */
149 1
	public function deleteComment ( MediaId $mediaId, CommentId $commentId ) : array
150
	{
151 1
		if ( false === ( $mediaId instanceof MediaId ) )
152
		{
153
			throw new CommentException ( "Current param isn't instance of MediaId." );
154
		}
155
156 1
		if ( false === ( $commentId instanceof CommentId ) )
157
		{
158
			throw new CommentException ( "Current param isn't instance of MediaId." );
159
		}
160
161 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/%s/comments/%s?access_token=%s", $mediaId->__toString (), $commentId->__toInt (), $this->accessToken );
162
163 1
		$request = $this->requestFactory->createRequest ( "DELETE", $uri );
164
165 1
		$response = $this->httpClient->sendRequest ( $request );
166
167 1
		if ( $response->getStatusCode () === 400 )
168
		{
169
			$body = json_decode ( ( string ) $response->getBody () );
170
171
			throw new CommentException ( $body->meta->error_message );
172
		}
173
174 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
175
	}
176
}