CUrl::createRelative()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 16
Ratio 72.73 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 22
rs 8.6737
ccs 0
cts 9
cp 0
cc 5
eloc 9
nc 4
nop 1
crap 30
1
<?php
2
3
namespace Anax\Url;
4
5
/**
6
 * A helper to create urls.
7
 *
8
 */
9
class CUrl
10
{
11
12
    /**
13
     * Properties
14
     *
15
     */
16
    const URL_CLEAN  = 'clean';  // controller/action/param1/param2
17
    const URL_APPEND = 'append'; // index.php/controller/action/param1/param2
18
19
    private $urlType = self::URL_APPEND; // What type of urls to generate
20
21
    private $siteUrl = null; // Siteurl to prepend to all absolute urls created
22
    private $baseUrl = null; // Baseurl to prepend to all relative urls created
23
    private $scriptName = null; // Name of the frontcontroller script
24
25
26
    private $staticSiteUrl = null; // Siteurl to prepend to all absolute urls for assets
27
    private $staticBaseUrl = null; // Baseurl to prepend to all relative urls for assets
28
29
30
31
    /**
32
     * Create an url and prepending the baseUrl.
33
     *
34
     * @param string $uri part of uri to use when creating an url. "" or null means baseurl to
35
     * current frontcontroller.
36
     *
37
     * @return string as resulting url.
38
     */
39 17
    public function create($uri = null)
40
    {
41 17
        if (empty($uri)) {
42
43
            // Empty uri means baseurl
44 2
            return $this->baseUrl
45 2
                . (($this->urlType == self::URL_APPEND)
46 2
                    ? "/$this->scriptName"
47 2
                    : null);
48
49 15
        } elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") {
50
51
            // Fully qualified, just leave as is.
52 3
            return rtrim($uri, '/');
53
54 12
        } elseif ($uri[0] == '/') {
55
56
            // Absolute url, prepend with siteUrl
57 4
            return rtrim($this->siteUrl . rtrim($uri, '/'), '/');
58
59
        }
60
61 8
        $uri = rtrim($uri, '/');
62 8
        if ($this->urlType == self::URL_CLEAN) {
63 4
            return $this->baseUrl . '/' . $uri;
64
        } else {
65 4
            return $this->baseUrl . '/' . $this->scriptName . '/' . $uri;
66
        }
67
    }
68
69
70
71
    /**
72
     * Create an url and prepend the baseUrl to the directory of the frontcontroller.
73
     *
74
     * @param string $uri part of uri to use when creating an url. "" or null means baseurl to
75
     * directory of the current frontcontroller.
76
     *
77
     * @return string as resulting url.
78
     */
79
    public function createRelative($uri = null)
80
    {
81 View Code Duplication
        if (empty($uri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
83
            // Empty uri means baseurl
84
            return $this->baseUrl;
85
86
        } elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") {
87
88
            // Fully qualified, just leave as is.
89
            return rtrim($uri, '/');
90
91
        } elseif ($uri[0] == '/') {
92
93
            // Absolute url, prepend with siteUrl
94
            return rtrim($this->siteUrl . rtrim($uri, '/'), '/');
95
96
        }
97
98
        $uri = rtrim($uri, '/');
99
        return $this->baseUrl . '/' . $uri;
100
    }
101
102
103
104
    /**
105
     * Create an url for a static asset.
106
     *
107
     * @param string $uri part of uri to use when creating an url.
108
     *
109
     * @return string as resulting url.
110
     */
111 8
    public function asset($uri = null)
112
    {
113 8 View Code Duplication
        if (empty($uri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
115
            // Allow empty
116
117 8
        } elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") {
118
119
            // Fully qualified, just leave as is.
120 4
            return rtrim($uri, '/');
121
122 4
        } elseif ($uri[0] == '/') {
123
124
            // Absolute url, prepend with staticSiteUrl
125 2
            return rtrim($this->staticSiteUrl . rtrim($uri, '/'), '/');
126
127
        }
128
129 2
        $baseUrl = isset($this->staticBaseUrl) ? $this->staticBaseUrl : $this->baseUrl;
130
131 2
        return empty($uri)
132 2
            ? $baseUrl
133 2
            : $baseUrl . '/' . $uri;
134
    }
135
136
137
138
    /**
139
     * Set the siteUrl to prepend all absolute urls created.
140
     *
141
     * @param string $url part of url to use when creating an url.
142
     *
143
     * @return $this
144
     */
145 17
    public function setSiteUrl($url)
146
    {
147 17
        $this->siteUrl = rtrim($url, '/');
148 17
        return $this;
149
    }
150
151
152
153
    /**
154
     * Set the baseUrl to prepend all relative urls created.
155
     *
156
     * @param string $url part of url to use when creating an url.
157
     *
158
     * @return $this
159
     */
160 18
    public function setBaseUrl($url)
161
    {
162 18
        $this->baseUrl = rtrim($url, '/');
163 18
        return $this;
164
    }
165
166
167
168
    /**
169
     * Set the siteUrl to prepend absolute urls for assets.
170
     *
171
     * @param string $url part of url to use when creating an url.
172
     *
173
     * @return $this
174
     */
175 4
    public function setStaticSiteUrl($url)
176
    {
177 4
        $this->staticSiteUrl = rtrim($url, '/');
178 4
        return $this;
179
    }
180
181
182
183
    /**
184
     * Set the baseUrl to prepend relative urls for assets.
185
     *
186
     * @param string $url part of url to use when creating an url.
187
     *
188
     * @return $this
189
     */
190 4
    public function setStaticBaseUrl($url)
191
    {
192 4
        $this->staticBaseUrl = rtrim($url, '/');
193 4
        return $this;
194
    }
195
196
197
198
    /**
199
     * Set the scriptname to use when creating URL_APPEND urls.
200
     *
201
     * @param string $name as the scriptname, for example index.php.
202
     *
203
     * @return $this
204
     */
205 7
    public function setScriptName($name)
206
    {
207 7
        $this->scriptName = $name;
208 7
        return $this;
209
    }
210
211
212
    /**
213
     * Set the type of urls to be generated, URL_CLEAN, URL_APPEND.
214
     *
215
     * @param string $type what type of urls to create.
216
     *
217
     * @return $this
218
     * @throws \Exception
219
     */
220 15
    public function setUrlType($type)
221
    {
222 15
        if (!in_array($type, [self::URL_APPEND, self::URL_CLEAN])) {
223 1
            throw new \Exception("Unsupported Url type.");
224
        }
225
226 14
        $this->urlType = $type;
227 14
        return $this;
228
    }
229
}
230