Passed
Push — master ( 9abeb8...36c770 )
by Dev
13:09
created

HarvestLinksTrait::getType()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8.125

Importance

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