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

BaseUrlFunction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 23.08%

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 3
cts 13
cp 0.2308
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

2 Methods

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