Completed
Push — master ( 1af02f...31e012 )
by Arne
02:10
created

Configuration::getVaultByTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Storeman;
4
5
use Storeman\Exception\Exception;
6
use Symfony\Component\Validator\Mapping\ClassMetadata;
7
use Zend\Stdlib\ArraySerializableInterface;
8
use Symfony\Component\Validator\Constraints as Assert;
9
10
class Configuration implements ArraySerializableInterface
11
{
12
    public const VAULT_CONFIG_CLASS = VaultConfiguration::class;
13
14
15
    /**
16
     * The local base path of the archive.
17
     *
18
     * @var string
19
     */
20
    protected $path = './';
21
22
    /**
23
     * Set of excluded paths.
24
     *
25
     * @var string[]
26
     */
27
    protected $exclude = [];
28
29
    /**
30
     * Identity to be visible in synchronization log.
31
     *
32
     * @var string
33
     */
34
    protected $identity;
35
36
    /**
37
     * Array of vault configurations.
38
     *
39
     * @var VaultConfiguration[]
40
     */
41
    protected $vaults = [];
42
43
    /**
44
     * @return string
45
     */
46
    public function getPath(): string
47
    {
48
        return $this->path;
49
    }
50
51
    /**
52
     * @param string $path
53
     *
54
     * @return Configuration
55
     */
56
    public function setPath(string $path): Configuration
57
    {
58
        if (substr($path, -1) !== DIRECTORY_SEPARATOR)
59
        {
60
            $path .= DIRECTORY_SEPARATOR;
61
        }
62
63
        $this->path = $path;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return \string[]
70
     */
71
    public function getExclude(): array
72
    {
73
        return $this->exclude;
74
    }
75
76
    /**
77
     * @param \string[] $paths
78
     *
79
     * @return Configuration
80
     */
81
    public function setExclude(array $paths): Configuration
82
    {
83
        $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...
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param string $path
90
     *
91
     * @return Configuration
92
     */
93
    public function addExclusion(string $path): Configuration
94
    {
95
        $this->exclude[] = $path;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getIdentity(): ?string
104
    {
105
        return $this->identity;
106
    }
107
108
    /**
109
     * @param string $identity
110
     *
111
     * @return Configuration
112
     */
113
    public function setIdentity(string $identity): Configuration
114
    {
115
        $this->identity = $identity;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return VaultConfiguration[]
122
     */
123
    public function getVaults(): array
124
    {
125
        return $this->vaults;
126
    }
127
128
    /**
129
     * @param string $title
130
     * @return bool
131
     */
132
    public function hasVault(string $title): bool
133
    {
134
        return $this->getVaultConfiguration($title) !== null;
135
    }
136
137
    /**
138
     * @param string $title
139
     *
140
     * @return VaultConfiguration
141
     */
142
    public function getVault(string $title): VaultConfiguration
143
    {
144
        if ($vaultConfiguration = $this->getVaultConfiguration($title))
145
        {
146
            return $vaultConfiguration;
147
        }
148
149
        throw new \InvalidArgumentException("Unknown vault configuration requested: {$title}");
150
    }
151
152
    /**
153
     * @internal Use VaultConfiguration constructor
154
     * @param VaultConfiguration $configuration
155
     *
156
     * @return Configuration
157
     * @throws Exception
158
     */
159
    public function addVault(VaultConfiguration $configuration): Configuration
160
    {
161
        if (array_search($configuration, $this->vaults) === false)
162
        {
163
            if ($this->hasVault($configuration->getTitle()))
164
            {
165
                throw new Exception(sprintf('Trying to add vault with duplicate title %s.', $configuration->getTitle()));
166
            }
167
168
            $this->vaults[] = $configuration;
169
        }
170
171
        return $this;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function exchangeArray(array $array)
178
    {
179
        if ($diff = array_diff(array_keys($array), array_keys(get_object_vars($this))))
180
        {
181
            throw new \InvalidArgumentException("Invalid index(es): " . implode(',', $diff));
182
        }
183
184
        foreach ($array as $key => $value)
185
        {
186
            if ($key === 'vaults')
187
            {
188
                if (!is_array($value))
189
                {
190
                    throw new \InvalidArgumentException();
191
                }
192
193
                $this->vaults = [];
194
195
                foreach ($value as $val)
196
                {
197
                    if (!is_array($val))
198
                    {
199
                        throw new \InvalidArgumentException();
200
                    }
201
202
                    $className = static::VAULT_CONFIG_CLASS;
203
204
                    /** @var VaultConfiguration $vaultConfig */
205
                    $vaultConfig = new $className($this);
206
                    $vaultConfig->exchangeArray($val);
207
                }
208
            }
209
            else
210
            {
211
                // using setter to prevent skipping validation
212
                call_user_func([$this, 'set' . ucfirst($key)], $value);
213
            }
214
        }
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function getArrayCopy()
221
    {
222
        $return = get_object_vars($this);
223
        $return['vaults'] = array_values(array_map(function(VaultConfiguration $vaultConfiguration) {
224
225
            return $vaultConfiguration->getArrayCopy();
226
227
        }, $this->vaults));
228
229
        return $return;
230
    }
231
232
    protected function getVaultConfiguration(string $title): ?VaultConfiguration
233
    {
234
        foreach ($this->vaults as $vaultConfiguration)
235
        {
236
            if ($vaultConfiguration->getTitle() === $title)
237
            {
238
                return $vaultConfiguration;
239
            }
240
        }
241
242
        return null;
243
    }
244
245
    public static function loadValidatorMetadata(ClassMetadata $metadata)
246
    {
247
        $metadata->addPropertyConstraint('path', new Assert\NotBlank());
248
        $metadata->addPropertyConstraint('identity', new Assert\NotBlank());
249
        $metadata->addPropertyConstraint('vaults', new Assert\Count(['min' => 1]));
250
    }
251
}
252