Completed
Push — master ( 0c3733...9b1319 )
by Vladimir
02:41
created

BaseUrlFunction::__invoke()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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