Completed
Push — master ( c8ace5...4714ba )
by Alex
03:55 queued 01:45
created

RequestExtension::generateRelativeUrl()   D

Complexity

Conditions 10
Paths 11

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 40
rs 4.8196
cc 10
eloc 22
nc 11
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Asmaster\EquipTwig\Extension;
4
5
class RequestExtension extends \Twig_Extension
6
{
7
    use ServerRequestTrait;
8
9
    /**
10
     * @return string The extension name
11
     */
12
    public function getName()
13
    {
14
        return 'request';
15
    }
16
17
    public function getFunctions()
18
    {
19
        return [
20
            new \Twig_SimpleFunction('absolute_url', [$this, 'generateAbsoluteUrl']),
21
            new \Twig_SimpleFunction('relative_url', [$this, 'generateRelativeUrl'])
22
        ];
23
    }
24
25
    /**
26
     * @param string $path
27
     * @return string
28
     */
29
    public function generateAbsoluteUrl($path)
30
    {
31
        if ($this->isNetworkPath($path)) {
32
            return $path;
33
        }
34
35
        $uri = $this->request->getUri();
36
37
        $host = $uri->getHost();
38
        if ('' === $host) {
39
            return $path;
40
        }
41
42
        if (null !== $uri->getPort()) {
43
            $host .= ':' . $uri->getPort();
44
        }
45
46
        if (! $this->hasLeadingSlash($path)) {
47
            $path = rtrim($uri->getPath(), '/') . '/' . $path;
48
        }
49
50
        return $uri->getScheme() . '://' . $host.$path;
51
    }
52
53
    /**
54
     * @param string $path
55
     * @return string
56
     */
57
    public function generateRelativeUrl($path)
58
    {
59
        if ($this->isNetworkPath($path)
60
            || ! $this->hasLeadingSlash($path)
61
        ) {
62
            return $path;
63
        }
64
65
        $uri = $this->request->getUri();
66
67
        $basePath = $uri->getPath();
68
        if ($path === $basePath) {
69
            return '';
70
        }
71
72
        $baseParts = explode('/', $basePath, -1);
73
        $pathParts = explode('/', $path);
74
75
        foreach ($baseParts as $i => $segment) {
76
            if (isset($pathParts[$i]) && $segment === $pathParts[$i]) {
77
                unset($baseParts[$i], $pathParts[$i]);
78
            } else {
79
                break;
80
            }
81
        }
82
83
        $path = str_repeat('../', count($baseParts)) . implode('/', $pathParts);
84
85
        if (empty($path)) {
86
            return './';
87
        }
88
89
        if (empty($baseParts)
90
            && false !== strpos(current($pathParts), ':')
91
        ) {
92
            $path = './' . $path;
93
        }
94
95
        return $path;
96
    }
97
98
    /**
99
     * @param string $path
100
     * @return bool
101
     */
102
    private function isNetworkPath($path)
103
    {
104
        return false !== strpos($path, '://')
105
            || '//' === substr($path, 0, 2);
106
    }
107
108
    /**
109
     * @param string $path
110
     * @return bool
111
     */
112
    private function hasLeadingSlash($path)
113
    {
114
        return isset($path[0]) && '/' === $path[0];
115
    }
116
}
117