Completed
Pull Request — master (#168)
by
unknown
01:49 queued 11s
created

CrawlRequestFulfilledAbstract   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 31
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
__invoke() 0 1 ?
A handleCrawled() 0 4 1
A convertBodyToString() 0 8 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 Psr\Http\Message\StreamInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
abstract class CrawlRequestFulfilledAbstract
12
{
13
    /** @var \Spatie\Crawler\Crawler */
14
    protected $crawler;
15
16
    /** @var \Spatie\Crawler\LinkAdder */
17
    protected $linkAdder;
18
19
    public function __construct(Crawler $crawler)
20
    {
21
        $this->crawler = $crawler;
22
23
        $this->linkAdder = new LinkAdder($this->crawler);
24
    }
25
26
    abstract public function __invoke(ResponseInterface $response, $index);
27
28
    protected function handleCrawled(ResponseInterface $response, CrawlUrl $crawlUrl)
29
    {
30
        $this->crawler->getCrawlObservers()->crawled($crawlUrl, $response);
31
    }
32
33
    protected function convertBodyToString(StreamInterface $bodyStream, $readMaximumBytes = 1024 * 1024 * 2): string
34
    {
35
        $bodyStream->rewind();
36
37
        $body = $bodyStream->read($readMaximumBytes);
38
39
        return $body;
40
    }
41
}
42