Issues (29)

src/Endpoints/Media.php (2 issues)

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\MediaId;
13
use Ch0c01dxyz\InstaToken\Objects\ShortCode;
14
use Ch0c01dxyz\InstaToken\Objects\Map;
15
use Ch0c01dxyz\InstaToken\Interfaces\MediaInterface;
16
use Ch0c01dxyz\InstaToken\Exceptions\MediaException;
17
18
/**
19
 * @author Egar Rizki <[email protected]>
20
 */
21
class Media implements MediaInterface
22
{
23
	/**
24
	 * @var \Ch0c01dxyz\InstaToken\AccessToken
0 ignored issues
show
The type Ch0c01dxyz\InstaToken\AccessToken was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	 * New instance of Media class
40
	 *
41
	 * @param \Http\Client\HttpClient|null $httpClient
42
	 * @param \Http\Message\RequestFactory|null $requestFactory
43
	 */
44 4
	public function __construct ( HttpClient $httpClient = null, RequestFactory $requestFactory = null )
45
	{
46 4
		$this->httpClient = $httpClient ?: HttpClientDiscovery::find ();
47
48 4
		$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find ();
49 4
	}
50
51
	/**
52
	 * Access Token Setter
53
	 *
54
	 * @param string accessToken
55
	 */
56 3
	public function setToken ( string $token )
57
	{
58 3
		$this->accessToken = new AccessToken ( $token );
0 ignored issues
show
Documentation Bug introduced by
It seems like new Ch0c01dxyz\InstaToke...cts\AccessToken($token) of type Ch0c01dxyz\InstaToken\Objects\AccessToken is incompatible with the declared type Ch0c01dxyz\InstaToken\AccessToken of property $accessToken.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59 3
	}
60
61
	/**
62
	 * Access Token Getter
63
	 *
64
	 * @return object AccessToken
65
	 */
66
	public function getToken () : AccessToken
67
	{
68
		return $this->accessToken;
69
	}
70
71
	/**
72
	 * Get Information from given Media Id
73
	 *
74
	 * @return array
75
	 */
76 1
	public function readMedia ( MediaId $mediaId ) : array
77
	{
78 1
		if ( false === ( $mediaId instanceof MediaId ) )
79
		{
80
			throw new MediaException ( "Current param isn't Instance of MediaId" );
81
		}
82
83 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/%s?access_token=%s", $mediaId, $this->accessToken );
84
85 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
86
87 1
		$response = $this->httpClient->sendRequest ( $request );
88
89 1
		if ( $response->getStatusCode () === 400 )
90
		{
91
			$body = json_decode ( ( string ) $response->getBody () );
92
93
			throw new MediaException ( $body->meta->error_message );
94
		}
95
96 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
97
	}
98
99
	/**
100
	 * Get Information from given Shortcoded Media
101
	 *
102
	 * @return array
103
	 */
104 1
	public function infoMedia ( ShortCode $shortCode ) : array
105
	{
106 1
		if ( false === ( $shortCode instanceof ShortCode ) )
107
		{
108
			throw new MediaException ( "Current param isn't Instance Of ShortCode." );
109
		}
110
111 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/shortcode/%s?access_token=%s", $shortCode, $this->accessToken );
112
113 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
114
115 1
		$response = $this->httpClient->sendRequest ( $request );
116
117 1
		if ( $response->getStatusCode () === 400 )
118
		{
119
			$body = json_decode ( ( string ) $response->getBody () );
120
121
			throw new MediaException ( $body->meta->error_message );
122
		}
123
124 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
125
	}
126
127
	/**
128
	 * Search recent Media from given Area
129
	 *
130
	 * @return array
131
	 */
132 1
	public function searchMedia ( Map $map ) : array
133
	{
134 1
		if ( false === ( $map instanceof Map ) )
135
		{
136
			throw new MediaException ( "Current param isn't Instance of Map." );
137
		}
138
139 1
		$uri = sprintf ( "https://api.instagram.com/v1/media/search?lat=%s&lng=%s&distance=%s&access_token=%s", $map->getLat (), $map->getLng (), $map->getDistance (), $this->accessToken );
140
141 1
		$request = $this->requestFactory->createRequest ( "GET", $uri );
142
143 1
		$response = $this->httpClient->sendRequest ( $request );
144
145 1
		if ( $response->getStatusCode () === 400 )
146
		{
147
			$body = json_decode ( ( string ) $response->getBody () );
148
149
			throw new MediaException ( $body->meta->error_message );
150
		}
151
152 1
		return json_decode ( ( string ) $response->getBody ()->getContents (), true );
153
	}
154
}