Completed
Push — master ( 3657ae...e63a18 )
by Vladimir
02:34
created

BaseUrlFunction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 16.66%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 32
ccs 3
cts 18
cp 0.1666
rs 10
c 1
b 0
f 0
wmc 7
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 22 6
A get() 0 6 1
1
<?php
2
3
namespace allejo\stakx\Twig;
4
5
use allejo\stakx\Object\JailObject;
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 JailObject) {
15
            $assetPath = $assetPath->getPermalink();
0 ignored issues
show
Documentation Bug introduced by
The method getPermalink does not exist on object<allejo\stakx\Object\JailObject>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
16
        }
17
        else if (is_array($assetPath)) {
18
            $assetPath = $assetPath['permalink'];
19
        }
20
        else if (is_null($assetPath)) {
21
            $assetPath = '/';
22
        }
23
24
        // @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...
25
        $base = (array_key_exists('base', $globals['site'])) ? $globals['site']['base'] : $globals['site']['baseurl'];
26
27
        $baseURL = (empty($base)) ? '/' : '/' . trim($base, '/') . '/';
28
        $url     = ltrim($assetPath, '/');
29
30
        return ($baseURL . $url);
31
    }
32
33 1
    public static function get ()
34
    {
35 1
        return new \Twig_SimpleFunction('url', new self(), array(
36
            'needs_environment' => true
37 1
        ));
38
    }
39
}