Passed
Pull Request — master (#101)
by Damien
02:47
created

AuditConfiguration::getAnnotationLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DH\DoctrineAuditBundle;
4
5
use DH\DoctrineAuditBundle\Annotation\AnnotationLoader;
6
use DH\DoctrineAuditBundle\Helper\DoctrineHelper;
7
use DH\DoctrineAuditBundle\User\UserProviderInterface;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
12
class AuditConfiguration
13
{
14
    /**
15
     * @var string
16
     */
17
    private $tablePrefix;
18
19
    /**
20
     * @var string
21
     */
22
    private $tableSuffix;
23
24
    /**
25
     * @var string
26
     */
27
    private $timezone;
28
29
    /**
30
     * @var array
31
     */
32
    private $ignoredColumns = [];
33
34
    /**
35
     * @var array
36
     */
37
    private $entities = [];
38
39
    /**
40
     * @var bool
41
     */
42
    private $enabled = true;
43
44
    /**
45
     * @var UserProviderInterface
46
     */
47
    protected $userProvider;
48
49
    /**
50
     * @var RequestStack
51
     */
52
    protected $requestStack;
53
54
    /**
55
     * @var FirewallMap
56
     */
57
    private $firewallMap;
58
59
    /**
60
     * @var EntityManagerInterface
61
     */
62
    private $entityManager;
63
64
    /**
65
     * @var null|AnnotationLoader
66
     */
67
    private $annotationLoader;
68
69
    public function __construct(
70
        array $config,
71
        UserProviderInterface $userProvider,
72
        RequestStack $requestStack,
73
        FirewallMap $firewallMap,
74
        EntityManagerInterface $entityManager,
75
        ?AnnotationLoader $annotationLoader = null
76
    ) {
77
        $this->userProvider = $userProvider;
78
        $this->requestStack = $requestStack;
79
        $this->firewallMap = $firewallMap;
80
        $this->entityManager = $entityManager;
81
        $this->annotationLoader = $annotationLoader;
82
83
        $this->enabled = $config['enabled'];
84
        $this->tablePrefix = $config['table_prefix'];
85
        $this->tableSuffix = $config['table_suffix'];
86
        $this->timezone = $config['timezone'];
87
        $this->ignoredColumns = $config['ignored_columns'];
88
89
        if (isset($config['entities']) && !empty($config['entities'])) {
90
            // use entity names as array keys for easier lookup
91
            foreach ($config['entities'] as $auditedEntity => $entityOptions) {
92
                $this->entities[$auditedEntity] = $entityOptions;
93
            }
94
        }
95
96
        // Update config using annotations
97
        if (null !== $this->annotationLoader) {
98
            $config = $this->annotationLoader->load();
99
            $this->entities = array_merge($this->entities, $config);
100
        }
101
    }
102
103
    /**
104
     * Set the value of entities.
105
     *
106
     * @param array $entities
107
     */
108
    public function setEntities(array $entities): void
109
    {
110
        $this->entities = $entities;
111
    }
112
113
    /**
114
     * enabled audit.
115
     *
116
     * @return $this
117
     */
118
    public function enable(): self
119
    {
120
        $this->enabled = true;
121
122
        return $this;
123
    }
124
125
    /**
126
     * disable audit.
127
     *
128
     * @return $this
129
     */
130
    public function disable(): self
131
    {
132
        $this->enabled = false;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Get enabled flag.
139
     *
140
     * @return bool
141
     */
142
    public function isEnabled(): bool
143
    {
144
        return $this->enabled;
145
    }
146
147
    /**
148
     * Returns true if $entity is auditable.
149
     *
150
     * @param object|string $entity
151
     *
152
     * @return bool
153
     */
154
    public function isAuditable($entity): bool
155
    {
156
        $class = DoctrineHelper::getRealClass($entity);
157
158
        // is $entity part of audited entities?
159
        if (!\array_key_exists($class, $this->entities)) {
160
            // no => $entity is not audited
161
            return false;
162
        }
163
164
        return true;
165
    }
166
167
    /**
168
     * Returns true if $entity is audited.
169
     *
170
     * @param object|string $entity
171
     *
172
     * @return bool
173
     */
174
    public function isAudited($entity): bool
175
    {
176
        if (!$this->enabled) {
177
            return false;
178
        }
179
180
        $class = DoctrineHelper::getRealClass($entity);
181
182
        // is $entity part of audited entities?
183
        if (!\array_key_exists($class, $this->entities)) {
184
            // no => $entity is not audited
185
            return false;
186
        }
187
188
        $entityOptions = $this->entities[$class];
189
190
        if (null === $entityOptions) {
191
            // no option defined => $entity is audited
192
            return true;
193
        }
194
195
        if (isset($entityOptions['enabled'])) {
196
            return (bool) $entityOptions['enabled'];
197
        }
198
199
        return true;
200
    }
201
202
    /**
203
     * Returns true if $field is audited.
204
     *
205
     * @param object|string $entity
206
     * @param string        $field
207
     *
208
     * @return bool
209
     */
210
    public function isAuditedField($entity, string $field): bool
211
    {
212
        // is $field is part of globally ignored columns?
213
        if (\in_array($field, $this->ignoredColumns, true)) {
214
            // yes => $field is not audited
215
            return false;
216
        }
217
218
        // is $entity audited?
219
        if (!$this->isAudited($entity)) {
220
            // no => $field is not audited
221
            return false;
222
        }
223
224
        $class = DoctrineHelper::getRealClass($entity);
225
        $entityOptions = $this->entities[$class];
226
227
        if (null === $entityOptions) {
228
            // no option defined => $field is audited
229
            return true;
230
        }
231
232
        // are columns excluded and is field part of them?
233
        if (isset($entityOptions['ignored_columns']) &&
234
            \in_array($field, $entityOptions['ignored_columns'], true)) {
235
            // yes => $field is not audited
236
            return false;
237
        }
238
239
        return true;
240
    }
241
242
    /**
243
     * Get the value of tablePrefix.
244
     *
245
     * @return string
246
     */
247
    public function getTablePrefix(): string
248
    {
249
        return $this->tablePrefix;
250
    }
251
252
    /**
253
     * Get the value of tableSuffix.
254
     *
255
     * @return string
256
     */
257
    public function getTableSuffix(): string
258
    {
259
        return $this->tableSuffix;
260
    }
261
262
    /**
263
     * Get the value of timezone.
264
     *
265
     * @return string
266
     */
267
    public function getTimezone(): string
268
    {
269
        return $this->timezone;
270
    }
271
272
    /**
273
     * Get the value of excludedColumns.
274
     *
275
     * @return array
276
     */
277
    public function getIgnoredColumns(): array
278
    {
279
        return $this->ignoredColumns;
280
    }
281
282
    /**
283
     * Get the value of entities.
284
     *
285
     * @return array
286
     */
287
    public function getEntities(): array
288
    {
289
        return $this->entities;
290
    }
291
292
    /**
293
     * Enables auditing for a specific entity.
294
     *
295
     * @param string $entity Entity class name
296
     *
297
     * @return $this
298
     */
299
    public function enableAuditFor(string $entity): self
300
    {
301
        if (isset($this->entities[$entity])) {
302
            $this->entities[$entity]['enabled'] = true;
303
        }
304
305
        return $this;
306
    }
307
308
    /**
309
     * Disables auditing for a specific entity.
310
     *
311
     * @param string $entity Entity class name
312
     *
313
     * @return $this
314
     */
315
    public function disableAuditFor(string $entity): self
316
    {
317
        if (isset($this->entities[$entity])) {
318
            $this->entities[$entity]['enabled'] = false;
319
        }
320
321
        return $this;
322
    }
323
324
    /**
325
     * Get the value of userProvider.
326
     *
327
     * @return UserProviderInterface
328
     */
329
    public function getUserProvider(): ?UserProviderInterface
330
    {
331
        return $this->userProvider;
332
    }
333
334
    /**
335
     * Get the value of requestStack.
336
     *
337
     * @return RequestStack
338
     */
339
    public function getRequestStack(): RequestStack
340
    {
341
        return $this->requestStack;
342
    }
343
344
    /**
345
     * Gets the value of firewallMap.
346
     *
347
     * @return FirewallMap
348
     */
349
    public function getFirewallMap(): FirewallMap
350
    {
351
        return $this->firewallMap;
352
    }
353
354
    /**
355
     * @return EntityManagerInterface
356
     */
357
    public function getEntityManager(): EntityManagerInterface
358
    {
359
        return $this->entityManager;
360
    }
361
362
    /**
363
     * @return null|AnnotationLoader
364
     */
365
    public function getAnnotationLoader(): ?AnnotationLoader
366
    {
367
        return $this->annotationLoader;
368
    }
369
}
370