ApiCall::getConsumer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * ApiCall.php
4
 *
5
 * @copyright	More in license.md
6
 * @license		http://www.ipublikuj.eu
7
 * @author		Adam Kadlec http://www.ipublikuj.eu
8
 * @package		iPublikuj:Flickr!
9
 * @subpackage	common
10
 * @since		5.0
11
 *
12
 * @date		06.03.15
13
 */
14
15
namespace IPub\Flickr;
16
17
use Nette;
18
use Nette\Utils;
19
20
use IPub;
21
use IPub\Flickr;
22
use IPub\Flickr\Api;
23
24
use IPub\OAuth;
25
26
/**
27
 * Abstract API calls definition
28
 *
29
 * @package		iPublikuj:Flickr!
30
 * @subpackage	common
31
 *
32
 * @author Adam Kadlec <[email protected]>
33
 */
34
abstract class ApiCall extends Nette\Object
35
{
36
	/**
37
	 * @var OAuth\Consumer
38
	 */
39
	protected $consumer;
40
41
	/**
42
	 * @var OAuth\HttpClient
43
	 */
44
	protected $httpClient;
45
46
	/**
47
	 * @var Configuration
48
	 */
49
	protected $config;
50
51
	/**
52
	 * @param OAuth\Consumer $consumer
53
	 * @param OAuth\HttpClient $httpClient
54
	 * @param Configuration $config
55
	 */
56
	public function __construct(
57
		OAuth\Consumer $consumer,
58
		OAuth\HttpClient $httpClient,
59
		Configuration $config
60
	){
61
		$this->consumer = $consumer;
62
		$this->httpClient = $httpClient;
63
		$this->config = $config;
64
	}
65
66
	/**
67
	 * @internal
68
	 *
69
	 * @return OAuth\HttpClient
70
	 */
71
	public function getHttpClient()
72
	{
73
		return $this->httpClient;
74
	}
75
76
	/**
77
	 * @return Configuration
78
	 */
79
	public function getConfig()
80
	{
81
		return $this->config;
82
	}
83
84
	/**
85
	 * @return OAuth\Consumer
86
	 */
87
	public function getConsumer()
88
	{
89
		return $this->consumer;
90
	}
91
92
	/**
93
	 * Determines the access token that should be used for API calls.
94
	 * The first time this is called, $this->accessToken is set equal
95
	 * to either a valid user access token, or it's set to the application
96
	 * access token if a valid user access token wasn't available.  Subsequent
97
	 * calls return whatever the first call returned.
98
	 *
99
	 * @return OAuth\Token The access token
100
	 */
101
	abstract public function getAccessToken();
102
103
	/**
104
	 * @param string $path
105
	 * @param array $params
106
	 * @param array $headers
107
	 *
108
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
109
	 *
110
	 * @throws OAuth\Exceptions\ApiException
111
	 */
112
	public function get($path, array $params = [], array $headers = [])
113
	{
114
		return $this->api($path, Api\Request::GET, $params, [], $headers);
115
	}
116
117
	/**
118
	 * @param string $path
119
	 * @param array $params
120
	 * @param array $headers
121
	 *
122
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
123
	 *
124
	 * @throws OAuth\Exceptions\ApiException
125
	 */
126
	public function head($path, array $params = [], array $headers = [])
127
	{
128
		return $this->api($path, Api\Request::HEAD, $params, [], $headers);
129
	}
130
131
	/**
132
	 * @param string $path
133
	 * @param array $params
134
	 * @param array $post
135
	 * @param array $headers
136
	 *
137
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
138
	 *
139
	 * @throws OAuth\Exceptions\ApiException
140
	 */
141
	public function post($path, array $params = [], array $post = [], array $headers = [])
142
	{
143
		return $this->api($path, Api\Request::POST, $params, $post, $headers);
144
	}
145
146
	/**
147
	 * @param string $path
148
	 * @param array $params
149
	 * @param array $post
150
	 * @param array $headers
151
	 *
152
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
153
	 *
154
	 * @throws OAuth\Exceptions\ApiException
155
	 */
156
	public function patch($path, array $params = [], array $post = [], array $headers = [])
157
	{
158
		return $this->api($path, Api\Request::PATCH, $params, $post, $headers);
159
	}
160
161
	/**
162
	 * @param string $path
163
	 * @param array $params
164
	 * @param array $post
165
	 * @param array $headers
166
	 *
167
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
168
	 *
169
	 * @throws OAuth\Exceptions\ApiException
170
	 */
171
	public function put($path, array $params = [], array $post = [], array $headers = [])
172
	{
173
		return $this->api($path, Api\Request::PUT, $params, $post, $headers);
174
	}
175
176
	/**
177
	 * @param string $path
178
	 * @param array $params
179
	 * @param array $headers
180
	 *
181
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
182
	 *
183
	 * @throws OAuth\Exceptions\ApiException
184
	 */
185
	public function delete($path, array $params = [], array $headers = [])
186
	{
187
		return $this->api($path, Api\Request::DELETE, $params, [], $headers);
188
	}
189
190
	/**
191
	 * Simply pass anything starting with a slash and it will call the Api, for example
192
	 * <code>
193
	 * $details = $flickr->api('flick.people.info');
194
	 * </code>
195
	 *
196
	 * @param string $path
197
	 * @param string $method The argument is optional
198
	 * @param array $params Query parameters
199
	 * @param array $post Post request parameters or body to send
200
	 * @param array $headers Http request headers
201
	 *
202
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
203
	 *
204
	 * @throws OAuth\Exceptions\ApiException
205
	 */
206
	public function api($path, $method = Api\Request::GET, array $params = [], array $post = [], array $headers = [])
207
	{
208
		if (is_array($method)) {
209
			$headers = $post;
210
			$post = $params;
211
			$params = $method;
212
			$method = Api\Request::GET;
213
		}
214
215
		$params = array_merge($params, [
216
			'method'            => $path,
217
			'format'            => 'json',
218
			'nojsoncallback'    => 1,
219
		]);
220
221
		$response = $this->httpClient->makeRequest(
222
			new Api\Request($this->consumer, $this->config->createUrl('api', 'rest', $params), $method, $post, $headers, $this->getAccessToken()),
223
			'HMAC-SHA1'
224
		);
225
226
		if (!$response->isJson() || (!$data = Utils\ArrayHash::from($response->toArray())) || Utils\Strings::lower($data->stat) != 'ok') {
227
			$ex = $response->toException();
228
			throw $ex;
229
		}
230
231
		if ($response->isPaginated()) {
232
			return new Paginator($this, $response);
233
		}
234
235
		return Utils\ArrayHash::from($response->toArray());
236
	}
237
238
	/**
239
	 * Upload photo to the Flickr
240
	 *
241
	 * @param string $photo
242
	 * @param array $params
243
	 *
244
	 * @return int
245
	 *
246
	 * @throws Exceptions\InvalidArgumentException
247
	 * @throws OAuth\Exceptions\ApiException|static
248
	 */
249
	public function uploadPhoto($photo, array $params = [])
250
	{
251
		$data = $this->processImage('upload', $photo, $params);
252
253
		return (string) $data->photoid;
254
	}
255
256
	/**
257
	 * Replace photo in Flickr
258
	 *
259
	 * @param string $photo
260
	 * @param int $photoId
261
	 * @param bool $async
262
	 *
263
	 * @return int
264
	 *
265
	 * @throws Exceptions\InvalidArgumentException
266
	 * @throws OAuth\Exceptions\ApiException|static
267
	 */
268
	public function replacePhoto($photo, $photoId, $async = FALSE)
269
	{
270
		// Complete request params
271
		$params = [
272
			'photo_id' => $photoId,
273
			'async' => $async ? 1 : 0
274
		];
275
276
		$data = $this->processImage('replace', $photo, $params);
277
278
		return (string) $data->photoid;
279
	}
280
281
	/**
282
	 * @param string $method
283
	 * @param string $photo Path to image
284
	 * @param array $params
285
	 *
286
	 * @return Utils\ArrayHash
287
	 *
288
	 * @throws Exceptions\FileNotFoundException
289
	 * @throws OAuth\Exceptions\ApiException|static
290
	 */
291
	private function processImage($method, $photo, array $params = [])
292
	{
293
		if (!is_file($photo)) {
294
			throw new Exceptions\FileNotFoundException("File '$photo' does not exists. Please provide valid path to file.");
295
		}
296
297
		// Add file to post params
298
		$post = [
299
			'photo' => new \CURLFile($photo),
300
		];
301
302
		$post = array_merge($post, $params);
303
304
		$response = $this->httpClient->makeRequest(
305
			new Api\Request($this->consumer, $this->config->createUrl('upload', $method), Api\Request::POST, $post, [], $this->getAccessToken()),
306
			'HMAC-SHA1'
307
		);
308
309
		if ($response->isOk() && $response->isXml() && ($data = Utils\ArrayHash::from($response->toArray())) && $data->{'@attributes'}->stat == 'ok') {
310
			return $data;
311
312
		} else {
313
			$ex = $response->toException();
314
			throw $ex;
315
		}
316
	}
317
}