Completed
Push — master ( 6cbd96...c237cf )
by Dev
02:36
created

Recorder::cacheWithIdAsFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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
    public function getFolder()
33
    {
34
        return $this->folder;
35
    }
36
37 4
    public function cache(Harvest $harvest, Url $url)
38
    {
39 4
        if (Recorder::CACHE_NONE === $this->cacheMethod || !$this->mustWeCache($harvest)) {
40
            return;
41 4
        } elseif (Recorder::CACHE_URI === $this->cacheMethod) {
42 2
            return $this->cacheWithUrlAsFilename($harvest, $url);
43
        } else {
44 2
            return $this->cacheWithIdAsFilename($harvest, $url);
45
        }
46
    }
47
48 4
    protected function mustWeCache(Harvest $harvest)
49
    {
50 4
        return false !== strpos($harvest->getResponse()->getContentType(), 'text/html');
51
    }
52
53 2
    protected function cacheWithIdAsFilename(Harvest $harvest, Url $url)
54
    {
55 2
        return file_put_contents(
56 2
            $this->folder.Recorder::CACHE_DIR.'/'.$url->id,
0 ignored issues
show
Bug introduced by
Are you sure $url->id of type mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            $this->folder.Recorder::CACHE_DIR.'/'./** @scrutinizer ignore-type */ $url->id,
Loading history...
57 2
            $harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent()
58
        );
59
    }
60
61 2
    protected function cacheWithUrlAsFilename(Harvest $harvest, Url $url)
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

61
    protected function cacheWithUrlAsFilename(Harvest $harvest, /** @scrutinizer ignore-unused */ Url $url)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63 2
        $url = trim($harvest->getAbsoluteInternalLink($harvest->getResponse()->getEffectiveUrl()), '/').'/';
64 2
        $urlPart = explode('/', $url);
65 2
        $folder = $this->folder.Recorder::CACHE_DIR;
66
67 2
        $urlPartLenght = count($urlPart);
68 2
        for ($i = 0; $i < $urlPartLenght; ++$i) {
69 2
            if ($i == $urlPartLenght - 1) {
70 2
                $filename = empty($urlPart[$i]) ? 'index.html' : $urlPart[$i];
71
72 2
                return file_put_contents(
73 2
                    $folder.'/'.$filename,
74 2
                    $harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent()
75
                );
76
            } else {
77 2
                $folder .= '/'.$urlPart[$i];
78 2
                if (!file_exists($folder) || !is_dir($folder)) {
79
                    mkdir($folder);
80
                }
81
            }
82
        }
83
    }
84
85 6
    public function record(array $urls)
86
    {
87 6
        $fp = fopen($this->folder.'/index.csv', 'w');
88
89 6
        if (false !== $fp) {
90 6
            $header = array_keys(get_object_vars(array_values($urls)[0]));
91 6
            fputcsv($fp, $header);
92
93 6
            foreach ($urls as $url) {
94 6
                fputcsv($fp, get_object_vars($url));
95
            }
96
97 6
            fclose($fp);
98
99 6
            return true;
100
        }
101
102
        return false;
103
    }
104
105 4
    public function recordInboundLink(Url $from, Url $to)
106
    {
107 4
        file_put_contents($this->folder.Recorder::LINKS_DIR.'/To_'.$to->id.'.txt', $from->uri, FILE_APPEND);
0 ignored issues
show
Bug introduced by
Are you sure $to->id of type mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        file_put_contents($this->folder.Recorder::LINKS_DIR.'/To_'./** @scrutinizer ignore-type */ $to->id.'.txt', $from->uri, FILE_APPEND);
Loading history...
108 4
    }
109
110 4
    public function recordOutboundLink(Url $from, array $links)
111
    {
112
        $links = array_map(function ($link) {
113 4
            return $link->getUrl();
114 4
        }, $links);
115 4
        file_put_contents($this->folder.Recorder::LINKS_DIR.'/From_'.$from->id.'.txt', implode(PHP_EOL, $links));
0 ignored issues
show
Bug introduced by
Are you sure $from->id of type mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        file_put_contents($this->folder.Recorder::LINKS_DIR.'/From_'./** @scrutinizer ignore-type */ $from->id.'.txt', implode(PHP_EOL, $links));
Loading history...
116 4
    }
117
}
118