Passed
Push — feat/add-provider-bindings ( 36306e )
by Chema
04:36
created

SetupCombinator::combineBindings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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