Passed
Push — master ( 6b4be4...c652ff )
by Damien
04:06
created

Configuration::setStorageMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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