1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WordSelector\Proxy; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
8
|
|
|
use ProxyManager\Factory\RemoteObject\AdapterInterface; |
9
|
|
|
use WordSelector\Entity\Word; |
10
|
|
|
use WordSelector\WordSelector; |
11
|
|
|
|
12
|
|
|
class WordSelectorProxyAdapter implements AdapterInterface |
13
|
|
|
{ |
14
|
|
|
const GET_RANDOM_WORD_METHOD = 'getRandomWord'; |
15
|
|
|
|
16
|
|
|
const LENGTH_OPTION = 'length'; |
17
|
|
|
const LANG_OPTION = 'lang'; |
18
|
|
|
const COMPLEXITY_OPTION = 'complexity'; |
19
|
|
|
|
20
|
|
|
const WORD_RESPONSE = 'word'; |
21
|
|
|
const LANG_RESPONSE = 'lang'; |
22
|
|
|
const COMPLEXITY_RESPONSE = 'complexity'; |
23
|
|
|
|
24
|
|
|
const ERROR_RESPONSE = 'error'; |
25
|
|
|
|
26
|
|
|
const API_ENDPOINT = '/random'; |
27
|
|
|
const HTTP_METHOD = 'GET'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Client |
31
|
|
|
*/ |
32
|
|
|
private $client; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Construct |
36
|
|
|
* |
37
|
|
|
* @param Client $client |
38
|
|
|
*/ |
39
|
18 |
|
public function __construct(Client $client) |
40
|
|
|
{ |
41
|
18 |
|
$this->client = $client; |
42
|
18 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Call remote object |
46
|
|
|
* |
47
|
|
|
* @param string $wrappedClass |
48
|
|
|
* @param string $method |
49
|
|
|
* @param array $params |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
18 |
|
public function call($wrappedClass, $method, array $params = []) |
53
|
|
|
{ |
54
|
18 |
|
if ($wrappedClass !== WordSelector::class) { |
55
|
3 |
|
throw new \InvalidArgumentException('Cannot call this class'); |
56
|
|
|
} |
57
|
|
|
|
58
|
15 |
|
if ($method !== self::GET_RANDOM_WORD_METHOD) { |
59
|
3 |
|
throw new \InvalidArgumentException('Cannot call this method'); |
60
|
|
|
} |
61
|
|
|
|
62
|
12 |
|
if (count($params) !== 3) { |
63
|
3 |
|
throw new \InvalidArgumentException('Cannot call this method with the given parameters'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$options = [ |
67
|
9 |
|
self::LENGTH_OPTION => $params[0], |
68
|
9 |
|
self::LANG_OPTION => $params[1], |
69
|
9 |
|
self::COMPLEXITY_OPTION => $params[2] |
70
|
6 |
|
]; |
71
|
|
|
|
72
|
|
|
try { |
73
|
9 |
|
$response = $this->client->request( |
74
|
9 |
|
self::HTTP_METHOD, |
75
|
9 |
|
self::API_ENDPOINT . '?' . http_build_query($options) |
76
|
6 |
|
); |
77
|
3 |
|
$decodedJson = json_decode((string) $response->getBody()); |
78
|
3 |
|
return new Word( |
79
|
3 |
|
$decodedJson->{self::WORD_RESPONSE}, |
80
|
3 |
|
$decodedJson->{self::LANG_RESPONSE}, |
81
|
3 |
|
$decodedJson->{self::COMPLEXITY_RESPONSE} |
82
|
2 |
|
); |
83
|
6 |
|
} catch (ClientException $e) { |
84
|
3 |
|
$decodedJson = json_decode((string) $e->getResponse()->getBody()); |
85
|
3 |
|
throw new \InvalidArgumentException($decodedJson->{self::ERROR_RESPONSE}); |
86
|
3 |
|
} catch (BadResponseException $e) { |
87
|
3 |
|
$exceptionBody = (string) $e->getResponse()->getBody(); |
88
|
3 |
|
throw new \RuntimeException($exceptionBody); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|