UrlBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 108
ccs 0
cts 48
cp 0
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
A 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
    public function __construct($baseUrl = '', SignatureInterface $signature = null)
34
    {
35
        $this->setBaseUrl($baseUrl);
36
        $this->setSignature($signature);
37
    }
38
39
    /**
40
     * Set the base URL.
41
     * @param string $baseUrl The base URL.
42
     */
43
    public function setBaseUrl($baseUrl)
44
    {
45
        if (substr($baseUrl, 0, 2) === '//') {
46
            $baseUrl = 'http:'.$baseUrl;
47
            $this->isRelativeDomain = true;
48
        }
49
50
        $this->baseUrl = rtrim($baseUrl, '/');
51
    }
52
53
    /**
54
     * Set the HTTP signature.
55
     * @param SignatureInterface|null $signature The HTTP signature used to sign URLs.
56
     */
57
    public function setSignature(SignatureInterface $signature = null)
58
    {
59
        $this->signature = $signature;
60
    }
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
    public function getUrl($path, array $params = [])
69
    {
70
        $parts = parse_url($this->baseUrl.'/'.trim($path, '/'));
71
72
        if ($parts === false) {
73
            throw new InvalidArgumentException('Not a valid path.');
74
        }
75
76
        $parts['path'] = '/'.trim($parts['path'], '/');
77
78
        if ($this->signature) {
79
            $params = $this->signature->addSignature($parts['path'], $params);
80
        }
81
82
        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
    protected function buildUrl($parts, $params)
92
    {
93
        $url = '';
94
95
        if (isset($parts['host'])) {
96
            if ($this->isRelativeDomain) {
97
                $url .= '//'.$parts['host'];
98
            } else {
99
                $url .= $parts['scheme'].'://'.$parts['host'];
100
            }
101
102
            if (isset($parts['port'])) {
103
                $url .= ':'.$parts['port'];
104
            }
105
        }
106
107
        $url .= $parts['path'];
108
109
        if (count($params)) {
110
            $url .= '?'.http_build_query($params);
111
        }
112
113
        return $url;
114
    }
115
}
116