Passed
Push — master ( 3d5b25...1cb9ca )
by Damien
05:58
created

Configuration::getTablePrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace DH\Auditor\Provider\Doctrine;
4
5
use DH\Auditor\Provider\ConfigurationInterface;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
class Configuration implements ConfigurationInterface
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 bool
32
     */
33
    private $enabledViewer;
34
35
    /**
36
     * @var bool
37
     */
38
    private $annotationLoaded = false;
39
40
    public function __construct(array $options)
41
    {
42
        $resolver = new OptionsResolver();
43
        $this->configureOptions($resolver);
44
        $config = $resolver->resolve($options);
45
46
        $this->enabledViewer = $config['viewer'];
47
        $this->tablePrefix = $config['table_prefix'];
48
        $this->tableSuffix = $config['table_suffix'];
49
        $this->ignoredColumns = $config['ignored_columns'];
50
51
        if (isset($config['entities']) && !empty($config['entities'])) {
52
            // use entity names as array keys for easier lookup
53
            foreach ($config['entities'] as $auditedEntity => $entityOptions) {
54
                $this->entities[$auditedEntity] = $entityOptions;
55
            }
56
        }
57
    }
58
59
    public function configureOptions(OptionsResolver $resolver): void
60
    {
61
        // https://symfony.com/doc/current/components/options_resolver.html
62
        $resolver
63
            ->setDefaults([
64
                'table_prefix' => '',
65
                'table_suffix' => '_audit',
66
                'ignored_columns' => [],
67
                'entities' => [],
68
                'viewer' => true,
69
            ])
70
            ->setAllowedTypes('table_prefix', 'string')
71
            ->setAllowedTypes('table_suffix', 'string')
72
            ->setAllowedTypes('ignored_columns', 'array')
73
            ->setAllowedTypes('entities', 'array')
74
            ->setAllowedTypes('viewer', 'bool')
75
        ;
76
    }
77
78
    /**
79
     * Set the value of entities.
80
     *
81
     * This method completely overrides entities configuration
82
     * including annotation configuration
83
     *
84
     * @param array<int|string, mixed> $entities
85
     */
86
    public function setEntities(array $entities): self
87
    {
88
        $this->annotationLoaded = true;
89
        $this->entities = $entities;
90
91
        return $this;
92
    }
93
94
    /**
95
     * enable audit Controller and its routing.
96
     *
97
     * @return $this
98
     */
99
    public function enableViewer(): self
100
    {
101
        $this->enabledViewer = true;
102
103
        return $this;
104
    }
105
106
    /**
107
     * disable audit Controller and its routing.
108
     *
109
     * @return $this
110
     */
111
    public function disableViewer(): self
112
    {
113
        $this->enabledViewer = false;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Get enabled flag.
120
     */
121
    public function isViewerEnabled(): bool
122
    {
123
        return $this->enabledViewer;
124
    }
125
126
    /**
127
     * Get the value of tablePrefix.
128
     */
129
    public function getTablePrefix(): string
130
    {
131
        return $this->tablePrefix;
132
    }
133
134
    /**
135
     * Get the value of tableSuffix.
136
     */
137
    public function getTableSuffix(): string
138
    {
139
        return $this->tableSuffix;
140
    }
141
142
    /**
143
     * Get the value of excludedColumns.
144
     *
145
     * @return array<string>
146
     */
147
    public function getIgnoredColumns(): array
148
    {
149
        return $this->ignoredColumns;
150
    }
151
152
    /**
153
     * Get the value of entities.
154
     */
155
    public function getEntities(): array
156
    {
157
        return $this->entities;
158
    }
159
160
    /**
161
     * Enables auditing for a specific entity.
162
     *
163
     * @param string $entity Entity class name
164
     *
165
     * @return $this
166
     */
167
    public function enableAuditFor(string $entity): self
168
    {
169
        if (isset($this->getEntities()[$entity])) {
170
            $this->entities[$entity]['enabled'] = true;
171
        }
172
173
        return $this;
174
    }
175
176
    /**
177
     * Disables auditing for a specific entity.
178
     *
179
     * @param string $entity Entity class name
180
     *
181
     * @return $this
182
     */
183
    public function disableAuditFor(string $entity): self
184
    {
185
        if (isset($this->getEntities()[$entity])) {
186
            $this->entities[$entity]['enabled'] = false;
187
        }
188
189
        return $this;
190
    }
191
}
192