SetupMerger::overrideResetInMemoryCache()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap\Setup;
6
7
use Gacela\Framework\Bootstrap\SetupGacela;
8
use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher;
9
10
/**
11
 * Merges two SetupGacela instances together with conditional logic based on change tracking.
12
 *
13
 * @psalm-suppress MixedArgumentTypeCoercion
14
 */
15
final class SetupMerger
16
{
17
    public function __construct(
18
        private readonly SetupGacela $original,
19
    ) {
20
    }
21
22
    public function merge(SetupGacela $other): SetupGacela
23
    {
24
        $this->overrideResetInMemoryCache($other);
25
        $this->overrideFileCacheSettings($other);
26
27
        $this->mergeExternalServices($other);
28
        $this->mergeProjectNamespaces($other);
29
        $this->mergeConfigKeyValues($other);
30
        $this->mergeEventDispatcher($other);
31
        $this->mergeServicesToExtend($other);
32
        $this->mergeFactories($other);
33
        $this->mergeProtectedServices($other);
34
        $this->mergeAliases($other);
35
        $this->mergeContextualBindings($other);
36
        $this->mergePlugins($other);
37
        $this->mergeGacelaConfigsToExtend($other);
38
39
        return $this->original;
40
    }
41
42
    private function overrideResetInMemoryCache(SetupGacela $other): void
43
    {
44
        if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) {
45
            $this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache());
46
        }
47
    }
48
49
    private function overrideFileCacheSettings(SetupGacela $other): void
50
    {
51
        if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) {
52
            $this->original->setFileCacheEnabled($other->isFileCacheEnabled());
53
        }
54
55
        if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) {
56
            $this->original->setFileCacheDirectory($other->getFileCacheDirectory());
57
        }
58
    }
59
60
    private function mergeExternalServices(SetupGacela $other): void
61
    {
62
        if ($other->isPropertyChanged(SetupGacela::externalServices)) {
63
            $this->original->mergeExternalServices($other->externalServices());
64
        }
65
    }
66
67
    private function mergeProjectNamespaces(SetupGacela $other): void
68
    {
69
        if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) {
70
            $this->original->mergeProjectNamespaces($other->getProjectNamespaces());
71
        }
72
    }
73
74
    private function mergeConfigKeyValues(SetupGacela $other): void
75
    {
76
        if ($other->isPropertyChanged(SetupGacela::configKeyValues)) {
77
            $this->original->mergeConfigKeyValues($other->getConfigKeyValues());
78
        }
79
    }
80
81
    private function mergeEventDispatcher(SetupGacela $other): void
82
    {
83
        if ($other->canCreateEventDispatcher()) {
84
            if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) {
85
                $eventDispatcher = $this->original->getEventDispatcher();
86
            } else {
87
                $eventDispatcher = new ConfigurableEventDispatcher();
88
            }
89
90
            /** @var ConfigurableEventDispatcher $eventDispatcher */
91
            $eventDispatcher->registerGenericListeners((array)$other->getGenericListeners());
92
93
            foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) {
94
                foreach ($listeners as $callable) {
95
                    $eventDispatcher->registerSpecificListener($event, $callable);
96
                }
97
            }
98
        } else {
99
            $eventDispatcher = $this->original->getEventDispatcher();
100
        }
101
102
        $this->original->setEventDispatcher($eventDispatcher);
103
    }
104
105
    private function mergeServicesToExtend(SetupGacela $other): void
106
    {
107
        if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) {
108
            foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) {
109
                $this->original->addServicesToExtend($serviceId, $otherServiceToExtend);
110
            }
111
        }
112
    }
113
114
    private function mergePlugins(SetupGacela $other): void
115
    {
116
        if ($other->isPropertyChanged(SetupGacela::plugins)) {
117
            $this->original->mergePlugins($other->getPlugins());
118
        }
119
    }
120
121
    private function mergeGacelaConfigsToExtend(SetupGacela $other): void
122
    {
123
        if ($other->isPropertyChanged(SetupGacela::gacelaConfigsToExtend)) {
124
            $this->original->mergeGacelaConfigsToExtend($other->getGacelaConfigsToExtend());
125
        }
126
    }
127
128
    private function mergeFactories(SetupGacela $other): void
129
    {
130
        if ($other->isPropertyChanged(SetupGacela::factories)) {
131
            $this->original->mergeFactories($other->getFactories());
132
        }
133
    }
134
135
    private function mergeProtectedServices(SetupGacela $other): void
136
    {
137
        if ($other->isPropertyChanged(SetupGacela::protectedServices)) {
138
            $this->original->mergeProtectedServices($other->getProtectedServices());
139
        }
140
    }
141
142
    private function mergeAliases(SetupGacela $other): void
143
    {
144
        if ($other->isPropertyChanged(SetupGacela::aliases)) {
145
            $this->original->mergeAliases($other->getAliases());
146
        }
147
    }
148
149
    private function mergeContextualBindings(SetupGacela $other): void
150
    {
151
        if ($other->isPropertyChanged(SetupGacela::contextualBindings)) {
152
            $this->original->mergeContextualBindings($other->getContextualBindings());
153
        }
154
    }
155
}
156