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

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