Completed
Pull Request — master (#78)
by Vladimir
04:42 queued 01:10
created

BaseUrlFunction::guessAssetPath()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8.512

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 8
cts 10
cp 0.8
rs 8.4444
c 0
b 0
f 0
cc 8
nc 7
nop 2
crap 8.512
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig\Extension;
9
10
use allejo\stakx\Document\RepeaterPageView;
11
use allejo\stakx\Service;
12
use Twig_Environment;
13
14
class BaseUrlFunction extends AbstractTwigExtension implements TwigFunctionInterface
15
{
16
    private $site;
17
18 18
    public function __invoke(Twig_Environment $env, $assetPath, $absolute = false, $params = [])
19
    {
20 18
        $globals = $env->getGlobals();
21 18
        $this->site = $globals['site'];
22
23 18
        $url = $this->getUrl($absolute);
24 18
        $baseURL = $this->getBaseUrl();
25 18
        $permalink = $this->guessAssetPath($assetPath, $params);
26
27 18
        return $this->buildPermalink($url, $baseURL, $permalink);
28
    }
29
30 18
    public static function get()
31
    {
32 18
        return new \Twig_SimpleFunction('url', new self(), [
33 18
            'needs_environment' => true,
34
        ]);
35
    }
36
37 18
    private function getUrl($absolute)
38
    {
39 18
        $url = '/';
40
41 18
        if (!$absolute)
42
        {
43 16
            return $url;
44
        }
45
46 2
        if (isset($this->site['url']))
47
        {
48 2
            $url = $this->site['url'];
49
        }
50
51 2
        return ltrim($url, '/');
52
    }
53
54 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...
55
    {
56 18
        $base = '';
57
58 18
        if (isset($this->site['baseurl']))
59
        {
60 15
            $base = $this->site['baseurl'];
61
        }
62 3
        elseif (isset($this->site['base']))
63
        {
64
            $base = $this->site['base'];
65
        }
66
67 18
        return $base;
68
    }
69
70 18
    private function guessAssetPath($assetPath, $params)
71
    {
72 18
        if ($assetPath instanceof RepeaterPageView)
73
        {
74
            return ($link = $assetPath->getPermalinkWhere($params)) ? $link : '/';
75
        }
76 18
        elseif (is_array($assetPath) || ($assetPath instanceof \ArrayAccess))
77
        {
78 4
            return (isset($assetPath['permalink'])) ? $assetPath['permalink'] : '/';
79
        }
80 14
        elseif (is_null($assetPath))
81
        {
82 1
            return '/';
83
        }
84 13
        elseif ($assetPath instanceof \SplFileInfo)
85
        {
86
            return str_replace(Service::getWorkingDirectory(), '', $assetPath);
87
        }
88
89 13
        return $assetPath;
90
    }
91
92
    /**
93
     * @see   https://stackoverflow.com/a/15575293
94
     *
95
     * @return string
96
     */
97 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...
98
    {
99 18
        $paths = [];
100
101 18
        foreach (func_get_args() as $arg)
102
        {
103 18
            if ($arg !== '')
104
            {
105 18
                $paths[] = $arg;
106
            }
107
        }
108
109 18
        return preg_replace('#(?<!:)/+#', '/', join('/', $paths));
110
    }
111
}
112