Total Complexity | 11 |
Total Lines | 77 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
15 | class Pwned |
||
16 | { |
||
17 | protected $endpoint; |
||
18 | protected $client; |
||
19 | |||
20 | /** |
||
21 | * Pwned constructor. |
||
22 | */ |
||
23 | public function __construct() |
||
24 | { |
||
25 | $this->client = new Client(); |
||
26 | $this->endpoint = 'https://haveibeenpwned.com/api/v2/'; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Set the filters. |
||
31 | * |
||
32 | * @param array $filters |
||
33 | * @return string |
||
34 | */ |
||
35 | protected function setFilters(string $url, array $filters): string |
||
36 | { |
||
37 | // Append query filters if available. |
||
38 | if (!empty($filters)) { |
||
39 | $query = http_build_query($filters); |
||
40 | $url .= '?' . $query; |
||
41 | } |
||
42 | |||
43 | return $url; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Guzzle GET request. |
||
48 | * |
||
49 | * @param string $url |
||
50 | * @return array |
||
51 | */ |
||
52 | protected function requestGet(string $url): array |
||
53 | { |
||
54 | try { |
||
55 | $result = $this->client->request('GET', $url); |
||
56 | $jsonResponse = $this->getJsonResponse($result); |
||
57 | |||
58 | return $jsonResponse; |
||
59 | } catch(\GuzzleHttp\Exception\ClientException $e) { |
||
60 | return [ |
||
61 | 'code' => $e->getCode(), |
||
62 | 'message' => $e->getMessage() |
||
63 | ]; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * This function converts a guzzle response into a JSON response. |
||
69 | * |
||
70 | * @param \GuzzleHttp\Psr7\Response $response |
||
71 | * @return array |
||
72 | */ |
||
73 | private function getJsonResponse(\GuzzleHttp\Psr7\Response $response): array |
||
92 | } |
||
93 | } |