1 | <?php |
||
14 | class KProxy implements GoogleProxyInterface |
||
15 | { |
||
16 | /** @var string $endpoint */ |
||
17 | protected $endpoint; |
||
18 | /** @var int $serverNumber */ |
||
19 | protected $serverNumber; |
||
20 | |||
21 | /** |
||
22 | * Constructor that initializes the proxy service in one of its servers, which go from 1 to 9 |
||
23 | * |
||
24 | * @param int $serverNumber |
||
25 | */ |
||
26 | 6 | public function __construct(int $serverNumber) |
|
27 | { |
||
28 | 6 | if ($serverNumber > 9 || $serverNumber < 1) { |
|
29 | 2 | throw new \InvalidArgumentException(); |
|
30 | } |
||
31 | 4 | $this->serverNumber = $serverNumber; |
|
32 | 4 | $this->endpoint = "http://server{$serverNumber}.kproxy.com"; |
|
33 | 4 | } |
|
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | * @throws \GuzzleHttp\Exception\ServerException If the proxy was overused |
||
38 | * @throws \GuzzleHttp\Exception\ConnectException If the proxy is unavailable |
||
39 | */ |
||
40 | public function getHttpResponse(string $url): ResponseInterface |
||
52 | |||
53 | /** |
||
54 | * Accesses the main page of the kproxy.com server. This is mandatory. |
||
55 | * |
||
56 | * @param Client $httpClient |
||
57 | */ |
||
58 | private function accessMainPage(Client $httpClient): void |
||
62 | |||
63 | /** {@inheritdoc} */ |
||
64 | 3 | public function parseUrl(string $url): string |
|
65 | { |
||
66 | 3 | $parsedUrl = parse_url($url); |
|
67 | 3 | parse_str($parsedUrl['query'], $link); |
|
68 | |||
69 | 3 | if (!array_key_exists('q', $link)) { |
|
70 | // Generally a book suggestion |
||
71 | throw new InvalidResultException(); |
||
72 | } |
||
73 | |||
74 | 3 | $url = filter_var($link['q'], FILTER_VALIDATE_URL); |
|
75 | // If this is not a valid URL, so the result is (probably) an image, news or video suggestion |
||
76 | 3 | if (!$url) { |
|
77 | 2 | throw new InvalidResultException(); |
|
78 | } |
||
79 | |||
80 | 1 | return $url; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Sends the request to the proxy service that saves the info in session. After this we can redirect |
||
85 | * the user to the search results |
||
86 | * |
||
87 | * @param Client $httpClient |
||
88 | * @param string $url |
||
89 | */ |
||
90 | private function sendRequestToProxy(Client $httpClient, string $url): void |
||
103 | } |
||
104 |