Completed
Push — master ( 74371a...6da8c0 )
by Vladimir
02:21
created

BaseUrlFunction::getBaseUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 3.0261
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig\Extension;
9
10
use allejo\stakx\Service;
11
use Twig_Environment;
12
13
class BaseUrlFunction extends AbstractTwigExtension implements TwigFunctionInterface
14
{
15
    private $site;
16
17 18
    public function __invoke(Twig_Environment $env, $assetPath, $absolute = false)
18
    {
19 18
        $globals = $env->getGlobals();
20 18
        $this->site = $globals['site'];
21
22 18
        $url = $this->getUrl($absolute);
23 18
        $baseURL = $this->getBaseUrl();
24 18
        $permalink = $this->guessAssetPath($assetPath);
25
26 18
        return $this->buildPermalink($url, $baseURL, $permalink);
27
    }
28
29 18
    public static function get()
30
    {
31 18
        return new \Twig_SimpleFunction('url', new self(), array(
32 18
            'needs_environment' => true,
33
        ));
34
    }
35
36 18
    private function getUrl($absolute)
37
    {
38 18
        $url = '/';
39
40 18
        if (!$absolute)
41
        {
42 16
            return $url;
43
        }
44
45 2
        if (isset($this->site['url']))
46
        {
47 2
            $url = $this->site['url'];
48
        }
49
50 2
        return ltrim($url, '/');
51
    }
52
53 18
    private function getBaseUrl()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
54
    {
55 18
        $base = '';
56
57 18
        if (isset($this->site['baseurl']))
58
        {
59 15
            $base = $this->site['baseurl'];
60
        }
61 3
        elseif (isset($this->site['base']))
62
        {
63
            $base = $this->site['base'];
64
        }
65
66 18
        return $base;
67
    }
68
69 18
    private function guessAssetPath($assetPath)
70
    {
71 18
        if (is_array($assetPath) || ($assetPath instanceof \ArrayAccess))
72
        {
73 4
            return (isset($assetPath['permalink'])) ? $assetPath['permalink'] : '/';
74
        }
75 14
        elseif (is_null($assetPath))
76
        {
77 1
            return '/';
78
        }
79 13
        elseif ($assetPath instanceof \SplFileInfo)
80
        {
81
            return str_replace(Service::getWorkingDirectory(), '', $assetPath);
82
        }
83
84 13
        return $assetPath;
85
    }
86
87
    /**
88
     * @link   https://stackoverflow.com/a/15575293
89
     * @return string
90
     */
91 18 View Code Duplication
    private function buildPermalink()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93 18
        $paths = array();
94
95 18
        foreach (func_get_args() as $arg)
96
        {
97 18
            if ($arg !== '')
98
            {
99 18
                $paths[] = $arg;
100
            }
101
        }
102
103 18
        return preg_replace('#(?<!:)/+#','/', join('/', $paths));
104
    }
105
}
106