Completed
Push — master ( e60207...37fe77 )
by Jonathan
05:38
created

UrlBuilder::setBaseUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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