Passed
Push — master ( f60a2c...2d7c5c )
by Dev
10:34 queued 19s
created

HarvestLinksTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 70
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLinkedRessources() 0 3 1
A getDomainAndScheme() 0 8 2
A classifyLinks() 0 17 5
A getLinks() 0 18 6
1
<?php
2
3
namespace PiedWeb\UrlHarvester;
4
5
trait HarvestLinksTrait
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $links;
11
    protected $selfs = [];
12
    protected $internals = [];
13
    protected $subs = [];
14
    protected $externals = [];
15
16
    private $domainWithScheme;
17
18
    abstract public function getDom();
19
20
    abstract public function getDomain();
21
22
    public function getLinkedRessources()
23
    {
24
        return ExtractLinks::get($this->getDom(), $this->response->getEffectiveUrl(), ExtractLinks::SELECT_ALL);
25
    }
26
27 3
    public function getLinks($type = null)
28
    {
29 3
        if (null === $this->links) {
30 3
            $this->links = ExtractLinks::get($this->getDom(), $this->response->getEffectiveUrl());
31 3
            $this->classifyLinks();
32
        }
33
34 3
        switch ($type) {
35 3
            case 'self':
36 3
                return $this->selfs;
37 3
            case 'internal':
38 3
                return $this->internals;
39 3
            case 'sub':
40 3
                return $this->subs;
41 3
            case 'external':
42 3
                return $this->externals;
43
            default:
44 3
                return $this->links;
45
        }
46
    }
47
48 3
    public function getDomainAndScheme()
49
    {
50 3
        if (null === $this->domainWithScheme) {
51 3
            $url = parse_url($this->response->getEffectiveUrl());
52 3
            $this->domainWithScheme = $url['scheme'].'://'.$url['host'];
53
        }
54
55 3
        return $this->domainWithScheme;
56
    }
57
58 3
    public function classifyLinks()
59
    {
60 3
        $links = $this->getLinks();
61
62 3
        foreach ($links as $link) {
63 3
            $urlParsed = parse_url($link->getUrl());
64
65 3
            if (preg_match('/^'.preg_quote($this->getDomainAndScheme().'/', '/').'/si', $link->getUrl().'/')) {
66 3
                if (preg_replace('/(\#.*)/si', '', $link->getUrl()) == $this->response->getEffectiveUrl()) {
67 3
                    $this->selfs[] = $link;
68
                } else {
69 3
                    $this->internals[] = $link;
70
                }
71 3
            } elseif (preg_match('/'.preg_quote($this->getDomain(), '/').'$/si', $urlParsed['host'])) {
72 3
                $this->subs[] = $link;
73
            } else {
74 3
                $this->externals[] = $link;
75
            }
76
        }
77 3
    }
78
}
79