Completed
Push — master ( 4dd42e...7a1ec5 )
by Dev
12:19 queued 11:13
created

Link::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace PiedWeb\UrlHarvester;
4
5
use simple_html_dom_node;
6
7
class Link
8
{
9
    private $url;
10
    private $anchor;
11
    private $element;
12
13 12
    public function __construct(string $url, ?simple_html_dom_node $element = null)
14
    {
15 12
        $this->url = trim($url);
16 12
        if (null !== $element) {
17 12
            $this->setAnchor($element);
18
        }
19 12
        $this->element = $element;
20 12
    }
21
22 12
    protected function setAnchor(simple_html_dom_node $element)
23
    {
24 12
        $this->anchor = substr(Helper::clean($element->plaintext), 0, 100);
25
26 12
        if (empty($this->anchor) && $element->find('*[alt]', 0)) {
27
            $this->anchor = substr(Helper::clean($element->find('*[alt]', 0)->alt), 0, 100);
28
        }
29 12
    }
30
31 9
    public function getUrl()
32
    {
33 9
        return $this->url;
34
    }
35
36 3
    public function getPageUrl()
37
    {
38 3
        return preg_replace('/(\#.*)/si', '', $this->url);
39
    }
40
41 3
    public function getAnchor()
42
    {
43 3
        return $this->anchor;
44
    }
45
46 3
    public function getElement()
47
    {
48 3
        return $this->element;
49
    }
50
51 6
    public function mayFollow()
52
    {
53 6
        if (isset($this->element) && isset($this->element->rel)) {
54 3
            if (false !== strpos($this->element->rel, 'nofollow')) {
55 3
                return false;
56
            }
57
        }
58
59 3
        return true;
60
    }
61
}
62