Passed
Pull Request — master (#53)
by Damien
02:38
created

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