|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/gigya-client |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/gigya-client/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/gigya-client |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\Gigya\Endpoint; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use Graze\Gigya\Response\ResponseFactory; |
|
18
|
|
|
use Graze\Gigya\Response\ResponseFactoryInterface; |
|
19
|
|
|
use Graze\Gigya\Response\ResponseInterface; |
|
20
|
|
|
use Graze\Gigya\Validation\ResponseValidatorInterface; |
|
21
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
22
|
|
|
|
|
23
|
|
|
class Client |
|
24
|
|
|
{ |
|
25
|
|
|
const DOMAIN = 'gigya.com'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $dataCenter; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ResponseFactoryInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $factory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Collection of options to pass to Guzzle. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $options; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $namespace; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var GuzzleClient |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $client; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $config; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var ResponseValidatorInterface[] |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $validators = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param GuzzleClient $client |
|
66
|
|
|
* @param string $namespace |
|
67
|
|
|
* @param string $dataCenter |
|
68
|
|
|
* @param array $config List of configuration settings for Guzzle |
|
69
|
|
|
* @param array $options Options to pass to each request |
|
70
|
|
|
* @param array $validators Response validators |
|
71
|
|
|
* @param ResponseFactoryInterface|null $factory |
|
72
|
|
|
*/ |
|
73
|
48 |
|
public function __construct( |
|
74
|
|
|
GuzzleClient $client, |
|
75
|
|
|
$namespace, |
|
76
|
|
|
$dataCenter, |
|
77
|
|
|
array $config = [], |
|
78
|
|
|
array $options = [], |
|
79
|
|
|
array $validators = [], |
|
80
|
|
|
ResponseFactoryInterface $factory = null |
|
81
|
|
|
) { |
|
82
|
48 |
|
$this->client = $client; |
|
83
|
48 |
|
$this->namespace = $namespace; |
|
84
|
48 |
|
$this->dataCenter = $dataCenter; |
|
85
|
48 |
|
$this->config = $config; |
|
86
|
48 |
|
$this->options = $options; |
|
87
|
48 |
|
$this->factory = $factory ?: new ResponseFactory(); |
|
88
|
48 |
|
array_map([$this, 'addValidator'], $validators); |
|
89
|
48 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param ResponseValidatorInterface $validator |
|
93
|
|
|
*/ |
|
94
|
11 |
|
public function addValidator(ResponseValidatorInterface $validator) |
|
95
|
|
|
{ |
|
96
|
11 |
|
$this->validators[] = $validator; |
|
97
|
11 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the method namespace for each method call. |
|
101
|
|
|
* |
|
102
|
|
|
* Overload this to handle different method namespaces |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
44 |
|
public function getMethodNamespace() |
|
107
|
|
|
{ |
|
108
|
44 |
|
return $this->namespace; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the endpoint for a method. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $method |
|
115
|
|
|
* |
|
116
|
|
|
* @throws Exception |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
48 |
|
public function getEndpoint($method) |
|
121
|
|
|
{ |
|
122
|
48 |
|
return sprintf( |
|
123
|
48 |
|
'https://%s.%s.%s/%s.%s', |
|
124
|
48 |
|
$this->namespace, |
|
125
|
48 |
|
$this->dataCenter, |
|
126
|
48 |
|
static::DOMAIN, |
|
127
|
48 |
|
$this->getMethodNamespace(), |
|
128
|
48 |
|
$method |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $method |
|
134
|
|
|
* @param array $params Parameters to pass as part the of the request |
|
135
|
|
|
* @param array $options Extra options to be passed to guzzle. These will overwrite any existing options defined |
|
136
|
|
|
* by using addOption |
|
137
|
|
|
* |
|
138
|
|
|
* @return ResponseInterface When an error is encountered |
|
139
|
|
|
* |
|
140
|
|
|
* @throws Exception |
|
141
|
|
|
*/ |
|
142
|
48 |
|
public function request($method, array $params = [], array $options = []) |
|
143
|
|
|
{ |
|
144
|
48 |
|
$requestOptions = array_merge($this->options, $options); |
|
145
|
48 |
|
$requestOptions['form_params'] = $params; |
|
146
|
48 |
|
$guzzleResponse = $this->client->post($this->getEndpoint($method), $requestOptions); |
|
147
|
48 |
|
$response = $this->factory->getResponse($guzzleResponse); |
|
148
|
|
|
|
|
149
|
48 |
|
$this->assert($response); |
|
150
|
|
|
|
|
151
|
44 |
|
return $response; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param string $method |
|
156
|
|
|
* @param array $arguments [params, options] |
|
157
|
|
|
* |
|
158
|
|
|
* @return ResponseInterface |
|
159
|
|
|
* @throws Exception |
|
160
|
|
|
*/ |
|
161
|
48 |
|
public function __call($method, array $arguments = []) |
|
162
|
|
|
{ |
|
163
|
48 |
|
return $this->request( |
|
164
|
48 |
|
$method, |
|
165
|
48 |
|
isset($arguments[0]) ? $arguments[0] : [], |
|
166
|
48 |
|
isset($arguments[1]) ? $arguments[1] : [] |
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $className |
|
172
|
|
|
* |
|
173
|
|
|
* @return Client |
|
174
|
|
|
*/ |
|
175
|
2 |
|
protected function endpointFactory($className = self::class) |
|
176
|
|
|
{ |
|
177
|
2 |
|
return new $className( |
|
178
|
2 |
|
$this->client, |
|
179
|
2 |
|
$this->namespace, |
|
180
|
2 |
|
$this->dataCenter, |
|
181
|
2 |
|
$this->config, |
|
182
|
2 |
|
$this->options, |
|
183
|
2 |
|
$this->validators, |
|
184
|
2 |
|
$this->factory |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Throws exceptions if any errors are found. |
|
190
|
|
|
* |
|
191
|
|
|
* @param ResponseInterface $response |
|
192
|
|
|
* |
|
193
|
|
|
* @return void |
|
194
|
|
|
*/ |
|
195
|
48 |
|
public function assert(ResponseInterface $response) |
|
196
|
|
|
{ |
|
197
|
48 |
|
foreach ($this->validators as $validator) { |
|
198
|
11 |
|
if ($validator->canValidate($response)) { |
|
199
|
11 |
|
$validator->assert($response); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
44 |
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|