Completed
Pull Request — master (#150)
by Brent
07:23 queued 33s
created

CrawlRequestFulfilled   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 92
Duplicated Lines 34.78 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 10
dl 32
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B __invoke() 0 28 5
A convertBodyToString() 0 8 1
A handleCrawled() 0 4 1
A mayIndex() 16 16 4
A mayFollow() 16 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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