PersistenceDefinition   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 68
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
createConfigurator() 0 1 ?
formalize() 0 1 ?
A supports() 0 10 2
A hasSupportFor() 0 4 1
A applyFor() 0 5 1
A getSupportedStorages() 0 8 2
1
<?php
2
namespace PSB\Core\Persistence;
3
4
5
use PSB\Core\Exception\InvalidArgumentException;
6
use PSB\Core\Util\Settings;
7
8
abstract class PersistenceDefinition
9
{
10
    private $storageTypeToInitializerMap = [];
11
12
    /**
13
     * Creates the PersistenceConfigurator specific to the transport implementation.
14
     *
15
     * @param Settings $settings
16
     *
17
     * @return PersistenceConfigurator
18
     */
19
    abstract public function createConfigurator(Settings $settings);
20
21
    /**
22
     * This is where subclasses declare what storage types they support together with
23
     * the initializers for those types.
24
     */
25
    abstract public function formalize();
26
27
    /**
28
     * @param StorageType $storageType
29
     * @param callable    $storageTypeInitializer
30
     */
31 7
    protected function supports(StorageType $storageType, callable $storageTypeInitializer)
32
    {
33 7
        if (isset($this->storageTypeToInitializerMap[$storageType->getValue()])) {
34 1
            throw new InvalidArgumentException(
35 1
                "Storage type initializer for type '{$storageType->getValue()}' is already defined."
36
            );
37
        }
38
39 7
        $this->storageTypeToInitializerMap[$storageType->getValue()] = $storageTypeInitializer;
40 7
    }
41
42
    /**
43
     * @param StorageType $storageType
44
     *
45
     * @return bool
46
     */
47 5
    public function hasSupportFor(StorageType $storageType)
48
    {
49 5
        return isset($this->storageTypeToInitializerMap[$storageType->getValue()]);
50
    }
51
52
    /**
53
     * @param StorageType $storageType
54
     * @param Settings    $settings
55
     */
56 2
    public function applyFor(StorageType $storageType, Settings $settings)
57
    {
58 2
        $storageTypeInitializer = $this->storageTypeToInitializerMap[$storageType->getValue()];
59 2
        $storageTypeInitializer($settings);
60 2
    }
61
62
    /**
63
     * @param StorageType|null $storageType
64
     *
65
     * @return array
66
     */
67 3
    public function getSupportedStorages(StorageType $storageType = null)
68
    {
69 3
        if ($storageType) {
70 2
            return [$storageType->getValue()];
71
        }
72
73 1
        return array_keys($this->storageTypeToInitializerMap);
74
    }
75
}
76