Passed
Push — master ( 111b19...81e998 )
by Damien
06:16
created

Configuration::getIgnoredColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace DH\Auditor\Provider\Doctrine;
4
5
use DH\Auditor\Provider\ConfigurationInterface;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
class Configuration implements ConfigurationInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $tablePrefix;
14
15
    /**
16
     * @var string
17
     */
18
    private $tableSuffix;
19
20
    /**
21
     * @var array
22
     */
23
    private $ignoredColumns;
24
25
    /**
26
     * @var array
27
     */
28
    private $entities = [];
29
30
    /**
31
     * @var array
32
     */
33
    private $storageServices = [];
34
35
    /**
36
     * @var array
37
     */
38
    private $auditingServices = [];
39
40
    /**
41
     * @var bool
42
     */
43
    private $isViewerEnabled;
44
45
    /**
46
     * @var callable
47
     */
48
    private $storageMapper;
49
50
    /**
51
     * @var callable
52
     */
53
    private $userProvider;
54
55
    /**
56
     * @var callable
57
     */
58
    private $roleChecker;
59
60
    /**
61
     * @var callable
62
     */
63
    private $securityProvider;
64
65
    public function __construct(array $options)
66
    {
67
        $resolver = new OptionsResolver();
68
        $this->configureOptions($resolver);
69
        $config = $resolver->resolve($options);
70
71
        $this->tablePrefix = $config['table_prefix'];
72
        $this->tableSuffix = $config['table_suffix'];
73
        $this->ignoredColumns = $config['ignored_columns'];
74
75
        if (isset($config['entities']) && !empty($config['entities'])) {
76
            // use entity names as array keys for easier lookup
77
            foreach ($config['entities'] as $auditedEntity => $entityOptions) {
78
                $this->entities[$auditedEntity] = $entityOptions;
79
            }
80
        }
81
82
        $this->storageServices = $config['storage_services'];
83
        $this->auditingServices = $config['auditing_services'];
84
        $this->storageMapper = $config['storage_mapper'];
85
        $this->roleChecker = $config['role_checker'];
86
        $this->userProvider = $config['user_provider'];
87
        $this->securityProvider = $config['security_provider'];
88
        $this->isViewerEnabled = $config['viewer'];
89
    }
90
91
    public function configureOptions(OptionsResolver $resolver): void
92
    {
93
        // https://symfony.com/doc/current/components/options_resolver.html
94
        $resolver
95
            ->setDefaults([
96
                'table_prefix' => '',
97
                'table_suffix' => '_audit',
98
                'ignored_columns' => [],
99
                'entities' => [],
100
                'storage_services' => [],
101
                'auditing_services' => [],
102
                'storage_mapper' => null,
103
                'role_checker' => null,
104
                'user_provider' => null,
105
                'security_provider' => null,
106
                'viewer' => true,
107
            ])
108
            ->setAllowedTypes('table_prefix', 'string')
109
            ->setAllowedTypes('table_suffix', 'string')
110
            ->setAllowedTypes('ignored_columns', 'array')
111
            ->setAllowedTypes('entities', 'array')
112
            ->setAllowedTypes('storage_services', 'array')
113
            ->setAllowedTypes('auditing_services', 'array')
114
            ->setAllowedTypes('storage_mapper', ['null', 'string', 'callable'])
115
            ->setAllowedTypes('role_checker', ['null', 'string', 'callable'])
116
            ->setAllowedTypes('user_provider', ['null', 'string', 'callable'])
117
            ->setAllowedTypes('security_provider', ['null', 'string', 'callable'])
118
            ->setAllowedTypes('viewer', 'bool')
119
        ;
120
    }
121
122
    /**
123
     * Set the value of entities.
124
     *
125
     * This method completely overrides entities configuration
126
     * including annotation configuration
127
     *
128
     * @param array<int|string, mixed> $entities
129
     */
130
    public function setEntities(array $entities): self
131
    {
132
        $this->entities = $entities;
133
134
        return $this;
135
    }
136
137
    /**
138
     * enable audit Controller and its routing.
139
     *
140
     * @return $this
141
     */
142
    public function enableViewer(): self
143
    {
144
        $this->isViewerEnabled = true;
145
146
        return $this;
147
    }
148
149
    /**
150
     * disable audit Controller and its routing.
151
     *
152
     * @return $this
153
     */
154
    public function disableViewer(): self
155
    {
156
        $this->isViewerEnabled = false;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Get enabled flag.
163
     */
164
    public function isViewerEnabled(): bool
165
    {
166
        return $this->isViewerEnabled;
167
    }
168
169
    /**
170
     * Get the value of tablePrefix.
171
     */
172
    public function getTablePrefix(): string
173
    {
174
        return $this->tablePrefix;
175
    }
176
177
    /**
178
     * Get the value of tableSuffix.
179
     */
180
    public function getTableSuffix(): string
181
    {
182
        return $this->tableSuffix;
183
    }
184
185
    /**
186
     * Get the value of excludedColumns.
187
     *
188
     * @return array<string>
189
     */
190
    public function getIgnoredColumns(): array
191
    {
192
        return $this->ignoredColumns;
193
    }
194
195
    /**
196
     * Get the value of entities.
197
     */
198
    public function getEntities(): array
199
    {
200
        return $this->entities;
201
    }
202
203
    /**
204
     * Enables auditing for a specific entity.
205
     *
206
     * @param string $entity Entity class name
207
     *
208
     * @return $this
209
     */
210
    public function enableAuditFor(string $entity): self
211
    {
212
        if (isset($this->getEntities()[$entity])) {
213
            $this->entities[$entity]['enabled'] = true;
214
        }
215
216
        return $this;
217
    }
218
219
    /**
220
     * Disables auditing for a specific entity.
221
     *
222
     * @param string $entity Entity class name
223
     *
224
     * @return $this
225
     */
226
    public function disableAuditFor(string $entity): self
227
    {
228
        if (isset($this->getEntities()[$entity])) {
229
            $this->entities[$entity]['enabled'] = false;
230
        }
231
232
        return $this;
233
    }
234
235
    public function setStorageMapper(callable $mapper): self
236
    {
237
        $this->storageMapper = $mapper;
238
239
        return $this;
240
    }
241
242
    public function getStorageMapper(): ?callable
243
    {
244
        return $this->storageMapper;
245
    }
246
247
    public function setUserProvider(callable $userProvider): self
248
    {
249
        $this->userProvider = $userProvider;
250
251
        return $this;
252
    }
253
254
    public function getUserProvider(): ?callable
255
    {
256
        return $this->userProvider;
257
    }
258
259
    public function setRoleChecker(callable $roleChecker): self
260
    {
261
        $this->roleChecker = $roleChecker;
262
263
        return $this;
264
    }
265
266
    public function getRoleChecker(): ?callable
267
    {
268
        return $this->roleChecker;
269
    }
270
271
    public function setSecurityProvider(callable $securityProvider): self
272
    {
273
        $this->securityProvider = $securityProvider;
274
275
        return $this;
276
    }
277
278
    public function getSecurityProvider(): ?callable
279
    {
280
        return $this->securityProvider;
281
    }
282
}
283