Completed
Push — master ( 31ed5b...1b5be9 )
by Dev
08:32 queued 07:20
created

Recorder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 15
dl 0
loc 39
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A recordOutboundLink() 0 4 1
A record() 0 19 3
A recordInboundLink() 0 3 1
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler;
4
5
class Recorder
6
{
7
    protected $folder;
8
9
    public function __construct($folder)
10
    {
11
        $this->folder = $folder;
12
    }
13
14
    public function record(array $urls)
15
    {
16
        $fp = fopen($this->folder.'/index.csv', 'w');
17
18
        if ($fp) {
0 ignored issues
show
introduced by
$fp is of type false|resource, thus it always evaluated to false.
Loading history...
19
            $header = array_keys(get_object_vars(array_values($urls)[0]));
20
            fputcsv($fp, $header);
21
22
            foreach ($urls as $url) {
23
                fputcsv($fp, get_object_vars($url));
24
            }
25
26
            fclose($fp);
27
28
29
            return true;
30
        }
31
32
        return false;
33
    }
34
35
    public function recordInboundLink(Url $from, Url $to)
36
    {
37
        file_put_contents($this->folder.'/links/To_'.$to->sha1.'.txt', $from->uri, FILE_APPEND);
38
    }
39
40
    public function recordOutboundLink(Url $from, array $links)
41
    {
42
        $links = array_map(function ($link) { return $link->getUrl(); }, $links);
43
        file_put_contents($this->folder.'/links/From_'.$from->sha1.'.txt', implode(PHP_EOL, $links));
44
    }
45
}
46