Url   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 71
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getBaseUrl() 0 4 1
A getUrlForPath() 0 9 3
A getFullUrl() 0 8 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http;
12
13
/**
14
 * @author      Iqbal Maulana <[email protected]>
15
 * @created     8/5/15
16
 */
17
class Url
18
{
19
    /**
20
     * @var Uri
21
     */
22
    private $uri;
23
24
    /**
25
     * @var SchemaHost
26
     */
27
    private $schemaHost;
28
29
    /**
30
     * @var string
31
     */
32
    private $queryString;
33
34
    /**
35
     * @param Uri        $uri
36
     * @param SchemaHost $schemaHost
37
     * @param string     $queryString
38
     */
39
    public function __construct(Uri $uri, SchemaHost $schemaHost, $queryString)
40
    {
41
        $this->uri = $uri;
42
        $this->schemaHost = $schemaHost;
43
        $this->queryString = $queryString;
44
    }
45
46
    /**
47
     * Get base url including schema, host and port (if non-standard).
48
     *
49
     * @return string
50
     */
51
    public function getBaseUrl()
52
    {
53
        return rtrim($this->schemaHost->getSchemaAndHttpHost() . $this->uri->getUri(), '/');
54
    }
55
56
    /**
57
     * Get url for specified path.
58
     *
59
     * @param string $path
60
     * @param bool   $withQueryString
61
     *
62
     * @return string
63
     */
64
    public function getUrlForPath($path, $withQueryString = false)
65
    {
66
        $queryString = '';
67
        if (true === $withQueryString && (null !== $queryString = $this->queryString)) {
68
            $queryString = '?' . $queryString;
69
        }
70
71
        return $this->getBaseUrl() . '/' . trim($path, '/') . $queryString;
72
    }
73
74
    /**
75
     * Get full url including query string if exist.
76
     *
77
     * @return string
78
     */
79
    public function getFullUrl()
80
    {
81
        if (null !== $queryString = $this->queryString) {
82
            $queryString = '?' . $queryString;
83
        }
84
85
        return $this->getBaseUrl() . $this->uri->getPathInfo() . $queryString;
86
    }
87
}
88