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

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