ExtractExternalLinks::filter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
crap 1
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