Completed
Push — master ( 9fafdc...f557d8 )
by Dev
02:28
created

Recorder   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 21
eloc 56
dl 0
loc 114
ccs 54
cts 58
cp 0.931
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A recordOutboundLink() 0 6 1
A __construct() 0 9 2
A cache() 0 14 4
A getCacheFilePathWithUrlAsFilename() 0 14 6
A getCacheFilePath() 0 6 2
A mustWeCache() 0 3 1
A record() 0 18 3
A getCacheFilePathWithIdAsFilename() 0 3 1
A recordInboundLink() 0 6 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 6
        if (!file_exists($folder)) {
25 3
            mkdir($folder);
26 3
            mkdir($folder.Recorder::LINKS_DIR);
27 3
            mkdir($folder.Recorder::CACHE_DIR);
28
        }
29 6
    }
30
31 4
    public function cache(Harvest $harvest, Url $url)
32
    {
33 4
        if (Recorder::CACHE_NONE === $this->cacheMethod || !$this->mustWeCache($harvest)) {
34
            return;
35
        }
36
37 4
        $filePath = $this->getCacheFilePath($url);
38 4
        if (!file_exists($filePath)) {
39 4
            file_put_contents(
40 4
                $filePath,
41 4
                $harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent()
42
            );
43
44 4
            return file_put_contents($filePath.'---info', json_encode($harvest->getResponse()->getInfo()));
45
        }
46 2
    }
47
48 5
    public function getCacheFilePath(Url $url)
49
    {
50 5
        if (Recorder::CACHE_URI === $this->cacheMethod) {
51 3
            return $this->getCacheFilePathWithUrlAsFilename($url);
52
        } else {
53 2
            return $this->getCacheFilePathWithIdAsFilename($url);
54
        }
55
    }
56
57 3
    protected function getCacheFilePathWithUrlAsFilename(Url $url)
58
    {
59 3
        $url = trim($url->uri, '/').'/';
60 3
        $urlPart = explode('/', $url);
61 3
        $folder = $this->folder.Recorder::CACHE_DIR;
62
63 3
        $urlPartLenght = count($urlPart);
64 3
        for ($i = 0; $i < $urlPartLenght; ++$i) {
65 3
            if ($i == $urlPartLenght - 1) {
66 3
                return $folder.'/'.(empty($urlPart[$i]) ? 'index.html' : $urlPart[$i]);
67
            } else {
68 3
                $folder .= '/'.$urlPart[$i];
69 3
                if (!file_exists($folder) || !is_dir($folder)) {
70
                    mkdir($folder);
71
                }
72
            }
73
        }
74
    }
75
76 2
    protected function getCacheFilePathWithIdAsFilename(Url $url)
77
    {
78 2
        return $this->folder.Recorder::CACHE_DIR.'/'.(string) $url->id;
79
    }
80
81 4
    protected function mustWeCache(Harvest $harvest)
82
    {
83 4
        return false !== strpos($harvest->getResponse()->getContentType(), 'text/html');
84
    }
85
86 6
    public function record(array $urls)
87
    {
88 6
        $fp = fopen($this->folder.'/index.csv', 'w');
89
90 6
        if (false !== $fp) {
91 6
            $header = array_keys(get_object_vars(array_values($urls)[0]));
92 6
            fputcsv($fp, $header);
93
94 6
            foreach ($urls as $url) {
95 6
                fputcsv($fp, get_object_vars($url));
96
            }
97
98 6
            fclose($fp);
99
100 6
            return true;
101
        }
102
103
        return false;
104
    }
105
106 4
    public function recordInboundLink(Url $from, Url $to, int $type)
107
    {
108 4
        file_put_contents(
109 4
            $this->folder.Recorder::LINKS_DIR.'/To_'.(string) $to->id.'_'.$type,
110 4
            $from->uri.PHP_EOL,
111 4
            FILE_APPEND
112
        );
113 4
    }
114
115 4
    public function recordOutboundLink(Url $from, array $links)
116
    {
117
        $links = array_map(function ($link) {
118 4
            return $link->getUrl();
119 4
        }, $links);
120 4
        file_put_contents($this->folder.Recorder::LINKS_DIR.'/From_'.(string) $from->id, implode(PHP_EOL, $links));
121 4
    }
122
}
123