1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shutterstock\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as Guzzle; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use GuzzleHttp\Middleware; |
8
|
|
|
use GuzzleHttp\Promise\PromiseInterface as Promise; |
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
10
|
|
|
|
11
|
|
|
class Client |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** @var Guzzle */ |
15
|
|
|
protected $guzzle; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $clientId |
19
|
|
|
* @param string $clientSecret |
20
|
|
|
*/ |
21
|
|
|
public function __construct($clientId, $clientSecret) |
22
|
|
|
{ |
23
|
|
|
$stack = HandlerStack::create(); |
24
|
|
|
$stack->push(Middleware::mapResponse(function (Response $response) { |
25
|
|
|
$jsonStream = new JsonStream($response->getBody()); |
26
|
|
|
return $response->withBody($jsonStream); |
27
|
|
|
})); |
28
|
|
|
|
29
|
|
|
$guzzle = new Guzzle([ |
30
|
|
|
'base_uri' => 'https://api.shutterstock.com/v2/', |
31
|
|
|
'auth' => [$clientId, $clientSecret], |
32
|
|
|
'handler' => $stack, |
33
|
|
|
]); |
34
|
|
|
$this->guzzle = $guzzle; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $uri |
39
|
|
|
* @param array $query |
40
|
|
|
* @param array $options |
41
|
|
|
* |
42
|
|
|
* @return Response |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function get($uri, array $query = [], array $options = []) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
if (!empty($query)) { |
47
|
|
|
$options['query'] = $this->buildQuery($query); |
48
|
|
|
} |
49
|
|
|
return $this->guzzle->get($uri, $options); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $uri |
54
|
|
|
* @param array $query |
55
|
|
|
* @param array $options |
56
|
|
|
* |
57
|
|
|
* @return Promise |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function getAsync($uri, array $query = [], array $options = []) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if (!empty($query)) { |
62
|
|
|
$options['query'] = $this->buildQuery($query); |
63
|
|
|
} |
64
|
|
|
return $this->guzzle->getAsync($uri, $options); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $query |
69
|
|
|
* @param string $separator |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function buildQuery(array $query, $separator = '&') |
74
|
|
|
{ |
75
|
|
|
$queryPieces = []; |
76
|
|
|
foreach ($query as $key => $value) { |
77
|
|
|
if (!is_array($value)) { |
78
|
|
|
$piece = urlencode($key) . '=' . urlencode($value); |
79
|
|
|
array_push($queryPieces, $piece); |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
foreach ($value as $valuePiece) { |
83
|
|
|
$piece = urlencode($key) . '=' . urlencode($valuePiece); |
84
|
|
|
array_push($queryPieces, $piece); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$queryString = implode($separator, $queryPieces); |
89
|
|
|
return $queryString; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $uri |
94
|
|
|
* @param array $body |
95
|
|
|
* @param array $options |
96
|
|
|
* |
97
|
|
|
* @return Response |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function post($uri, array $body = [], array $options = []) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if (!empty($body)) { |
102
|
|
|
$options['json'] = $body; |
103
|
|
|
} |
104
|
|
|
return $this->guzzle->post($uri, $options); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $uri |
109
|
|
|
* @param array $body |
110
|
|
|
* @param array $options |
111
|
|
|
* |
112
|
|
|
* @return Promise |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function postAsync($uri, array $body = [], array $options = []) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
if (!empty($body)) { |
117
|
|
|
$options['json'] = $body; |
118
|
|
|
} |
119
|
|
|
return $this->guzzle->postAsync($uri, $options); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.