Passed
Pull Request — master (#61)
by Chad
02:42
created

AuditConfiguration::isAuditedField()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 34
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 6
nop 2
dl 0
loc 34
rs 8.8333
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
     * Set enabled flag.
82
     *
83
     * @param bool $enabled
84
     */
85
    public function setEnabled(bool $enabled): void
86
    {
87
        $this->enabled = $enabled;
88
    }
89
90
    /**
91
     * Returns true if $entity is audited.
92
     *
93
     * @param object|string $entity
94
     *
95
     * @return bool
96
     */
97
    public function isAuditable($entity): bool
98
    {
99
        $class = DoctrineHelper::getRealClass($entity);
100
101
        // is $entity part of audited entities?
102
        if (!array_key_exists($class, $this->entities)) {
103
            // no => $entity is not audited
104
            return false;
105
        }
106
107
        return true;
108
    }
109
110
    /**
111
     * Returns true if $entity is audited.
112
     *
113
     * @param object|string $entity
114
     *
115
     * @return bool
116
     */
117
    public function isAudited($entity): bool
118
    {
119
        if (!$this->enabled) {
120
            return false;
121
        }
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
        $entityOptions = $this->entities[$class];
132
133
        if (null === $entityOptions) {
134
            // no option defined => $entity is audited
135
            return true;
136
        }
137
138
        if (isset($entityOptions['enabled'])) {
139
            return (bool) $entityOptions['enabled'];
140
        }
141
142
        return true;
143
    }
144
145
    /**
146
     * Returns true if $field is audited.
147
     *
148
     * @param object|string $entity
149
     * @param string        $field
150
     *
151
     * @return bool
152
     */
153
    public function isAuditedField($entity, string $field): bool
154
    {
155
        if (!$this->enabled) {
156
            return false;
157
        }
158
159
        // is $field is part of globally ignored columns?
160
        if (\in_array($field, $this->ignoredColumns, true)) {
161
            // yes => $field is not audited
162
            return false;
163
        }
164
165
        // is $entity audited?
166
        if (!$this->isAudited($entity)) {
167
            // no => $field is not audited
168
            return false;
169
        }
170
171
        $class = DoctrineHelper::getRealClass($entity);
172
        $entityOptions = $this->entities[$class];
173
174
        if (null === $entityOptions) {
175
            // no option defined => $field is audited
176
            return true;
177
        }
178
179
        // are columns excluded and is field part of them?
180
        if (isset($entityOptions['ignored_columns']) &&
181
            \in_array($field, $entityOptions['ignored_columns'], true)) {
182
            // yes => $field is not audited
183
            return false;
184
        }
185
186
        return true;
187
    }
188
189
    /**
190
     * Get the value of tablePrefix.
191
     *
192
     * @return string
193
     */
194
    public function getTablePrefix(): string
195
    {
196
        return $this->tablePrefix;
197
    }
198
199
    /**
200
     * Get the value of tableSuffix.
201
     *
202
     * @return string
203
     */
204
    public function getTableSuffix(): string
205
    {
206
        return $this->tableSuffix;
207
    }
208
209
    /**
210
     * Get the value of excludedColumns.
211
     *
212
     * @return array
213
     */
214
    public function getIgnoredColumns(): array
215
    {
216
        return $this->ignoredColumns;
217
    }
218
219
    /**
220
     * Get the value of entities.
221
     *
222
     * @return array
223
     */
224
    public function getEntities(): array
225
    {
226
        return $this->entities;
227
    }
228
229
    /**
230
     * Enables auditing for a specific entity.
231
     *
232
     * @param string $entity Entity class name
233
     *
234
     * @return $this
235
     */
236
    public function enableAuditFor(string $entity): self
237
    {
238
        if (isset($this->entities[$entity])) {
239
            $this->entities[$entity]['enabled'] = true;
240
        }
241
242
        return $this;
243
    }
244
245
    /**
246
     * Disables auditing for a specific entity.
247
     *
248
     * @param string $entity Entity class name
249
     *
250
     * @return $this
251
     */
252
    public function disableAuditFor(string $entity): self
253
    {
254
        if (isset($this->entities[$entity])) {
255
            $this->entities[$entity]['enabled'] = false;
256
        }
257
258
        return $this;
259
    }
260
261
    /**
262
     * Get the value of userProvider.
263
     *
264
     * @return UserProviderInterface
265
     */
266
    public function getUserProvider(): ?UserProviderInterface
267
    {
268
        return $this->userProvider;
269
    }
270
271
    /**
272
     * Get the value of requestStack.
273
     *
274
     * @return RequestStack
275
     */
276
    public function getRequestStack(): RequestStack
277
    {
278
        return $this->requestStack;
279
    }
280
281
    /**
282
     * Gets the value of firewallMap.
283
     *
284
     * @return FirewallMap
285
     */
286
    public function getFirewallMap(): FirewallMap
287
    {
288
        return $this->firewallMap;
289
    }
290
}
291