Completed
Push — master ( b17a5a...5b7630 )
by Dev
02:30
created

Recorder   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 19
eloc 52
dl 0
loc 104
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A cache() 0 8 4
A mustWeCache() 0 3 1
A recordOutboundLink() 0 6 1
A record() 0 18 3
A cacheWithIdAsFilename() 0 5 1
A cacheWithUrlAsFilename() 0 19 6
A recordInboundLink() 0 3 1
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler;
4
5
use PiedWeb\UrlHarvester\Harvest;
6
7
class Recorder
8
{
9
    const LINKS_DIR = '/links';
10
    const CACHE_DIR = '/cache';
11
12
    const CACHE_NONE = 0;
13
    const CACHE_ID = 2;
14
    const CACHE_URI = 1;
15
16
    protected $folder;
17
    protected $cacheMethod;
18
19 6
    public function __construct($folder, $cacheMethod = Record::CACHE_ID)
20
    {
21 6
        $this->folder = $folder;
22 6
        $this->cacheMethod = $cacheMethod;
23
24
        //exec('rm -rf '.$folder);
25 6
        if (!file_exists($folder)) {
26 3
            mkdir($folder);
27 3
            mkdir($folder.Recorder::LINKS_DIR);
28 3
            mkdir($folder.Recorder::CACHE_DIR);
29
        }
30 6
    }
31
32 4
    public function cache(Harvest $harvest, Url $url)
33
    {
34 4
        if (Recorder::CACHE_NONE === $this->cacheMethod || !$this->mustWeCache($harvest)) {
35
            return;
36 4
        } elseif (Recorder::CACHE_URI === $this->cacheMethod) {
37 2
            return $this->cacheWithUrlAsFilename($harvest, $url);
38
        } else {
39 2
            return $this->cacheWithIdAsFilename($harvest, $url);
40
        }
41
    }
42
43 4
    protected function mustWeCache(Harvest $harvest)
44
    {
45 4
        return false !== strpos($harvest->getResponse()->getContentType(), 'text/html');
46
    }
47
48 2
    protected function cacheWithIdAsFilename(Harvest $harvest, Url $url)
49
    {
50 2
        return file_put_contents(
51 2
            $this->folder.Recorder::CACHE_DIR.'/'.(string) $url->id,
52 2
            $harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent()
53
        );
54
    }
55
56 2
    protected function cacheWithUrlAsFilename(Harvest $harvest, Url $url)
57
    {
58 2
        $url = trim($url->uri, '/').'/';
59 2
        $urlPart = explode('/', $url);
60 2
        $folder = $this->folder.Recorder::CACHE_DIR;
61
62 2
        $urlPartLenght = count($urlPart);
63 2
        for ($i = 0; $i < $urlPartLenght; ++$i) {
64 2
            if ($i == $urlPartLenght - 1) {
65 2
                $filename = empty($urlPart[$i]) ? 'index.html' : $urlPart[$i];
66
67 2
                return file_put_contents(
68 2
                    $folder.'/'.$filename,
69 2
                    $harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent()
70
                );
71
            } else {
72 2
                $folder .= '/'.$urlPart[$i];
73 2
                if (!file_exists($folder) || !is_dir($folder)) {
74
                    mkdir($folder);
75
                }
76
            }
77
        }
78
    }
79
80 6
    public function record(array $urls)
81
    {
82 6
        $fp = fopen($this->folder.'/index.csv', 'w');
83
84 6
        if (false !== $fp) {
85 6
            $header = array_keys(get_object_vars(array_values($urls)[0]));
86 6
            fputcsv($fp, $header);
87
88 6
            foreach ($urls as $url) {
89 6
                fputcsv($fp, get_object_vars($url));
90
            }
91
92 6
            fclose($fp);
93
94 6
            return true;
95
        }
96
97
        return false;
98
    }
99
100 4
    public function recordInboundLink(Url $from, Url $to)
101
    {
102 4
        file_put_contents($this->folder.Recorder::LINKS_DIR.'/To_'.(string) $to->id, $from->uri, FILE_APPEND);
103 4
    }
104
105 4
    public function recordOutboundLink(Url $from, array $links)
106
    {
107
        $links = array_map(function ($link) {
108 4
            return $link->getUrl();
109 4
        }, $links);
110 4
        file_put_contents($this->folder.Recorder::LINKS_DIR.'/From_'.(string) $from->id, implode(PHP_EOL, $links));
111 4
    }
112
}
113