Completed
Push — master ( 40a7a3...6234fd )
by Dev
02:19
created

CrawlerRestart   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 38
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A resetLinks() 0 4 1
A getHarvest() 0 16 4
A loadFromPreviousCrawl() 0 3 1
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler;
4
5
use PiedWeb\UrlHarvester\Harvest;
6
use PiedWeb\Curl\ResponseFromCache;
7
8
class CrawlerRestart extends CrawlerContinue
9
{
10 3
    public function __construct(string $id, bool $fromCache = false)
11
    {
12 3
        $this->fromCache = $fromCache;
13
14 3
        parent::__construct($id);
15
16 3
        $this->resetLinks();
17 3
    }
18
19 3
    protected function resetLinks()
20
    {
21 3
        exec('rm -rf '.$this->getDataFolder().Recorder::LINKS_DIR);
22 3
        mkdir($this->getDataFolder().Recorder::LINKS_DIR);
23 3
    }
24
25 3
    protected function loadFromPreviousCrawl(string $startUrl)
26
    {
27 3
        $this->urls[$startUrl] = null;
28 3
    }
29
30 3
    protected function getHarvest(Url $url)
31
    {
32 3
        if (true === $this->fromCache) {
33 3
            $filePath = $this->recorder->getCacheFilePath($url);
34 3
            if (null !== $filePath && file_exists($filePath)) {
35 2
                $response = new ResponseFromCache(
36 2
                    $filePath,
37 2
                    $this->base.$url->uri,
38 2
                    json_decode(file_get_contents($filePath.'---info'), true)
39
                );
40
41 2
                return new Harvest($response);
42
            }
43
        }
44
45 3
        return parent::getHarvest($url);
46
    }
47
}
48