Completed
Push — master ( f9e659...0e01a4 )
by Dev
13:26 queued 11:45
created

Recorder::record()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.9666
c 0
b 0
f 0
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 4
            mkdir($folder);
27 4
            mkdir($folder.Recorder::LINKS_DIR);
28 4
            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
        }
37
38 4
        $filePath = $this->getCacheFilePath($url);
39 4
        if (!file_exists($filePath)) {
40 4
            file_put_contents(
41 4
                $filePath,
42 4
                $harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent()
43
            );
44
45 4
            return file_put_contents($filePath.'---info', json_encode($harvest->getResponse()->getInfo()));
46
        }
47 2
    }
48
49 5
    public function getCacheFilePath(Url $url)
50
    {
51 5
        if (Recorder::CACHE_URI === $this->cacheMethod) {
52 3
            return $this->getCacheFilePathWithUrlAsFilename($url);
53
        } else {
54 2
            return $this->getCacheFilePathWithIdAsFilename($url);
55
        }
56
    }
57
58 3
    protected function getCacheFilePathWithUrlAsFilename(Url $url)
59
    {
60 3
        $url = trim($url->uri, '/').'/';
61 3
        $urlPart = explode('/', $url);
62 3
        $folder = $this->folder.Recorder::CACHE_DIR;
63
64 3
        $urlPartLenght = count($urlPart);
65 3
        for ($i = 0; $i < $urlPartLenght; ++$i) {
66 3
            if ($i == $urlPartLenght - 1) {
67 3
                $filename = empty($urlPart[$i]) ? 'index.html' : $urlPart[$i];
68 3
                break;
69
            } else {
70 3
                $folder .= '/'.$urlPart[$i];
71 3
                if (!file_exists($folder) || !is_dir($folder)) {
72
                    mkdir($folder);
73
                }
74
            }
75
        }
76
77 3
        return $folder.'/'.$filename;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filename does not seem to be defined for all execution paths leading up to this point.
Loading history...
78
    }
79
80 2
    protected function getCacheFilePathWithIdAsFilename(Url $url)
81
    {
82 2
        return $this->folder.Recorder::CACHE_DIR.'/'.(string) $url->id;
83
    }
84
85 4
    protected function mustWeCache(Harvest $harvest)
86
    {
87 4
        return false !== strpos($harvest->getResponse()->getContentType(), 'text/html');
88
    }
89
90 6
    public function record(array $urls)
91
    {
92 6
        $fp = fopen($this->folder.'/index.csv', 'w');
93
94 6
        if (false !== $fp) {
95 6
            $header = array_keys(get_object_vars(array_values($urls)[0]));
96 6
            fputcsv($fp, $header);
97
98 6
            foreach ($urls as $url) {
99 6
                fputcsv($fp, get_object_vars($url));
100
            }
101
102 6
            fclose($fp);
103
104 6
            return true;
105
        }
106
107
        return false;
108
    }
109
110 4
    public function recordInboundLink(Url $from, Url $to)
111
    {
112 4
        file_put_contents($this->folder.Recorder::LINKS_DIR.'/To_'.(string) $to->id, $from->uri.PHP_EOL, FILE_APPEND);
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