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

ExtractExternalLinks   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
wmc 12
eloc 26
dl 0
loc 62
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A filter() 0 3 1
A scanDir() 0 9 4
A getExternals() 0 3 1
A harvestExternalLinks() 0 8 3
A scan() 0 6 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