ExtractExternalLinks::scanLinksDir()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 12
rs 10
ccs 7
cts 7
cp 1
crap 4
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler;
4
5
class ExtractExternalLinks
6
{
7
    protected $id;
8
    protected $dir;
9
    protected $filter;
10
    protected $filterType;
11
    protected $external = [];
12
13
    /**
14 3
     * @var CrawlerConfig
15
     */
16 3
    protected $config;
17 3
18
    protected function __construct(string $id, ?string $dataDirectory = null)
19 3
    {
20
        $this->config = CrawlerConfig::loadFrom($id, $dataDirectory);
21
        $this->dir = $this->config->getDataFolder().'/links';
22 3
    }
23 3
24
    protected function filter($filename)
25 3
    {
26
        return 0 === strpos($filename, 'From_');
27 3
    }
28
29
    public static function scan(string $id)
30 3
    {
31
        $self = new self($id);
32 3
        $self->scanLinksDir();
33 3
34
        return $self->getExternals();
35 3
    }
36
37
    protected function scanLinksDir()
38 3
    {
39
        if ($resource = opendir($this->dir)) {
40 3
            while (false !== ($filename = readdir($resource))) {
41 3
                if ($this->filter($filename)) {
42 3
                    $this->harvestExternalLinks(
43 2
                        trim(file_get_contents($this->dir.'/'.$filename)),
44
                        $this->config->getUrlFromId(substr($filename, strlen('From_')))
45
                    );
46 3
                }
47
            }
48 3
            closedir($resource);
49
        }
50 2
    }
51
52 2
    protected function harvestExternalLinks(string $strUrlsLinked, $from)
53
    {
54 2
        if (empty($strUrlsLinked)) {
55 2
            return;
56 2
        }
57
58
        $lines = explode(chr(10), $strUrlsLinked);
59 2
60
        foreach ($lines as $line) {
61 3
            if (0 !== strpos($line, $this->config->getBase())) {
62
                if (! isset($this->external[$line])) {
63 3
                    $this->external[$line] = [];
64
                }
65
                $this->external[$line][] = $from;
66
            }
67
        }
68
    }
69
70
    public function getExternals()
71
    {
72
        return $this->external;
73
    }
74
}
75