Completed
Push — master ( a8c5d7...b64b96 )
by Arne
02:19
created

Configuration::getVaultByTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Storeman\Config;
4
5
use Symfony\Component\Validator\Mapping\ClassMetadata;
6
use Zend\Stdlib\ArraySerializableInterface;
7
use Symfony\Component\Validator\Constraints as Assert;
8
9
class Configuration implements ArraySerializableInterface
10
{
11
    /**
12
     * The local base path of the archive.
13
     *
14
     * @var string
15
     */
16
    protected $path = './';
17
18
    /**
19
     * Set of excluded paths.
20
     *
21
     * @var string[]
22
     */
23
    protected $exclude = [];
24
25
    /**
26
     * Identity to be visible in synchronization log.
27
     *
28
     * @var string
29
     */
30
    protected $identity = 'unknown';
31
32
    /**
33
     * Identifier of the index builder to use.
34
     *
35
     * @var string
36
     */
37
    protected $indexBuilder = 'standard';
38
39
    /**
40
     * List of file checksums to be used for integrity checks and modification detection.
41
     *
42
     * @var string[]
43
     */
44
    protected $fileChecksums = ['sha256', 'sha1', 'md5'];
45
46
    /**
47
     * Array of vault configurations.
48
     *
49
     * @var VaultConfiguration[]
50
     */
51
    protected $vaults = [];
52
53
    /**
54
     * @return string
55
     */
56
    public function getPath(): string
57
    {
58
        return $this->path;
59
    }
60
61
    /**
62
     * @param string $path
63
     *
64
     * @return Configuration
65
     */
66
    public function setPath(string $path): Configuration
67
    {
68
        if (substr($path, -1) !== '/')
69
        {
70
            $path .= '/';
71
        }
72
73
        $this->path = $path;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return \string[]
80
     */
81
    public function getExclude(): array
82
    {
83
        return $this->exclude;
84
    }
85
86
    /**
87
     * @param \string[] $paths
88
     *
89
     * @return Configuration
90
     */
91
    public function setExclude(array $paths): Configuration
92
    {
93
        $this->exclude = array_values($paths);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($paths) of type array<integer,object<string>> is incompatible with the declared type array<integer,string> of property $exclude.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param string $path
100
     *
101
     * @return Configuration
102
     */
103
    public function addExclusion(string $path): Configuration
104
    {
105
        $this->exclude[] = $path;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getIdentity(): string
114
    {
115
        return $this->identity;
116
    }
117
118
    /**
119
     * @param string $identity
120
     *
121
     * @return Configuration
122
     */
123
    public function setIdentity(string $identity): Configuration
124
    {
125
        $this->identity = $identity;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getIndexBuilder(): string
134
    {
135
        return $this->indexBuilder;
136
    }
137
138
    /**
139
     * @param string $indexBuilder
140
     * @return $this
141
     */
142
    public function setIndexBuilder(string $indexBuilder): Configuration
143
    {
144
        $this->indexBuilder = $indexBuilder;
145
146
        return $this;
147
    }
148
149
    /**
150
     * @return string[]
151
     */
152
    public function getFileChecksums(): array
153
    {
154
        return $this->fileChecksums;
155
    }
156
157
    /**
158
     * @param string[] $fileChecksums
159
     * @return Configuration
160
     */
161
    public function setFileChecksums(array $fileChecksums): Configuration
162
    {
163
        $this->fileChecksums = array_values($fileChecksums);
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return VaultConfiguration[]
170
     */
171
    public function getVaults(): array
172
    {
173
        return $this->vaults;
174
    }
175
176
    /**
177
     * @param string $title
178
     *
179
     * @return VaultConfiguration
180
     */
181
    public function getVaultByTitle(string $title): VaultConfiguration
182
    {
183
        if ($vaultConfiguration = $this->getVaultConfigurationByTitle($title))
184
        {
185
            return $vaultConfiguration;
186
        }
187
188
        throw new \InvalidArgumentException("Unknown vault configuration requested: {$title}");
189
    }
190
191
    public function addVault(VaultConfiguration $configuration): Configuration
192
    {
193
        if (array_search($configuration, $this->vaults) === false)
194
        {
195
            $this->vaults[] = $configuration;
196
        }
197
198
        return $this;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function exchangeArray(array $array)
205
    {
206
        if ($diff = array_diff(array_keys($array), array_keys(get_object_vars($this))))
207
        {
208
            throw new \InvalidArgumentException("Invalid index(es): " . implode(',', $diff));
209
        }
210
211
        foreach ($array as $key => $value)
212
        {
213
            if ($key === 'vaults')
214
            {
215
                if (!is_array($value))
216
                {
217
                    throw new \InvalidArgumentException();
218
                }
219
220
                $this->vaults = [];
221
222
                foreach ($value as $val)
223
                {
224
                    if (!is_array($val))
225
                    {
226
                        throw new \InvalidArgumentException();
227
                    }
228
229
                    $vaultConfig = new VaultConfiguration($this);
230
                    $vaultConfig->exchangeArray($val);
231
                }
232
            }
233
            else
234
            {
235
                // using setter to prevent skipping validation
236
                call_user_func([$this, 'set' . ucfirst($key)], $value);
237
            }
238
        }
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function getArrayCopy()
245
    {
246
        $return = get_object_vars($this);
247
        $return['vaults'] = array_values(array_map(function(VaultConfiguration $vaultConfiguration) {
248
249
            return $vaultConfiguration->getArrayCopy();
250
251
        }, $this->vaults));
252
253
        return $return;
254
    }
255
256
    protected function getVaultConfigurationByTitle(string $title): ?VaultConfiguration
257
    {
258
        foreach ($this->vaults as $vaultConfiguration)
259
        {
260
            if ($vaultConfiguration->getTitle() === $title)
261
            {
262
                return $vaultConfiguration;
263
            }
264
        }
265
266
        return null;
267
    }
268
269
    public static function loadValidatorMetadata(ClassMetadata $metadata)
270
    {
271
        $metadata->addPropertyConstraint('path', new Assert\NotBlank());
272
        $metadata->addPropertyConstraint('identity', new Assert\NotBlank());
273
        $metadata->addPropertyConstraint('vaults', new Assert\Count(['min' => 1]));
274
    }
275
}
276