Passed
Push — master ( 560dd8...313185 )
by Dev
22:47 queued 09:43
created

CrawlerUrlFromCache::getHarvester()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
eloc 15
c 2
b 1
f 0
nc 10
nop 0
dl 0
loc 28
rs 8.4444
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler;
4
5
use PiedWeb\Curl\ResponseFromCache;
6
use PiedWeb\UrlHarvester\Harvest;
7
8
class CrawlerUrlFromCache extends CrawlerUrl
9
{
10
    public function getHarvester(): ?Harvest
11
    {
12
        if (null !== $this->harvest) {
13
            return $this->harvest === false ? null : $this->harvest;
14
        }
15
16
        $filePath = $this->config->getRecorder()->getCacheFilePath($this->url);
17
        if (null !== $filePath && file_exists($filePath)) {
18
            $response = new ResponseFromCache(
19
                $filePath,
20
                $this->config->getBase().$this->url->getUri(),
21
                json_decode(file_get_contents($filePath.'---info'), true)
22
            );
23
24
            $this->harvest = new Harvest($response);
25
        }
26
27
        $this->harvest ?? $this->harvest = parent::getHarvester();
28
29
        if (!$this->harvest instanceof Harvest) {
30
            $this->harvest = false;
31
        }
32
33
        if (null !== $this->getHarvester() && null !== $this->config->getRobotsTxtCached()) {
34
            $this->harvest->setRobotsTxt($this->config->getRobotsTxtCached());
35
        }
36
37
        return $this->getHarvester();
38
    }
39
}
40