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