Passed
Push — master ( 91cbe9...a8de00 )
by Dev
10:22
created

Url   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 73.08%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 20
c 1
b 0
f 0
dl 0
loc 68
ccs 19
cts 26
cp 0.7308
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getOrigin() 0 5 1
A getHost() 0 3 1
A getRegistrableDomain() 0 4 1
A __toString() 0 3 1
A resolve() 0 5 1
A getScheme() 0 3 1
A getDocumentUrl() 0 3 1
A get() 0 3 1
A getHttp() 0 3 1
A relativize() 0 3 1
1
<?php
2
3
/**
4
 * Wrapper for League\Uri.
5
 *
6
 * Permits to cache registrableDomain and Origin
7
 */
8
9
namespace PiedWeb\UrlHarvester;
10
11
use League\Uri\Http;
12
use League\Uri\UriInfo;
13
use League\Uri\UriResolver;
14
15
class Url
16
{
17
    protected $http;
18
    protected $origin;
19
    protected $registrableDomain;
20
21 42
    public function __construct(string $url)
22
    {
23 42
        $this->http = Http::createFromString($url);
24
25 42
        if (!UriInfo::isAbsolute($this->http)) {
26
            throw new \Exception('$url must be absolute (`'.$url.'`)');
27
        }
28 42
    }
29
30 21
    public function resolve($url): string
31
    {
32 21
        $resolved = UriResolver::resolve(Http::createFromString($url), $this->http);
33
34 21
        return $resolved->__toString();
35
    }
36
37
    public function getHttp()
38
    {
39
        return $this->http;
40
    }
41
42
    public function getScheme()
43
    {
44
        return $this->http->getScheme();
45
    }
46
47
    public function getHost()
48
    {
49
        return $this->http->getHost();
50
    }
51
52 18
    public function getOrigin()
53
    {
54 18
        $this->origin = $this->origin ?? $this->origin = UriInfo::getOrigin($this->http);
55
56 18
        return $this->origin;
57
    }
58
59 12
    public function getRegistrableDomain()
60
    {
61 12
        return $this->registrableDomain
62 12
            ?? ($this->registrableDomain = Domain::getRegistrableDomain($this->http->getHost()));
63
    }
64
65 9
    public function getDocumentUrl(): string
66
    {
67 9
        return $this->http->withFragment('');
68
    }
69
70 9
    public function get()
71
    {
72 9
        return $this->__toString();
73
    }
74
75 12
    public function __toString()
76
    {
77 12
        return (string) $this->http;
78
    }
79
80
    public function relativize()
81
    {
82
        return substr($this->get(), strlen($this->getOrigin()));
83
    }
84
}
85