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

BaseUrlFunction::__invoke()   D

Complexity

Conditions 9
Paths 32

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 12
c 3
b 0
f 0
nc 32
nop 2
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 9
rs 4.909
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
}