Completed
Push — master ( 1e2294...0c3db1 )
by Mr
13:48
created

Config   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 137
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EasyRSA;
6
7
use function count;
8
use EasyRSA\Interfaces\ConfigInterface;
9
10
/**
11
 * Class Config.
12
 */
13
class Config implements ConfigInterface
14
{
15
    /**
16
     * Name of file (with or without full path to this file).
17
     */
18
    public string $archive = '.' . DIRECTORY_SEPARATOR . 'easy-rsa.tar.gz';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
19
20
    /**
21
     * Path to folder with easy-rsa scripts.
22
     */
23
    public string $scripts = '.' . DIRECTORY_SEPARATOR . 'easy-rsa';
24
25
    /**
26
     * Path to folder with certificates.
27
     */
28
    public string $certs = '.';
29
30
    /**
31
     * Config constructor.
32
     *
33
     * @param array<string, string> $parameters
34
     */
35
    public function __construct(array $parameters = [])
36
    {
37
        foreach ($parameters as $key => $value) {
38
            $this->set($key, $value);
39
        }
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function set(string $name, string $value): ConfigInterface
46
    {
47
        $this->$name = $value;
48
49
        return $this;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function get(string $name): string
56
    {
57
        return $this->$name;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getCerts(): string
64
    {
65
        return $this->certs;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function setCerts(string $path): ConfigInterface
72
    {
73
        return $this->set('certs', $this->resolvePath($path));
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getScripts(): string
80
    {
81
        return $this->scripts;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function setScripts(string $path): ConfigInterface
88
    {
89
        return $this->set('scripts', $this->resolvePath($path));
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getArchive(): string
96
    {
97
        return $this->archive;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function setArchive(string $path): ConfigInterface
104
    {
105
        return $this->set('archive', $this->resolvePath($path));
106
    }
107
108
    /**
109
     * Remove '.' and '..' path parts and make path absolute without
110
     * resolving symlinks.
111
     *
112
     * Examples:
113
     *
114
     *   resolvePath("test/./me/../now/", false);
115
     *   => test/now
116
     *
117
     *   resolvePath("test///.///me///../now/", true);
118
     *   => /home/example/test/now
119
     *
120
     *   resolvePath("test/./me/../now/", "/www/example.com");
121
     *   => /www/example.com/test/now
122
     *
123
     *   resolvePath("/test/./me/../now/", "/www/example.com");
124
     *   => /test/now
125
     *
126
     * @param string $path     Absolute or relative path on filesystem
127
     * @param mixed  $basePath Resolve paths relatively to this path. Params:
128
     *                         STRING: prefix with this path;
129
     *                         TRUE: use current dir;
130
     *                         FALSE: keep relative (default)
131
     *
132
     * @return string resolved path
133
     */
134
    protected function resolvePath(string $path, $basePath = true): string
135
    {
136
        // Make absolute path
137
        if (DIRECTORY_SEPARATOR !== $path[0]) {
138
            if (true === $basePath) {
139
                // Get PWD first to avoid getcwd() resolving symlinks if in symlinked folder
140
                $path = (getenv('PWD') ?: getcwd()) . DIRECTORY_SEPARATOR . $path;
141
            } elseif ('' !== $basePath) {
142
                $path = $basePath . DIRECTORY_SEPARATOR . $path;
143
            }
144
        }
145
146
        // Resolve '.' and '..'
147
        $components = [];
148
        foreach (explode(DIRECTORY_SEPARATOR, rtrim($path, DIRECTORY_SEPARATOR)) as $name) {
149
            if ('..' === $name) {
150
                array_pop($components);
151
            } elseif ('.' !== $name && !(count($components) && '' === $name)) {
152
                // … && !(count($components) && $name === '') - we want to keep initial '/' for abs paths
153
                $components[] = $name;
154
            }
155
        }
156
157
        return implode(DIRECTORY_SEPARATOR, $components);
158
    }
159
}
160