HarvestLinksTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 26
c 1
b 0
f 0
dl 0
loc 58
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A classifyLinks() 0 6 2
A getNbrDuplicateLinks() 0 9 2
A getLinks() 0 18 6
A getLinkedRessources() 0 3 1
1
<?php
2
3
namespace PiedWeb\UrlHarvester;
4
5
trait HarvestLinksTrait
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $links;
11
12
    protected $linksPerType;
13
14
    abstract public function getDom();
15 3
16
    public function getLinkedRessources()
17 3
    {
18
        return ExtractLinks::get($this, ExtractLinks::SELECT_ALL);
19
    }
20 9
21
    public function getLinks($type = null): array
22 9
    {
23 6
        if (null === $this->links) {
24 6
            $this->links = ExtractLinks::get($this, ExtractLinks::SELECT_A);
25
            $this->classifyLinks();
26
        }
27
28 9
        switch ($type) {
29 6
            case Link::LINK_SELF:
30 9
                return $this->linksPerType[Link::LINK_SELF] ?? [];
31 6
            case Link::LINK_INTERNAL:
32 9
                return $this->linksPerType[Link::LINK_INTERNAL] ?? [];
33 6
            case Link::LINK_SUB:
34 9
                return $this->linksPerType[Link::LINK_SUB] ?? [];
35 6
            case Link::LINK_EXTERNAL:
36
                return $this->linksPerType[Link::LINK_EXTERNAL] ?? [];
37 9
            default:
38
                return $this->links;
39
        }
40
    }
41
42
    /**
43
     * Return duplicate links
44
     * /test and /test#2 are not duplicates.
45 3
     */
46
    public function getNbrDuplicateLinks(): int
47 3
    {
48 3
        $links = $this->getLinks();
49 3
        $u = [];
50 3
        foreach ($links as $link) {
51
            $u[(string) $link->getUrl()] = 1;
52
        }
53 3
54
        return \count($links) - \count($u);
55
    }
56 6
57
    public function classifyLinks()
58 6
    {
59
        $links = $this->getLinks();
60 6
61 6
        foreach ($links as $link) {
62
            $this->linksPerType[$link->getType()][] = $link;
63 6
        }
64
    }
65
}
66