Completed
Pull Request — master (#150)
by Brent
02:10
created

CrawlRequestFulfilled::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Crawler\Handlers;
4
5
use Spatie\Crawler\Crawler;
6
use Spatie\Crawler\CrawlUrl;
7
use Spatie\Crawler\LinkAdder;
8
use Spatie\Robots\RobotsMeta;
9
use Spatie\Robots\RobotsHeaders;
10
use Spatie\Crawler\CrawlSubdomains;
11
use Psr\Http\Message\StreamInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
class CrawlRequestFulfilled
15
{
16
    /** @var \Spatie\Crawler\Crawler */
17
    protected $crawler;
18
19
    /** @var \Spatie\Crawler\LinkAdder */
20
    protected $linkAdder;
21
22
    public function __construct(Crawler $crawler)
23
    {
24
        $this->crawler = $crawler;
25
26
        $this->linkAdder = new LinkAdder($this->crawler);
27
    }
28
29
    public function __invoke(ResponseInterface $response, $index)
30
    {
31
        $crawlUrl = $this->crawler->getCrawlQueue()->getUrlById($index);
32
33
        $body = $this->convertBodyToString($response->getBody(), $this->crawler->getMaximumResponseSize());
34
35
        $robotsHeaders = RobotsHeaders::create($response->getHeaders());
36
37
        $robotsMeta = RobotsMeta::create($body);
38
39
        if (! $this->mayIndex($robotsHeaders, $robotsMeta)) {
40
            return;
41
        }
42
43
        $this->handleCrawled($response, $crawlUrl);
44
45
        if (! $this->crawler->getCrawlProfile() instanceof CrawlSubdomains) {
46
            if ($crawlUrl->url->getHost() !== $this->crawler->getBaseUrl()->getHost()) {
47
                return;
48
            }
49
        }
50
51
        if (! $this->mayFollow($robotsHeaders, $robotsMeta)) {
52
            return;
53
        }
54
55
        $this->linkAdder->addFromHtml($body, $crawlUrl->url);
56
    }
57
58
    protected function convertBodyToString(StreamInterface $bodyStream, $readMaximumBytes = 1024 * 1024 * 2): string
59
    {
60
        $bodyStream->rewind();
61
62
        $body = $bodyStream->read($readMaximumBytes);
63
64
        return $body;
65
    }
66
67
    protected function handleCrawled(ResponseInterface $response, CrawlUrl $crawlUrl)
68
    {
69
        $this->crawler->getCrawlObservers()->crawled($crawlUrl, $response);
70
    }
71
72 View Code Duplication
    protected function mayIndex(RobotsHeaders $robotsHeaders, RobotsMeta $robotsMeta): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        if (! $this->crawler->mustRespectRobots()) {
75
            return true;
76
        }
77
78
        if (! $robotsHeaders->mayIndex()) {
79
            return false;
80
        }
81
82
        if (! $robotsMeta->mayIndex()) {
83
            return false;
84
        }
85
86
        return true;
87
    }
88
89 View Code Duplication
    protected function mayFollow(RobotsHeaders $robotsHeaders, RobotsMeta $robotsMeta): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        if (! $this->crawler->mustRespectRobots()) {
92
            return true;
93
        }
94
95
        if (! $robotsHeaders->mayFollow()) {
96
            return false;
97
        }
98
99
        if (! $robotsMeta->mayFollow()) {
100
            return false;
101
        }
102
103
        return true;
104
    }
105
}
106