NoProxy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getHttpResponse() 0 8 2
A parseUrl() 0 15 2
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 2
    public function getHttpResponse(string $url): ResponseInterface
20
    {
21 2
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
22 1
            throw new InvalidUrlException("Invalid Google URL: $url");
23
        }
24
25 1
        return (new Client())->request('GET', $url);
26
    }
27
28
    /** {@inheritdoc} */
29 2
    public function parseUrl(string $url): string
30
    {
31
        // Separates the url parts
32 2
        $link = parse_url($url);
33
        // Parses the parameters of the url query
34 2
        parse_str($link['query'], $link);
35
36 2
        $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 2
        if (!$url) {
39 1
            throw new InvalidResultException();
40
        }
41
42 1
        return $url;
43
    }
44
}
45