Passed
Pull Request — master (#78)
by
unknown
02:56
created

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