Completed
Push — master ( 40c158...5cd494 )
by Alex
02:32
created

Psr7UriExtension::generateRelativePath()   D

Complexity

Conditions 10
Paths 11

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10
Metric Value
dl 0
loc 36
ccs 22
cts 22
cp 1
rs 4.8196
cc 10
eloc 20
nc 11
nop 1
crap 10

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