ForeignDomainFilter::isResponseFiltered()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Crawler\Filter;
4
5
use phm\HttpWebdriverClient\Http\Response\EffectiveUriAwareResponse;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\UriInterface;
8
use whm\Crawler\Filter;
9
use whm\Html\Uri;
10
11
class ForeignDomainFilter implements Filter
12
{
13
    /**
14
     * Filter foreign domains before request is fired
15
     *
16
     * @param UriInterface $currentUri
17
     * @param UriInterface $startUri
18
     * @return bool
19
     */
20
    public function isFiltered(UriInterface $currentUri, UriInterface $startUri)
21
    {
22
        /* @var $currentUri Uri */
23
        /* @var $startUri Uri */
24
25
        $startDomainElements = explode('.', $startUri->getHost());
26
        $currentDomainElements = explode('.', $currentUri->getHost());
27
28
        $startDomainLength = count($startDomainElements);
29
        $currentDomainLength = count($currentDomainElements);
30
31
        if ($currentDomainLength < $startDomainLength) {
32
            return true;
33
        }
34
35
        return $currentUri->getHost($startDomainLength) !== $startUri->getHost($startDomainLength);
36
    }
37
38
    public function isResponseFiltered(ResponseInterface $response, UriInterface $startUri)
39
    {
40
        if ($response instanceof EffectiveUriAwareResponse) {
41
            $isFiltered = $this->isFiltered($response->getEffectiveUri(), $startUri);
42
            return $isFiltered;
43
        }
44
        return false;
45
    }
46
}
47