UrlBuilder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 46
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getUrl() 0 13 3
A getCleanPath() 0 8 2
1
<?php
2
3
namespace Njasm\Soundcloud\UrlBuilder;
4
5
use Njasm\Soundcloud\Resource\ResourceInterface;
6
7
/**
8
 * SoundCloud API wrapper in PHP
9
 *
10
 * @author      Nelson J Morais <[email protected]>
11
 * @copyright   2014 Nelson J Morais <[email protected]>
12
 * @license     http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link        http://github.com/njasm/soundcloud
14
 * @package     Njasm\Soundcloud
15
 */
16
17
class UrlBuilder implements UrlBuilderInterface
18
{
19
    private $scheme;
20
    private $hostname;
21
    private $subdomain;
22
    private $resource;
23
24 17
    public function __construct(
25
        ResourceInterface $resource,
26
        $subdomain = "api",
27
        $hostname = "soundcloud.com",
28
        $scheme = "https://"
29
    ) {
30 17
        $this->resource = $resource;
31 17
        $this->scheme = $scheme;
32 17
        $this->subdomain = $subdomain;
33 17
        $this->hostname = $hostname;
34 17
    }
35
    
36 12
    public function getUrl()
37
    {
38 12
        $url = $this->scheme . $this->subdomain . "." . $this->hostname;
39 12
        $url .= $this->getCleanPath($this->resource->getPath());
40 12
        $verb = strtoupper($this->resource->getVerb());
41 12
        $params = $this->resource->getParams();
42
        
43 12
        if ($verb === 'GET' && empty($params) !== true) {
44 4
            $url .= '?' . http_build_query($params);
45
        }
46
47 12
        return $url;
48
    }
49
    
50
    /**
51
     * @param string $path
52
     * @return string
53
     */
54 12
    private function getCleanPath($path)
55
    {
56 12
        if (substr($path, strlen($path) - 1) === "/") {
57 1
            $path = substr($path, 0, strlen($path) - 1);
58
        }
59
        
60 12
        return $path;
61
    }
62
}
63