Config::set()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EasyRSA;
6
7
use function count;
8
use EasyRSA\Interfaces\ConfigInterface;
9
use InvalidArgumentException;
10
11
/**
12
 * Class Config.
13
 */
14
class Config implements ConfigInterface
15
{
16
    /**
17
     * Path to archive with EasyRSA scripts
18
     */
19
    public $archive = './easy-rsa.tar.gz';
20
21
    /**
22
     * Path to folder with EasyRSA scripts
23
     */
24
    public $scripts = './easy-rsa';
25
26
    /**
27
     * Part to certificates store folder
28
     */
29
    public $certs = './easy-rsa-certs';
30
31
    /**
32
     * List of allowed variables
33
     */
34
    public const ALLOWED = [
35
        'scripts',
36
        'archive',
37
        'certs',
38
    ];
39
40
    /**
41
     * Config constructor.
42
     *
43
     * @param array<string, string> $parameters
44
     */
45 12
    public function __construct(array $parameters = [])
46
    {
47 12
        foreach ($parameters as $key => $value) {
48 1
            $this->set($key, $value);
49
        }
50 12
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @throws \InvalidArgumentException If wrong key name provided
56
     */
57 3
    public function set(string $name, $value = null, bool $resolveAbsolutePath = true): ConfigInterface
58
    {
59 3
        if (!in_array($name, self::ALLOWED, true)) {
60 2
            throw new InvalidArgumentException('Parameter "' . $name . '" is not in allowed list [' . implode(',', self::ALLOWED) . ']');
61
        }
62
63 1
        $this->$name = $resolveAbsolutePath ? $this->resolvePath($value) : $value;
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 57 can also be of type boolean or null; however, EasyRSA\Config::resolvePath() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
64
65 1
        return $this;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @throws \InvalidArgumentException If wrong key name provided
72
     */
73 4
    public function get(string $name)
74
    {
75 4
        if (!in_array($name, self::ALLOWED, true)) {
76 1
            throw new InvalidArgumentException('Parameter "' . $name . '" is not in allowed list [' . implode(',', self::ALLOWED) . ']');
77
        }
78
79 3
        return $this->$name;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 5
    public function resolvePath(string $path, string $basePath = null): string
86
    {
87
        // Make absolute path
88 5
        if (DIRECTORY_SEPARATOR !== $path[0]) {
89 1
            if (null === $basePath) {
90
                // Get PWD first to avoid getcwd() resolving symlinks if in symlinked folder
91 1
                $path = (getenv('PWD') ?: getcwd()) . DIRECTORY_SEPARATOR . $path;
92
            } elseif ('' !== $basePath) {
93
                $path = $basePath . DIRECTORY_SEPARATOR . $path;
94
            }
95
        }
96
97
        // Resolve '.' and '..'
98 5
        $components = [];
99 5
        foreach (explode(DIRECTORY_SEPARATOR, rtrim($path, DIRECTORY_SEPARATOR)) as $name) {
100 5
            if ('..' === $name) {
101 2
                array_pop($components);
102 5
            } elseif ('.' !== $name && !(count($components) > 0 && '' === $name)) {
103
                // … && !(count($components) && $name === '') - we want to keep initial '/' for abs paths
104 5
                $components[] = $name;
105
            }
106
        }
107
108 5
        return implode(DIRECTORY_SEPARATOR, $components);
109
    }
110
111
    /**
112
     * @codeCoverageIgnore
113
     * @deprecated Use ->get('certs')
114
     */
115
    public function getCerts(): string
116
    {
117
        return $this->get('certs');
118
    }
119
120
    /**
121
     * @param string $path
122
     *
123
     * @return \EasyRSA\Interfaces\ConfigInterface
124
     * @codeCoverageIgnore
125
     * @deprecated Use ->set('certs')
126
     */
127
    public function setCerts(string $path): ConfigInterface
128
    {
129
        return $this->set('certs', $path, true);
130
    }
131
132
    /**
133
     * @codeCoverageIgnore
134
     * @deprecated Use ->get('scripts')
135
     */
136
    public function getScripts(): string
137
    {
138
        return $this->get('scripts');
139
    }
140
141
    /**
142
     * @param string $path
143
     *
144
     * @return \EasyRSA\Interfaces\ConfigInterface
145
     * @codeCoverageIgnore
146
     * @deprecated Use ->set('scripts')
147
     */
148
    public function setScripts(string $path): ConfigInterface
149
    {
150
        return $this->set('scripts', $path, true);
151
    }
152
153
    /**
154
     * @codeCoverageIgnore
155
     * @deprecated Use ->get('archive')
156
     */
157
    public function getArchive(): string
158
    {
159
        return $this->get('archive');
160
    }
161
162
    /**
163
     * @param string $path
164
     *
165
     * @return \EasyRSA\Interfaces\ConfigInterface
166
     * @codeCoverageIgnore
167
     * @deprecated Use ->set('archive')
168
     */
169
    public function setArchive(string $path): ConfigInterface
170
    {
171
        return $this->set('archive', $path, true);
172
    }
173
}
174