Completed
Pull Request — master (#124)
by ignace nyamagana
03:52
created

UrlBuilder::buildUrl()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 2
Bugs 2 Features 1
Metric Value
c 2
b 2
f 1
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 8.5126
cc 5
eloc 13
nc 10
nop 2
crap 5
1
<?php
2
3
namespace League\Glide\Urls;
4
5
use InvalidArgumentException;
6
use League\Glide\Signatures\SignatureInterface;
7
use League\Uri\Schemes\Http as HttpUri;
8
use League\Uri\Modifiers\AppendSegment;
9
use League\Uri\Modifiers\AddLeadingSlash;
10
11
class UrlBuilder
12
{
13
    /**
14
     * Whether the base URL is a relative domain.
15
     * @var Resolve
16
     */
17
    protected $resolve;
18
19
    /**
20
     * The HTTP signature used to sign URLs.
21
     * @var SignatureInterface
22
     */
23
    protected $signature;
24
25
    /**
26
     * Create UrlBuilder instance.
27
     * @param string                  $baseUrl   The base URL.
28
     * @param SignatureInterface|null $signature The HTTP signature used to sign URLs.
29
     */
30 30
    public function __construct($baseUrl = '', SignatureInterface $signature = null)
31
    {
32 30
        $this->setBaseUrl($baseUrl);
33 27
        $this->setSignature($signature);
34 27
    }
35
36
    /**
37
     * Set the base URL.
38
     * @param string $baseUrl The base URL.
39
     */
40 30
    public function setBaseUrl($baseUrl)
41
    {
42 30
        static $addLeadingSlash;
43 30
        if (!$addLeadingSlash) {
44 3
            $addLeadingSlash = new AddLeadingSlash();
45 2
        }
46
47 30
        $this->baseUrl = $addLeadingSlash->__invoke(HttpUri::createFromString($baseUrl));
0 ignored issues
show
Bug introduced by
The property baseUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48 27
    }
49
50
    /**
51
     * Set the HTTP signature.
52
     * @param SignatureInterface|null $signature The HTTP signature used to sign URLs.
53
     */
54 27
    public function setSignature(SignatureInterface $signature = null)
55
    {
56 27
        $this->signature = $signature;
57 27
    }
58
59
    /**
60
     * Get the URL.
61
     * @param  string $path   The resource path.
62
     * @param  array  $params The manipulation parameters.
63
     * @return string The URL.
64
     */
65 24
    public function getUrl($path, array $params = [])
66
    {
67 24
        $uri = (new AppendSegment($path))->__invoke($this->baseUrl);
68 24
        if ($this->signature) {
69 9
            $params = $this->signature->addSignature($uri->getPath(), $params);
70 6
        }
71
72 24
        return (string) $uri->withQuery(http_build_query($params, '', '&', PHP_QUERY_RFC3986));
73
    }
74
}
75