PersistenceDefinitionApplier::apply()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 24
cts 24
cp 1
rs 8.3146
c 0
b 0
f 0
cc 7
nc 6
nop 1
crap 7
1
<?php
2
namespace PSB\Core\Persistence;
3
4
5
use PSB\Core\Exception\RuntimeException;
6
use PSB\Core\Exception\UnexpectedValueException;
7
use PSB\Core\KnownSettingsEnum;
8
use PSB\Core\Util\Settings;
9
10
class PersistenceDefinitionApplier
11
{
12 3
    public function apply(Settings $settings)
13
    {
14
        /** @var EnabledPersistence[]|null $enabledPersistences */
15 3
        $enabledPersistences = $settings->tryGet(KnownSettingsEnum::ENABLED_PERSISTENCES);
16
17 3
        if (!$enabledPersistences) {
18
            // TODO tweak the error message (vezi interfetele de care vb in NServicebus)
19
            // TODO vezi si de SendOnly endpoint cand lamuresti cu sending messages din afara unui handler (fara tranzactie explicita)
20 1
            throw new UnexpectedValueException(
21 1
                "No persistence has been selected, please select your persistence by calling endpointConfigurator.usePersistence."
22
            );
23
        }
24
25 2
        $enabledPersistences = array_reverse($enabledPersistences);
26 2
        $availableStorageTypeValues = array_flip(StorageType::getConstants());
27
28 2
        $supportedStorageTypeValues = [];
29
30 2
        foreach ($enabledPersistences as $enabledPersistence) {
31 2
            $currentDefinition = $enabledPersistence->getDefinition();
32 2
            $currentDefinition->formalize();
33 2
            $selectedStorageType = $enabledPersistence->getSelectedStorageType();
34
35 2
            if ($selectedStorageType && !$currentDefinition->hasSupportFor($selectedStorageType)) {
36 1
                $definitionFqcn = get_class($currentDefinition);
37 1
                throw new RuntimeException(
38 1
                    "Definition '$definitionFqcn' does not support storage type {$selectedStorageType->getValue()}."
39
                );
40
            }
41
42 1
            $currentSupportedStorageTypeValues = $currentDefinition->getSupportedStorages($selectedStorageType);
43 1
            foreach ($currentSupportedStorageTypeValues as $supportedStorageValue) {
44 1
                if (isset($availableStorageTypeValues[$supportedStorageValue])) {
45 1
                    unset($availableStorageTypeValues[$supportedStorageValue]);
46 1
                    $supportedStorageTypeValues[$supportedStorageValue] = $supportedStorageValue;
47 1
                    $currentDefinition->applyFor(new StorageType($supportedStorageValue), $settings);
48
                }
49
            }
50
        }
51
52 1
        $settings->set(KnownSettingsEnum::SUPPORTED_STORAGE_TYPE_VALUES, $supportedStorageTypeValues);
53 1
    }
54
}
55