Completed
Push — master ( 60fa31...4f764e )
by Mr
01:55
created

Config::resolvePath()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 7.6666
c 0
b 0
f 0
cc 10
nc 20
nop 2

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 EasyRSA;
4
5
class Config
6
{
7
    /**
8
     * Name of file (with or without full path to this file)
9
     * @var string
10
     */
11
    private $_archive = './easy-rsa.tar.gz';
12
13
    /**
14
     * Path to folder with easy-rsa scripts
15
     * @var string
16
     */
17
    private $_scripts = './easy-rsa';
18
19
    /**
20
     * Path to folder with certificates
21
     * @var string
22
     */
23
    private $_certs = '.';
24
25
    /**
26
     * Get path with certificates
27
     *
28
     * @return string
29
     */
30
    public function getCerts(): string
31
    {
32
        return $this->_certs;
33
    }
34
35
    /**
36
     * Set full path to folder with certificates
37
     *
38
     * @param   string $folder
39
     * @return  Config
40
     */
41
    public function setCerts(string $folder): self
42
    {
43
        $this->_certs = $this->resolvePath($folder);
44
        return $this;
45
    }
46
47
    /**
48
     * Get full path to folder with scripts
49
     *
50
     * @return  string
51
     */
52
    public function getScripts(): string
53
    {
54
        return $this->_scripts;
55
    }
56
57
    /**
58
     * Set path with easy-rsa scripts
59
     *
60
     * @param   string $folder
61
     * @return  Config
62
     */
63
    public function setScripts(string $folder): self
64
    {
65
        $this->_scripts = $this->resolvePath($folder);
66
        return $this;
67
    }
68
69
    /**
70
     * Get archive file with full path
71
     *
72
     * @return  string
73
     */
74
    public function getArchive(): string
75
    {
76
        return $this->_archive;
77
    }
78
79
    /**
80
     * Set easy-rsa archive file with full path
81
     *
82
     * @param   string $archive
83
     * @return  Config
84
     */
85
    public function setArchive(string $archive): self
86
    {
87
        $this->_archive = $this->resolvePath($archive);
88
        return $this;
89
    }
90
91
    /**
92
     * Remove '.' and '..' path parts and make path absolute without
93
     * resolving symlinks.
94
     *
95
     * Examples:
96
     *
97
     *   resolvePath("test/./me/../now/", false);
98
     *   => test/now
99
     *
100
     *   resolvePath("test///.///me///../now/", true);
101
     *   => /home/example/test/now
102
     *
103
     *   resolvePath("test/./me/../now/", "/www/example.com");
104
     *   => /www/example.com/test/now
105
     *
106
     *   resolvePath("/test/./me/../now/", "/www/example.com");
107
     *   => /test/now
108
     *
109
     * @param string $path
110
     * @param mixed $basePath resolve paths realtively to this path. Params:
111
     *                        STRING: prefix with this path;
112
     *                        TRUE: use current dir;
113
     *                        FALSE: keep relative (default)
114
     * @return string resolved path
115
     */
116
    public function resolvePath(string $path, $basePath = true): string
117
    {
118
        // Make absolute path
119
        if ($path[0] !== DIRECTORY_SEPARATOR) {
120
            if ($basePath === true) {
121
                // Get PWD first to avoid getcwd() resolving symlinks if in symlinked folder
122
                $path = (getenv('PWD') ?: getcwd()) . DIRECTORY_SEPARATOR . $path;
123
            } elseif ('' !== $basePath) {
124
                $path = $basePath . DIRECTORY_SEPARATOR . $path;
125
            }
126
        }
127
128
        // Resolve '.' and '..'
129
        $components = array();
130
        foreach (explode(DIRECTORY_SEPARATOR, rtrim($path, DIRECTORY_SEPARATOR)) as $name) {
131
            if ($name === '..') {
132
                array_pop($components);
133
            } elseif ($name !== '.' && !(\count($components) && $name === '')) {
134
                // … && !(count($components) && $name === '') - we want to keep initial '/' for abs paths
135
                $components[] = $name;
136
            }
137
        }
138
139
        return implode(DIRECTORY_SEPARATOR, $components);
140
    }
141
}
142