Passed
Push — add-extend-config ( f39ef4 )
by Chema
03:24
created

SetupCombinator::combineExtendConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap;
6
7
use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher;
8
use Gacela\Framework\Event\Dispatcher\NullEventDispatcher;
9
10
/**
11
 * @psalm-suppress MixedArgumentTypeCoercion
12
 */
13
final class SetupCombinator
14
{
15 27
    public function __construct(private SetupGacela $original)
16
    {
17 27
    }
18
19 27
    public function combine(SetupGacela $other): SetupGacela
20
    {
21 27
        $this->overrideResetInMemoryCache($other);
22 27
        $this->overrideFileCacheSettings($other);
23
24 27
        $this->combineExternalServices($other);
25 27
        $this->combineProjectNamespaces($other);
26 27
        $this->combineConfigKeyValues($other);
27 27
        $this->combineEventDispatcher($other);
28 27
        $this->combineServicesToExtend($other);
29 27
        $this->combinePlugins($other);
30 27
        $this->combineExtendConfig($other);
31
32 27
        return $this->original;
33
    }
34
35 27
    private function overrideResetInMemoryCache(SetupGacela $other): void
36
    {
37 27
        if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) {
38 1
            $this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache());
39
        }
40
    }
41
42 27
    private function overrideFileCacheSettings(SetupGacela $other): void
43
    {
44 27
        if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) {
45 1
            $this->original->setFileCacheEnabled($other->isFileCacheEnabled());
46
        }
47 27
        if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) {
48 1
            $this->original->setFileCacheDirectory($other->getFileCacheDirectory());
49
        }
50
    }
51
52 27
    private function combineExternalServices(SetupGacela $other): void
53
    {
54 27
        if ($other->isPropertyChanged(SetupGacela::externalServices)) {
55 27
            $this->original->combineExternalServices($other->externalServices());
56
        }
57
    }
58
59 27
    private function combineProjectNamespaces(SetupGacela $other): void
60
    {
61 27
        if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) {
62 1
            $this->original->combineProjectNamespaces($other->getProjectNamespaces());
63
        }
64
    }
65
66 27
    private function combineConfigKeyValues(SetupGacela $other): void
67
    {
68 27
        if ($other->isPropertyChanged(SetupGacela::configKeyValues)) {
69 1
            $this->original->combineConfigKeyValues($other->getConfigKeyValues());
70
        }
71
    }
72
73 27
    private function combineEventDispatcher(SetupGacela $other): void
74
    {
75 27
        if ($other->canCreateEventDispatcher()) {
76 3
            if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) {
77 1
                $eventDispatcher = $this->original->getEventDispatcher();
78
            } else {
79 2
                $eventDispatcher = new ConfigurableEventDispatcher();
80
            }
81
            /** @var ConfigurableEventDispatcher $eventDispatcher */
82 3
            $eventDispatcher->registerGenericListeners((array)$other->getGenericListeners());
83
84 3
            foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) {
85 2
                foreach ($listeners as $callable) {
86 2
                    $eventDispatcher->registerSpecificListener($event, $callable);
87
                }
88
            }
89
        } else {
90 24
            $eventDispatcher = new NullEventDispatcher();
91
        }
92 27
        $this->original->setEventDispatcher($eventDispatcher);
93
    }
94
95 27
    private function combineServicesToExtend(SetupGacela $other): void
96
    {
97 27
        if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) {
98 27
            foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) {
99 1
                $this->original->addServicesToExtend($serviceId, $otherServiceToExtend);
100
            }
101
        }
102
    }
103
104 27
    private function combinePlugins(SetupGacela $other): void
105
    {
106 27
        if ($other->isPropertyChanged(SetupGacela::afterPlugins)) {
107 1
            $this->original->combineAfterPlugins($other->getAfterPlugins());
108
        }
109
    }
110
111 27
    private function combineExtendConfig(SetupGacela $other): void
112
    {
113 27
        if ($other->isPropertyChanged(SetupGacela::extendConfig)) {
114
            $this->original->combineExtendConfig($other->getExtendConfig());
115
        }
116
    }
117
}
118