UrlBuilder   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 1

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 108
ccs 39
cts 48
cp 0.8125
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setBaseUrl() 0 9 2
A setSignature() 0 4 1
A getUrl() 0 16 3
B buildUrl() 0 24 5
1
<?php
2
3
namespace League\Glide\Urls;
4
5
use InvalidArgumentException;
6
use League\Glide\Signatures\SignatureInterface;
7
8
class UrlBuilder
9
{
10
    /**
11
     * The base URL.
12
     * @var string
13
     */
14
    protected $baseUrl;
15
16
    /**
17
     * Whether the base URL is a relative domain.
18
     * @var bool
19
     */
20
    protected $isRelativeDomain = false;
21
22
    /**
23
     * The HTTP signature used to sign URLs.
24
     * @var SignatureInterface
25
     */
26
    protected $signature;
27
28
    /**
29
     * Create UrlBuilder instance.
30
     * @param string                  $baseUrl   The base URL.
31
     * @param SignatureInterface|null $signature The HTTP signature used to sign URLs.
32
     */
33 20
    public function __construct($baseUrl = '', SignatureInterface $signature = null)
34
    {
35 20
        $this->setBaseUrl($baseUrl);
36 20
        $this->setSignature($signature);
37 20
    }
38
39
    /**
40
     * Set the base URL.
41
     * @param string $baseUrl The base URL.
42
     */
43 20
    public function setBaseUrl($baseUrl)
44
    {
45 20
        if (substr($baseUrl, 0, 2) === '//') {
46 2
            $baseUrl = 'http:'.$baseUrl;
47 2
            $this->isRelativeDomain = true;
48 2
        }
49
50 20
        $this->baseUrl = rtrim($baseUrl, '/');
51 20
    }
52
53
    /**
54
     * Set the HTTP signature.
55
     * @param SignatureInterface|null $signature The HTTP signature used to sign URLs.
56
     */
57 20
    public function setSignature(SignatureInterface $signature = null)
58
    {
59 20
        $this->signature = $signature;
60 20
    }
61
62
    /**
63
     * Get the URL.
64
     * @param  string $path   The resource path.
65
     * @param  array  $params The manipulation parameters.
66
     * @return string The URL.
67
     */
68 18
    public function getUrl($path, array $params = [])
69
    {
70 18
        $parts = parse_url($this->baseUrl.'/'.trim($path, '/'));
71
72 18
        if ($parts === false) {
73 2
            throw new InvalidArgumentException('Not a valid path.');
74
        }
75
76 16
        $parts['path'] = '/'.trim($parts['path'], '/');
77
78 16
        if ($this->signature) {
79 6
            $params = $this->signature->addSignature($parts['path'], $params);
80 6
        }
81
82 16
        return $this->buildUrl($parts, $params);
83
    }
84
85
    /**
86
     * Build the URL.
87
     * @param  array  $parts  The URL parts.
88
     * @param  array  $params The manipulation parameters.
89
     * @return string The built URL.
90
     */
91 16
    protected function buildUrl($parts, $params)
92
    {
93 16
        $url = '';
94
95 16
        if (isset($parts['host'])) {
96 8
            if ($this->isRelativeDomain) {
97 2
                $url .= '//'.$parts['host'];
98 2
            } else {
99 6
                $url .= $parts['scheme'].'://'.$parts['host'];
100
            }
101
102 8
            if (isset($parts['port'])) {
103 4
                $url .= ':'.$parts['port'];
104 4
            }
105 8
        }
106
107 16
        $url .= $parts['path'];
108
109 16
        if (count($params)) {
110 14
            $url .= '?'.http_build_query($params);
111 14
        }
112
113 16
        return $url;
114
    }
115
}
116