Passed
Push — master ( 9c47c9...297c37 )
by Damien
04:39
created

Configuration::getStorageMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
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 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
    public function __construct(array $options)
57
    {
58
        $resolver = new OptionsResolver();
59
        $this->configureOptions($resolver);
60
        $config = $resolver->resolve($options);
61
62
        $this->tablePrefix = $config['table_prefix'];
63
        $this->tableSuffix = $config['table_suffix'];
64
        $this->ignoredColumns = $config['ignored_columns'];
65
66
        if (isset($config['entities']) && !empty($config['entities'])) {
67
            // use entity names as array keys for easier lookup
68
            foreach ($config['entities'] as $auditedEntity => $entityOptions) {
69
                $this->entities[$auditedEntity] = $entityOptions;
70
            }
71
        }
72
73
        $this->storageServices = $config['storage_services'];
74
        $this->auditingServices = $config['auditing_services'];
75
        $this->isViewerEnabled = $config['viewer'];
76
        $this->storageMapper = $config['storage_mapper'];
77
    }
78
79
    public function configureOptions(OptionsResolver $resolver): void
80
    {
81
        // https://symfony.com/doc/current/components/options_resolver.html
82
        $resolver
83
            ->setDefaults([
84
                'table_prefix' => '',
85
                'table_suffix' => '_audit',
86
                'ignored_columns' => [],
87
                'entities' => [],
88
                'storage_services' => [],
89
                'auditing_services' => [],
90
                'viewer' => true,
91
                'storage_mapper' => null,
92
            ])
93
            ->setAllowedTypes('table_prefix', 'string')
94
            ->setAllowedTypes('table_suffix', 'string')
95
            ->setAllowedTypes('ignored_columns', 'array')
96
            ->setAllowedTypes('entities', 'array')
97
            ->setAllowedTypes('storage_services', 'array')
98
            ->setAllowedTypes('auditing_services', 'array')
99
            ->setAllowedTypes('viewer', 'bool')
100
            ->setAllowedTypes('storage_mapper', ['null', 'string', 'callable'])
101
        ;
102
    }
103
104
    /**
105
     * Set the value of entities.
106
     *
107
     * This method completely overrides entities configuration
108
     * including annotation configuration
109
     *
110
     * @param array<int|string, mixed> $entities
111
     */
112
    public function setEntities(array $entities): self
113
    {
114
        $this->entities = $entities;
115
116
        return $this;
117
    }
118
119
    /**
120
     * enable audit Controller and its routing.
121
     *
122
     * @return $this
123
     */
124
    public function enableViewer(): self
125
    {
126
        $this->isViewerEnabled = true;
127
128
        return $this;
129
    }
130
131
    /**
132
     * disable audit Controller and its routing.
133
     *
134
     * @return $this
135
     */
136
    public function disableViewer(): self
137
    {
138
        $this->isViewerEnabled = false;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Get enabled flag.
145
     */
146
    public function isViewerEnabled(): bool
147
    {
148
        return $this->isViewerEnabled;
149
    }
150
151
    /**
152
     * Get the value of tablePrefix.
153
     */
154
    public function getTablePrefix(): string
155
    {
156
        return $this->tablePrefix;
157
    }
158
159
    /**
160
     * Get the value of tableSuffix.
161
     */
162
    public function getTableSuffix(): string
163
    {
164
        return $this->tableSuffix;
165
    }
166
167
    /**
168
     * Get the value of excludedColumns.
169
     *
170
     * @return array<string>
171
     */
172
    public function getIgnoredColumns(): array
173
    {
174
        return $this->ignoredColumns;
175
    }
176
177
    /**
178
     * Get the value of entities.
179
     */
180
    public function getEntities(): array
181
    {
182
        if (null === $this->entities && null !== $this->provider) {
183
            /** @var AuditingService[] $auditingServices */
184
            $auditingServices = $this->provider->getAuditingServices();
185
            foreach ($auditingServices as $auditingService) {
186
                $this->provider->loadAnnotations($auditingService->getEntityManager(), []);
187
            }
188
        }
189
190
        return null === $this->entities ? [] : $this->entities;
191
    }
192
193
    /**
194
     * Enables auditing for a specific entity.
195
     *
196
     * @param string $entity Entity class name
197
     *
198
     * @return $this
199
     */
200
    public function enableAuditFor(string $entity): self
201
    {
202
        if (isset($this->getEntities()[$entity])) {
203
            $this->entities[$entity]['enabled'] = true;
204
        }
205
206
        return $this;
207
    }
208
209
    /**
210
     * Disables auditing for a specific entity.
211
     *
212
     * @param string $entity Entity class name
213
     *
214
     * @return $this
215
     */
216
    public function disableAuditFor(string $entity): self
217
    {
218
        if (isset($this->getEntities()[$entity])) {
219
            $this->entities[$entity]['enabled'] = false;
220
        }
221
222
        return $this;
223
    }
224
225
    public function setStorageMapper(callable $mapper): self
226
    {
227
        $this->storageMapper = $mapper;
228
229
        return $this;
230
    }
231
232
    public function getStorageMapper(): ?callable
233
    {
234
        return $this->storageMapper;
235
    }
236
237
    public function getProvider(): DoctrineProvider
238
    {
239
        return $this->provider;
240
    }
241
242
    public function setProvider(DoctrineProvider $provider): void
243
    {
244
        $this->provider = $provider;
245
    }
246
}
247