Passed
Push — master ( cb03cc...7adcbb )
by Damien
14:28
created

AbstractProvider::registerAuditingService()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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