AbstractProvider::getAuditor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor\Provider;
6
7
use DH\Auditor\Auditor;
8
use DH\Auditor\Exception\ProviderException;
9
use DH\Auditor\Provider\Service\AuditingServiceInterface;
10
use DH\Auditor\Provider\Service\StorageServiceInterface;
11
12
abstract class AbstractProvider implements ProviderInterface
13
{
14
    protected ?Auditor $auditor = null;
15
16
    protected ConfigurationInterface $configuration;
17
18
    /**
19
     * @var StorageServiceInterface[]
20
     */
21
    protected array $storageServices = [];
22
23
    /**
24
     * @var AuditingServiceInterface[]
25
     */
26
    protected array $auditingServices = [];
27
28
    public function getConfiguration(): ConfigurationInterface
29
    {
30
        return $this->configuration;
31
    }
32
33
    public function setAuditor(Auditor $auditor): ProviderInterface
34
    {
35
        $this->auditor = $auditor;
36
37
        return $this;
38
    }
39
40
    public function getAuditor(): Auditor
41
    {
42
        if (!$this->auditor instanceof \DH\Auditor\Auditor) {
43
            throw new ProviderException('This provider has not been registered.');
44
        }
45
46
        return $this->auditor;
47
    }
48
49
    public function isRegistered(): bool
50
    {
51
        return $this->auditor instanceof \DH\Auditor\Auditor;
52
    }
53
54
    public function registerStorageService(StorageServiceInterface $service): ProviderInterface
55
    {
56
        if (!$this->supportsStorage()) {
57
            throw new ProviderException('This provider does not provide storage services.');
58
        }
59
60
        if (\array_key_exists($service->getName(), $this->storageServices)) {
61
            throw new ProviderException(sprintf('A storage service named "%s" is already registered.', $service->getName()));
62
        }
63
64
        $this->storageServices[$service->getName()] = $service;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @return StorageServiceInterface[]
71
     */
72
    public function getStorageServices(): array
73
    {
74
        return $this->storageServices;
75
    }
76
77
    public function registerAuditingService(AuditingServiceInterface $service): ProviderInterface
78
    {
79
        if (!$this->supportsAuditing()) {
80
            throw new ProviderException('This provider does not provide auditing services.');
81
        }
82
83
        if (\array_key_exists($service->getName(), $this->auditingServices)) {
84
            throw new ProviderException(sprintf('An auditing service named "%s" is already registered.', $service->getName()));
85
        }
86
87
        $this->auditingServices[$service->getName()] = $service;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return AuditingServiceInterface[]
94
     */
95
    public function getAuditingServices(): array
96
    {
97
        return $this->auditingServices;
98
    }
99
}
100