Completed
Push — master ( efb003...741aff )
by Vinicius
01:27
created

NoProxy::parseUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2.0116
1
<?php
2
namespace CViniciusSDias\GoogleCrawler\Proxy;
3
4
use CViniciusSDias\GoogleCrawler\Exception\InvalidResultException;
5
use CViniciusSDias\GoogleCrawler\Exception\InvalidUrlException;
6
use GuzzleHttp\Client;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Class that represents the absense of a proxy service, making the direct request to the url
11
 * and returning its response
12
 *
13
 * @package CViniciussDias\GoogleCrawler\Proxy
14
 * @author Vinicius Dias
15
 */
16
class NoProxy implements GoogleProxyInterface
17
{
18
    /** {@inheritdoc} */
19 1
    public function getHttpResponse(string $url): ResponseInterface
20
    {
21 1
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
22
            throw new InvalidUrlException("Invalid Google URL: $url");
23
        }
24
25 1
        return (new Client())->request('GET', $url);
26
    }
27
28
    /** {@inheritdoc} */
29 1
    public function parseUrl(string $url): string
30
    {
31
        // Separates the url parts
32 1
        $link = parse_url($url);
33
        // Parses the parameters of the url query
34 1
        parse_str($link['query'], $link);
35
36 1
        $url = filter_var($link['q'], FILTER_VALIDATE_URL);
37
        // If this is not a valid URL, so the result is (probably) an image, news or video suggestion
38 1
        if (!$url) {
39
            throw new InvalidResultException();
40
        }
41
42 1
        return $url;
43
    }
44
}
45