Passed
Push — master ( 62f214...87bb7b )
by Damien
02:13
created

Configuration::getEntities()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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