Passed
Push — master ( 7e03c5...6dfa53 )
by Dev
34:30 queued 19:18
created

CrawlerRestart::getHarvest()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler;
4
5
use PiedWeb\Curl\ResponseFromCache;
6
use PiedWeb\UrlHarvester\Harvest;
7
8
class CrawlerRestart extends CrawlerContinue
9
{
10 3
    /**
11
     * @var bool
12 3
     */
13
    protected $fromCache;
14 3
15
    public function __construct(string $id, bool $fromCache = false, ?string $dataDirectory = null)
16 3
    {
17 3
        $this->fromCache = $fromCache;
18
19 3
        $this->config = CrawlerConfig::loadFrom($id, $dataDirectory);
20
21 3
        $this->recorder = new Recorder($this->config->getDataFolder(), $this->config->getCacheMethod());
22 3
23 3
        $this->resetLinks();
24
25 3
        $this->urls[$this->config->getStartUrl()] = null;
26
    }
27 3
28 3
    protected function resetLinks()
29
    {
30 3
        exec('rm -rf '.$this->config->getDataFolder().Recorder::LINKS_DIR);
31
        mkdir($this->config->getDataFolder().Recorder::LINKS_DIR);
32 3
    }
33 3
34 3
    protected function getHarvester(Url $url)
35 2
    {
36 2
        if (false === $this->fromCache) {
37 2
            return parent::getHarvester($url);
38 2
        }
39
40
        $filePath = $this->recorder->getCacheFilePath($url);
41 2
        if (null !== $filePath && file_exists($filePath)) {
42
            $response = new ResponseFromCache(
43
                $filePath,
44
                $this->config->getBase().$url->uri,
45 3
                json_decode(file_get_contents($filePath.'---info'), true)
46
            );
47
48
            return new Harvest($response);
49
        }
50
    }
51
}
52