Passed
Pull Request — master (#61)
by Chad
03:07
created

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