Passed
Pull Request — master (#48)
by Damien
02:45
created

AuditConfiguration::isAudited()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 22
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace DH\DoctrineAuditBundle;
4
5
use DH\DoctrineAuditBundle\User\UserProviderInterface;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
class AuditConfiguration
9
{
10
    /**
11
     * @var string
12
     */
13
    private $tablePrefix;
14
15
    /**
16
     * @var string
17
     */
18
    private $tableSuffix;
19
20
    /**
21
     * @var array
22
     */
23
    private $ignoredColumns = [];
24
25
    /**
26
     * @var array
27
     */
28
    private $entities = [];
29
30
    /**
31
     * @var UserProviderInterface
32
     */
33
    protected $userProvider;
34
35
    /**
36
     * @var RequestStack
37
     */
38
    protected $requestStack;
39
40
    public function __construct(array $config, UserProviderInterface $userProvider, RequestStack $requestStack)
41
    {
42
        $this->userProvider = $userProvider;
43
        $this->requestStack = $requestStack;
44
45
        $this->tablePrefix = $config['table_prefix'];
46
        $this->tableSuffix = $config['table_suffix'];
47
        $this->ignoredColumns = $config['ignored_columns'];
48
49
        if (isset($config['entities']) && !empty($config['entities'])) {
50
            // use entity names as array keys for easier lookup
51
            foreach ($config['entities'] as $auditedEntity => $entityOptions) {
52
                $this->entities[$auditedEntity] = $entityOptions;
53
            }
54
        }
55
    }
56
57
    /**
58
     * Set the value of entities.
59
     *
60
     * @param array $entities
61
     */
62
    public function setEntities(array $entities): void
63
    {
64
        $this->entities = $entities;
65
    }
66
67
    /**
68
     * Returns true if $entity is audited.
69
     *
70
     * @param object|string $entity
71
     *
72
     * @return bool
73
     */
74
    public function isAudited($entity): bool
75
    {
76
        $class = DoctrineHelper::getRealClass($entity);
77
78
        // is $entity part of audited entities?
79
        if (!array_key_exists($class, $this->entities)) {
80
            // no => $entity is not audited
81
            return false;
82
        }
83
84
        $entityOptions = $this->entities[$class];
85
86
        if (null === $entityOptions) {
87
            // no option defined => $entity is audited
88
            return true;
89
        }
90
91
        if (isset($entityOptions['enabled'])) {
92
            return (bool) $entityOptions['enabled'];
93
        }
94
95
        return true;
96
    }
97
98
    /**
99
     * Returns true if $field is audited.
100
     *
101
     * @param object|string $entity
102
     * @param string        $field
103
     *
104
     * @return bool
105
     */
106
    public function isAuditedField($entity, string $field): bool
107
    {
108
        // is $field is part of globally ignored columns?
109
        if (\in_array($field, $this->ignoredColumns, true)) {
110
            // yes => $field is not audited
111
            return false;
112
        }
113
114
        // is $entity audited?
115
        if (!$this->isAudited($entity)) {
116
            // no => $field is not audited
117
            return false;
118
        }
119
120
        $class = DoctrineHelper::getRealClass($entity);
121
        $entityOptions = $this->entities[$class];
122
123
        if (null === $entityOptions) {
124
            // no option defined => $field is audited
125
            return true;
126
        }
127
128
        // are columns excluded and is field part of them?
129
        if (isset($entityOptions['ignored_columns']) &&
130
            \in_array($field, $entityOptions['ignored_columns'], true)) {
131
            // yes => $field is not audited
132
            return false;
133
        }
134
135
        return true;
136
    }
137
138
    /**
139
     * Get the value of tablePrefix.
140
     *
141
     * @return string
142
     */
143
    public function getTablePrefix(): string
144
    {
145
        return $this->tablePrefix;
146
    }
147
148
    /**
149
     * Get the value of tableSuffix.
150
     *
151
     * @return string
152
     */
153
    public function getTableSuffix(): string
154
    {
155
        return $this->tableSuffix;
156
    }
157
158
    /**
159
     * Get the value of excludedColumns.
160
     *
161
     * @return array
162
     */
163
    public function getIgnoredColumns(): array
164
    {
165
        return $this->ignoredColumns;
166
    }
167
168
    /**
169
     * Get the value of entities.
170
     *
171
     * @return array
172
     */
173
    public function getEntities(): array
174
    {
175
        return $this->entities;
176
    }
177
178
    /**
179
     * Enables auditing for a specific entity.
180
     *
181
     * @param string $entity Entity class name
182
     *
183
     * @return $this
184
     */
185
    public function enableAuditFor(string $entity): self
186
    {
187
        if (isset($this->entities[$entity])) {
188
            $this->entities[$entity]['enabled'] = true;
189
        }
190
191
        return $this;
192
    }
193
194
    /**
195
     * Disables auditing for a specific entity.
196
     *
197
     * @param string $entity Entity class name
198
     *
199
     * @return $this
200
     */
201
    public function disableAuditFor(string $entity): self
202
    {
203
        if (isset($this->entities[$entity])) {
204
            $this->entities[$entity]['enabled'] = false;
205
        }
206
207
        return $this;
208
    }
209
210
    /**
211
     * Get the value of userProvider.
212
     *
213
     * @return UserProviderInterface
214
     */
215
    public function getUserProvider(): ?UserProviderInterface
216
    {
217
        return $this->userProvider;
218
    }
219
220
    /**
221
     * Get the value of requestStack.
222
     *
223
     * @return RequestStack
224
     */
225
    public function getRequestStack(): RequestStack
226
    {
227
        return $this->requestStack;
228
    }
229
}
230