Completed
Push — master ( b2e536...a4e6dd )
by Arne
02:05
created

VaultConfiguration::setVaultLayout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Storeman;
4
5
use Storeman\Validation\Constraints as StoremanAssert;
6
use Symfony\Component\Validator\Constraints as Assert;
7
use Symfony\Component\Validator\Mapping\ClassMetadata;
8
use Zend\Stdlib\ArraySerializableInterface;
9
10
class VaultConfiguration implements ArraySerializableInterface
11
{
12
    /**
13
     * @var Configuration
14
     */
15
    protected $configuration;
16
17
    /**
18
     * An arbitrary user-defined title that helps to identity a vault by some user-specific information.
19
     *
20
     * @var string
21
     */
22
    protected $title = 'unknown';
23
24
    /**
25
     * Identifier for the vault layout to use.
26
     *
27
     * @var string
28
     */
29
    protected $vaultLayout = 'amberjack';
30
31
    /**
32
     * Identifier for the vault adapter to use.
33
     *
34
     * @var string
35
     */
36
    protected $adapter = 'unknown';
37
38
    /**
39
     * Identifier for the lock adapter to use.
40
     *
41
     * @var string
42
     */
43
    protected $lockAdapter = 'storage';
44
45
    /**
46
     * Identifier for the index merger to be used.
47
     *
48
     * @var string
49
     */
50
    protected $indexMerger = 'standard';
51
52
    /**
53
     * Identifier for the conflict handler to use.
54
     *
55
     * @var string
56
     */
57
    protected $conflictHandler = 'panicking';
58
59
    /**
60
     * Identifier for the operation list builder to use.
61
     *
62
     * @var string
63
     */
64
    protected $operationListBuilder = 'standard';
65
66
    /**
67
     * Map with additional storageAdapter- or lockAdapter-specific settings.
68
     *
69
     * @var array
70
     */
71
    protected $settings = [];
72
73
    public function __construct(Configuration $configuration)
74
    {
75
        $this->configuration = $configuration;
76
        $this->configuration->addVault($this);
77
    }
78
79
    public function getConfiguration(): Configuration
80
    {
81
        return $this->configuration;
82
    }
83
84
    public function getTitle(): string
85
    {
86
        return $this->title;
87
    }
88
89
    public function setTitle(string $title): VaultConfiguration
90
    {
91
        $this->title = $title;
92
93
        return $this;
94
    }
95
96
    public function getVaultLayout(): string
97
    {
98
        return $this->vaultLayout;
99
    }
100
101
    public function setVaultLayout(string $vaultLayout): VaultConfiguration
102
    {
103
        $this->vaultLayout = $vaultLayout;
104
105
        return $this;
106
    }
107
108
    public function getAdapter(): string
109
    {
110
        return $this->adapter;
111
    }
112
113
    public function setAdapter(string $adapter): VaultConfiguration
114
    {
115
        $this->adapter = $adapter;
116
117
        return $this;
118
    }
119
120
    public function getLockAdapter(): string
121
    {
122
        return $this->lockAdapter;
123
    }
124
125
    public function setLockAdapter(string $lockAdapter): VaultConfiguration
126
    {
127
        $this->lockAdapter = $lockAdapter;
128
129
        return $this;
130
    }
131
132
    public function getIndexMerger(): string
133
    {
134
        return $this->indexMerger;
135
    }
136
137
    public function setIndexMerger(string $indexMerger): VaultConfiguration
138
    {
139
        $this->indexMerger = $indexMerger;
140
141
        return $this;
142
    }
143
144
    public function getConflictHandler(): string
145
    {
146
        return $this->conflictHandler;
147
    }
148
149
    public function setConflictHandler(string $conflictHandler): VaultConfiguration
150
    {
151
        $this->conflictHandler = $conflictHandler;
152
153
        return $this;
154
    }
155
156
    public function getOperationListBuilder(): string
157
    {
158
        return $this->operationListBuilder;
159
    }
160
161
    public function setOperationListBuilder(string $operationListBuilder): VaultConfiguration
162
    {
163
        $this->operationListBuilder = $operationListBuilder;
164
165
        return $this;
166
    }
167
168
    public function getSettings(): array
169
    {
170
        return $this->settings;
171
    }
172
173
    public function getSetting(string $name)
174
    {
175
        return isset($this->settings[$name]) ? $this->settings[$name] : null;
176
    }
177
178
    public function setSettings(array $settings): VaultConfiguration
179
    {
180
        $this->settings = $settings;
181
182
        return $this;
183
    }
184
185
    public function setSetting(string $name, $value): VaultConfiguration
186
    {
187
        $this->settings[$name] = $value;
188
189
        return $this;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function exchangeArray(array $array)
196
    {
197
        if ($diff = array_diff(array_keys($array), array_keys($this->getArrayCopy())))
198
        {
199
            throw new \InvalidArgumentException("Invalid index(es): " . implode(',', $diff));
200
        }
201
202
        foreach ($array as $key => $value)
203
        {
204
            // using setter to prevent skipping validation
205
            call_user_func([$this, 'set' . ucfirst($key)], $value);
206
        }
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function getArrayCopy()
213
    {
214
        $array = get_object_vars($this);
215
216
        unset($array['configuration']);
217
218
        return $array;
219
    }
220
221
    public static function loadValidatorMetadata(ClassMetadata $metadata)
222
    {
223
        $metadata->addPropertyConstraint('title', new Assert\NotBlank());
224
        $metadata->addPropertyConstraint('adapter', new StoremanAssert\StorageAdapterExists());
225
        $metadata->addPropertyConstraint('conflictHandler', new StoremanAssert\ConflictHandlerExists());
226
        $metadata->addPropertyConstraint('indexMerger', new StoremanAssert\IndexMergerExists());
227
        $metadata->addPropertyConstraint('lockAdapter', new StoremanAssert\LockAdapterExists());
228
        $metadata->addPropertyConstraint('operationListBuilder', new StoremanAssert\OperationListBuilderExists());
229
    }
230
}
231