Conditions | 5 |
Paths | 9 |
Total Lines | 35 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 30 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
37 | public function call($wrappedClass, $method, array $params = []) |
||
38 | { |
||
39 | if ($wrappedClass !== WordSelector::class) { |
||
40 | throw new \InvalidArgumentException('Cannot call this class'); |
||
41 | } |
||
42 | |||
43 | if ($method !== 'getRandomWord') { |
||
44 | throw new \InvalidArgumentException('Cannot call this method'); |
||
45 | } |
||
46 | |||
47 | $options = [ |
||
48 | 'length' => $params[0], |
||
49 | 'lang' => $params[1], |
||
50 | 'complexity' => $params[2] |
||
51 | ]; |
||
52 | |||
53 | try { |
||
54 | $response = $this->client->request( |
||
55 | 'GET', |
||
56 | '/random?' . http_build_query($options) |
||
57 | ); |
||
58 | $decodedJson = json_decode((string) $response->getBody()); |
||
59 | return new Word( |
||
60 | $decodedJson->word, |
||
61 | $decodedJson->lang, |
||
62 | $decodedJson->complexity |
||
63 | ); |
||
64 | } catch (ClientException $e) { |
||
65 | $decodedJson = json_decode((string) $e->getResponse()->getBody()); |
||
66 | throw new \InvalidArgumentException($decodedJson->error); |
||
67 | } catch (BadResponseException $e) { |
||
68 | $exceptionBody = (string) $e->getResponse()->getBody(); |
||
69 | throw new \RuntimeException($exceptionBody); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 |