Completed
Push — master ( 2d7c5c...16dc59 )
by Dev
09:53
created

HarvestLinksTrait::getType()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\UrlHarvester;
4
5
trait HarvestLinksTrait
6
{
7
8
    /**
9
     * @var array
10
     */
11
    protected $links;
12
    protected $linksPerType;
13
14
    abstract public function getDom();
15
16
    abstract public function getDomain();
17
18
    public function getLinkedRessources()
19
    {
20
        return ExtractLinks::get($this->getDom(), $this->response->getEffectiveUrl(), ExtractLinks::SELECT_ALL);
21
    }
22
23 3
    public function getLinks($type = null)
24
    {
25 3
        if (null === $this->links) {
26 3
            $this->links = ExtractLinks::get($this->getDom(), $this->response->getEffectiveUrl());
27 3
            $this->classifyLinks();
28
        }
29
30
        switch ($type) {
31 3
            case self::LINK_SELF:
0 ignored issues
show
Bug introduced by
The constant PiedWeb\UrlHarvester\HarvestLinksTrait::LINK_SELF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32
                return $this->linksPerType[self::LINK_SELF];
33 3
            case self::LINK_INTERNAL:
0 ignored issues
show
Bug introduced by
The constant PiedWeb\UrlHarvester\Har...nksTrait::LINK_INTERNAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
34
                return $this->linksPerType[self::LINK_INTERNAL];
35 3
            case self::LINK_SUB:
0 ignored issues
show
Bug introduced by
The constant PiedWeb\UrlHarvester\HarvestLinksTrait::LINK_SUB was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
                return $this->linksPerType[self::LINK_SUB];
37 3
            case self::LINK_EXTERNAL:
0 ignored issues
show
Bug introduced by
The constant PiedWeb\UrlHarvester\Har...nksTrait::LINK_EXTERNAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
                return $this->linksPerType[self::LINK_EXTERNAL];
39
            default:
40 3
                return $this->links;
41
        }
42
    }
43
44
    public function getNbrDuplicateLinks()
45
    {
46
        $links = $this->getLinks();
47
        $u = [];
48
        foreach ($links as $link) {
49
            $u[$link->getUrl()] = 1;
50
        }
51
52
        return count($links) - count($u);
53
    }
54
55
    abstract function getDomainAndScheme();
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...
56
57 3
    public function classifyLinks()
58
    {
59 3
        $links = $this->getLinks();
60
61 3
        foreach ($links as $link) {
62 3
            $type = $this->getType($link->getPageUrl());
63 3
            $this->linksPerType[$type][] = $link;
64
        }
65 3
    }
66
67 3
    public function isInternalType(string $url)
68
    {
69 3
        return strpos($url, $this->getDomainAndScheme()) === 0;
70
    }
71
72 3
    public function isSubType(string $host)
73
    {
74 3
        return strtolower(substr($host, -strlen($this->getDomain()))) === $this->getDomain();
75
    }
76
77 3
    public function isSelfType(string $url)
78
    {
79 3
        if (strpos($url, '#') !== 0) {
80 3
            $url = substr($url, 0, -(strlen(parse_url($url, PHP_URL_FRAGMENT)) + 1));
81
        }
82
83 3
        return $this->isInternalType($url) && $url == $this->response->getEffectiveUrl();
84
    }
85
86 3
    public function getType(string $url): string
87
    {
88 3
        if ($this->isSelfType($url)) {
89
            return self::LINK_SELF;
0 ignored issues
show
Bug introduced by
The constant PiedWeb\UrlHarvester\HarvestLinksTrait::LINK_SELF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
90 3
        } elseif ($this->isInternalType($url)) {
91 3
            return self::LINK_INTERNAL;
0 ignored issues
show
Bug introduced by
The constant PiedWeb\UrlHarvester\Har...nksTrait::LINK_INTERNAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92 3
        } elseif ($this->isSubType(parse_url($url, PHP_URL_HOST))) {
93 3
            return self::LINK_SUB;
0 ignored issues
show
Bug introduced by
The constant PiedWeb\UrlHarvester\HarvestLinksTrait::LINK_SUB was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94
        }
95
96 3
        return self::LINK_EXTERNAL;
0 ignored issues
show
Bug introduced by
The constant PiedWeb\UrlHarvester\Har...nksTrait::LINK_EXTERNAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
97
    }
98
99
    public function getAbsoluteInternalLink(string $url)
100
    {
101
        return substr($url, strlen($this->getDomainAndScheme()));
102
    }
103
}
104