Completed
Push — master ( 4bb447...b82aa8 )
by Vladimir
9s
created

BaseUrlFunction   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 14.14 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.5%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 14
loc 99
ccs 37
cts 40
cp 0.925
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 1
A get() 0 6 1
A getUrl() 0 16 3
A getBaseUrl() 0 15 3
B guessAssetPath() 0 22 9
A buildPermalink() 14 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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