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

BaseUrlFunction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 37
ccs 21
cts 21
cp 1
rs 10
wmc 10
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
D __invoke() 0 27 9
A get() 0 6 1
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
}