Completed
Pull Request — master (#31)
by Vladimir
02:41
created

BaseUrlFunction::__invoke()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
nc 12
nop 2
dl 0
loc 19
ccs 0
cts 10
cp 0
crap 42
rs 8.8571
c 3
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Twig;
4
5
use Twig_Environment;
6
7
class BaseUrlFunction
8
{
9
    public function __invoke (Twig_Environment $env, $assetPath)
10
    {
11
        $globals = $env->getGlobals();
12
13
        if (is_array($assetPath) || ($assetPath instanceof \ArrayAccess)) {
14
            $assetPath = $assetPath['permalink'];
15
        }
16
        else if (is_null($assetPath)) {
17
            $assetPath = '/';
18
        }
19
20
        // @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...
21
        $base = (array_key_exists('base', $globals['site'])) ? $globals['site']['base'] : $globals['site']['baseurl'];
22
23
        $baseURL = (empty($base)) ? '/' : '/' . trim($base, '/') . '/';
24
        $url     = ltrim($assetPath, '/');
25
26
        return ($baseURL . $url);
27
    }
28 1
29
    public static function get ()
30 1
    {
31
        return new \Twig_SimpleFunction('url', new self(), array(
32 1
            'needs_environment' => true
33
        ));
34
    }
35
}