Completed
Push — master ( 943281...5c35c6 )
by Vladimir
02:38
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
    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
29 1
    public static function get ()
30
    {
31 1
        return new \Twig_SimpleFunction('url', new self(), array(
32
            'needs_environment' => true
33 1
        ));
34
    }
35
}