Issues (38)

src/Auditor.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor;
6
7
use DH\Auditor\EventSubscriber\AuditEventSubscriber;
0 ignored issues
show
The type DH\Auditor\EventSubscriber\AuditEventSubscriber was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use DH\Auditor\Exception\InvalidArgumentException;
9
use DH\Auditor\Exception\ProviderException;
10
use DH\Auditor\Provider\ProviderInterface;
11
use ReflectionException;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
/**
15
 * @see \DH\Auditor\Tests\AuditorTest
16
 */
17
final class Auditor
18
{
19
    private Configuration $configuration;
20
21
    /**
22
     * @var ProviderInterface[]
23
     */
24
    private array $providers = [];
25
26
    /**
27
     * @var ProviderInterface[]
28
     */
29
    private array $storageProviders = [];
30
31
    /**
32
     * @var ProviderInterface[]
33
     */
34
    private array $auditProviders = [];
35
36
    private \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher;
37
38
    /**
39
     * @throws ReflectionException
40
     */
41
    public function __construct(Configuration $configuration, EventDispatcherInterface $dispatcher)
42
    {
43
        $this->configuration = $configuration;
44
        $this->dispatcher = $dispatcher;
45
46
        // Attach persistence event subscriber to provided dispatcher
47
        $dispatcher->addSubscriber(new AuditEventSubscriber($this));
48
    }
49
50
    public function getEventDispatcher(): EventDispatcherInterface
51
    {
52
        return $this->dispatcher;
53
    }
54
55
    public function getConfiguration(): Configuration
56
    {
57
        return $this->configuration;
58
    }
59
60
    /**
61
     * @return ProviderInterface[]
62
     */
63
    public function getProviders(): array
64
    {
65
        return $this->providers;
66
    }
67
68
    /**
69
     * @throws InvalidArgumentException
70
     */
71
    public function getProvider(string $name): ProviderInterface
72
    {
73
        if (!$this->hasProvider($name)) {
74
            throw new InvalidArgumentException(sprintf('Unknown provider "%s"', $name));
75
        }
76
77
        return $this->providers[$name];
78
    }
79
80
    public function hasProvider(string $name): bool
81
    {
82
        return \array_key_exists($name, $this->providers);
83
    }
84
85
    /**
86
     * @throws ProviderException
87
     */
88
    public function registerProvider(ProviderInterface $provider): self
89
    {
90
        if (!$provider->supportsStorage() && !$provider->supportsAuditing()) {
91
            throw new ProviderException(sprintf('Provider "%s" does not support storage and auditing.', $provider::class));
92
        }
93
94
        $this->providers[$provider::class] = $provider;
95
        $provider->setAuditor($this);
96
97
        if ($provider->supportsStorage()) {
98
            $this->enableStorage($provider);
99
        }
100
101
        if ($provider->supportsAuditing()) {
102
            $this->enableAuditing($provider);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * @throws ProviderException
110
     */
111
    public function enableStorage(ProviderInterface $provider): self
112
    {
113
        if (!$provider->supportsStorage()) {
114
            throw new ProviderException(sprintf('Provider "%s" does not support storage.', $provider::class));
115
        }
116
117
        $this->storageProviders[$provider::class] = $provider;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @throws ProviderException
124
     */
125
    public function disableStorage(ProviderInterface $provider): self
126
    {
127
        if (!$provider->supportsStorage()) {
128
            throw new ProviderException(sprintf('Provider "%s" does not support storage.', $provider::class));
129
        }
130
131
        if (1 === \count($this->storageProviders)) {
132
            throw new ProviderException('At least one storage provider must be enabled.');
133
        }
134
135
        unset($this->storageProviders[$provider::class]);
136
137
        return $this;
138
    }
139
140
    /**
141
     * @throws InvalidArgumentException
142
     */
143
    public function isStorageEnabled(ProviderInterface $provider): bool
144
    {
145
        $key = $provider::class;
146
        if (!$this->hasProvider($key)) {
147
            throw new InvalidArgumentException(sprintf('Unknown provider "%s"', $key));
148
        }
149
150
        return \array_key_exists($key, $this->storageProviders);
151
    }
152
153
    /**
154
     * @throws ProviderException
155
     */
156
    public function enableAuditing(ProviderInterface $provider): self
157
    {
158
        if (!$provider->supportsAuditing()) {
159
            throw new ProviderException(sprintf('Provider "%s" does not support audit hooks.', $provider::class));
160
        }
161
162
        $this->auditProviders[$provider::class] = $provider;
163
164
        return $this;
165
    }
166
167
    /**
168
     * @throws ProviderException
169
     */
170
    public function disableAuditing(ProviderInterface $provider): self
171
    {
172
        if (!$provider->supportsAuditing()) {
173
            throw new ProviderException(sprintf('Provider "%s" does not support audit hooks.', $provider::class));
174
        }
175
176
        if (1 === \count($this->auditProviders)) {
177
            throw new ProviderException('At least one auditing provider must be enabled.');
178
        }
179
180
        unset($this->auditProviders[$provider::class]);
181
182
        return $this;
183
    }
184
185
    /**
186
     * @throws InvalidArgumentException
187
     */
188
    public function isAuditingEnabled(ProviderInterface $provider): bool
189
    {
190
        if (!$this->hasProvider($provider::class)) {
191
            throw new InvalidArgumentException(sprintf('Unknown provider "%s"', $provider::class));
192
        }
193
194
        return \array_key_exists($provider::class, $this->auditProviders);
195
    }
196
}
197