Completed
Push — master ( f557d8...5bb52d )
by Dev
02:27 queued 47s
created

ExtractExternalLinks::filter()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler;
4
5
class ExtractExternalLinks
6
{
7
8
    protected $id;
9
    protected $dir;
10
    protected $base;
11
    protected $filter;
12
    protected $filterType;
13
    protected $external = [];
14
15 3
    protected function __construct(string $id)
16
    {
17 3
        $this->id = $id;
18 3
        $this->dir = __DIR__.'/../data/'.$id.'/links';
19
20 3
        if (!file_exists($this->dir.'/../config.json')) {
21
            throw new \Exception('no crawl results found for id `'.$id.'`');
22
        }
23 3
        $this->base = json_decode(file_get_contents($this->dir.'/../config.json'), true)['base'];
24 3
    }
25
26 3
    protected function filter($filename)
27
    {
28 3
        return strpos($filename, 'From_') === 0;
29
    }
30
31 3
    public static function scan(string $id)
32
    {
33 3
        $self = new self($id);
34 3
        $self->scanDir();
35
36 3
        return $self->getExternals();
37
    }
38
39 3
    protected function scanDir()
40
    {
41 3
        if ($resource = opendir($this->dir)){
42 3
            while (($filename = readdir($resource)) !== false){
43 3
                if ($this->filter($filename)) {
44 2
                    $this->harvestExternalLinks(file_get_contents($this->dir.'/'.$filename));
45
                }
46
            }
47 3
            closedir($resource);
48
        }
49 3
    }
50
51
52 2
    function harvestExternalLinks(string $content)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
53
    {
54 2
        $lines = explode(chr(10), $content);
55
56 2
        foreach ($lines as $line)
57
        {
58 2
            if (strpos($line, $this->base) !== 0) {
59 2
                $this->external[$line] = ($this->external[$line] ?? 0) + 1;
60
            }
61
        }
62 2
    }
63
64 3
    function getExternals()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    {
66 3
        return $this->external;
67
    }
68
}
69