Completed
Push — master ( e4bfce...b9fe45 )
by Vladimir
02:19
created

BaseUrlFunction::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Twig;
4
5
use Twig_Environment;
6
7
class BaseUrlFunction
8
{
9 13
    public function __invoke (Twig_Environment $env, $assetPath)
10
    {
11 13
        $globals = $env->getGlobals();
12
13 13
        if (is_array($assetPath) || ($assetPath instanceof \ArrayAccess))
14 13
        {
15 4
            $assetPath = (isset($assetPath['permalink'])) ? $assetPath['permalink'] : '/';
16 4
        }
17 9
        else if (is_null($assetPath))
18 9
        {
19 1
            $assetPath = '/';
20 1
        }
21
22
        // @TODO 1.0.0 Remove support for 'base' as it's been deprecated
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
23 13
        $base = (array_key_exists('base', $globals['site'])) ? $globals['site']['base'] : $globals['site']['baseurl'];
24
25 13
        $baseURL = (empty($base)) ? '/' : '/' . trim($base, '/') . '/';
26 13
        $url     = ltrim($assetPath, '/');
27
28
        // Sanity check, remove any excess trailing '/'
29 13
        if (!empty($url) && $url[strlen($url) - 1] == '/')
30 13
        {
31 4
            $url = rtrim($url, '/') . '/';
32 4
        }
33
34 13
        return ($baseURL . $url);
35
    }
36
37 1
    public static function get ()
38
    {
39 1
        return new \Twig_SimpleFunction('url', new self(), array(
40
            'needs_environment' => true
41 1
        ));
42
    }
43
}