Completed
Push — master ( a2b567...2ce769 )
by Dev
02:37 queued 57s
created

Recorder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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