Passed
Pull Request — master (#143)
by Federico
02:47
created

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