Passed
Push — master ( 4846d2...6d951a )
by Damien
04:22 queued 12s
created

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