Completed
Push — master ( 527364...a780c1 )
by Vinicius
05:34
created

KProxy::sendRequestToProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
namespace CViniciusSDias\GoogleCrawler\Proxy;
3
4
use CViniciusSDias\GoogleCrawler\Exception\InvalidResultException;
5
use GuzzleHttp\Client;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Class that for using the kproxy.com servers
10
 *
11
 * @package CViniciusSDias\GoogleCrawler\Proxy
12
 * @author Vinicius Dias
13
 */
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
41
    {
42
        $httpClient = new Client(['cookies' => true]);
43
        $this->accessMainPage($httpClient);
44
        $this->sendRequestToProxy($httpClient, $url);
45
46
        $parsedUrl = parse_url($url);
47
        $queryString = $parsedUrl['query'];
48
        $actualUrl = "{$this->endpoint}/servlet/redirect.srv/swh/suxm/sqyudex/spqr/p1/search?{$queryString}";
49
50
        return $httpClient->request('GET', $actualUrl);
51
    }
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
59
    {
60
        $httpClient->request('GET', "{$this->endpoint}/index.jsp");
61
    }
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
91
    {
92
        $encodedUrl = urlencode($url);
93
        $postData = ['page' => $encodedUrl, 'x' => 0, 'y' => 0];
94
        $headers = [
95
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
96
        ];
97
        $httpClient->request(
98
            'POST',
99
            "{$this->endpoint}/doproxy.jsp",
100
            ['form_params' => $postData, 'headers' => $headers]
101
        );
102
    }
103
}
104