SetupMerger   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 130
rs 9.92
c 0
b 0
f 0
wmc 31

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A overrideFileCacheSettings() 0 8 3
A mergeConfigKeyValues() 0 4 2
A mergeServicesToExtend() 0 5 3
A mergeExternalServices() 0 4 2
A mergeProtectedServices() 0 4 2
A overrideResetInMemoryCache() 0 4 2
A mergeProjectNamespaces() 0 4 2
A mergeEventDispatcher() 0 22 5
A merge() 0 17 1
A mergeFactories() 0 4 2
A mergeGacelaConfigsToExtend() 0 4 2
A mergeAliases() 0 4 2
A mergePlugins() 0 4 2
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->mergePlugins($other);
36
        $this->mergeGacelaConfigsToExtend($other);
37
38
        return $this->original;
39
    }
40
41
    private function overrideResetInMemoryCache(SetupGacela $other): void
42
    {
43
        if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) {
44
            $this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache());
45
        }
46
    }
47
48
    private function overrideFileCacheSettings(SetupGacela $other): void
49
    {
50
        if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) {
51
            $this->original->setFileCacheEnabled($other->isFileCacheEnabled());
52
        }
53
54
        if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) {
55
            $this->original->setFileCacheDirectory($other->getFileCacheDirectory());
56
        }
57
    }
58
59
    private function mergeExternalServices(SetupGacela $other): void
60
    {
61
        if ($other->isPropertyChanged(SetupGacela::externalServices)) {
62
            $this->original->mergeExternalServices($other->externalServices());
63
        }
64
    }
65
66
    private function mergeProjectNamespaces(SetupGacela $other): void
67
    {
68
        if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) {
69
            $this->original->mergeProjectNamespaces($other->getProjectNamespaces());
70
        }
71
    }
72
73
    private function mergeConfigKeyValues(SetupGacela $other): void
74
    {
75
        if ($other->isPropertyChanged(SetupGacela::configKeyValues)) {
76
            $this->original->mergeConfigKeyValues($other->getConfigKeyValues());
77
        }
78
    }
79
80
    private function mergeEventDispatcher(SetupGacela $other): void
81
    {
82
        if ($other->canCreateEventDispatcher()) {
83
            if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) {
84
                $eventDispatcher = $this->original->getEventDispatcher();
85
            } else {
86
                $eventDispatcher = new ConfigurableEventDispatcher();
87
            }
88
89
            /** @var ConfigurableEventDispatcher $eventDispatcher */
90
            $eventDispatcher->registerGenericListeners((array)$other->getGenericListeners());
91
92
            foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) {
93
                foreach ($listeners as $callable) {
94
                    $eventDispatcher->registerSpecificListener($event, $callable);
95
                }
96
            }
97
        } else {
98
            $eventDispatcher = $this->original->getEventDispatcher();
99
        }
100
101
        $this->original->setEventDispatcher($eventDispatcher);
102
    }
103
104
    private function mergeServicesToExtend(SetupGacela $other): void
105
    {
106
        if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) {
107
            foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) {
108
                $this->original->addServicesToExtend($serviceId, $otherServiceToExtend);
109
            }
110
        }
111
    }
112
113
    private function mergePlugins(SetupGacela $other): void
114
    {
115
        if ($other->isPropertyChanged(SetupGacela::plugins)) {
116
            $this->original->mergePlugins($other->getPlugins());
117
        }
118
    }
119
120
    private function mergeGacelaConfigsToExtend(SetupGacela $other): void
121
    {
122
        if ($other->isPropertyChanged(SetupGacela::gacelaConfigsToExtend)) {
123
            $this->original->mergeGacelaConfigsToExtend($other->getGacelaConfigsToExtend());
124
        }
125
    }
126
127
    private function mergeFactories(SetupGacela $other): void
128
    {
129
        if ($other->isPropertyChanged(SetupGacela::factories)) {
130
            $this->original->mergeFactories($other->getFactories());
131
        }
132
    }
133
134
    private function mergeProtectedServices(SetupGacela $other): void
135
    {
136
        if ($other->isPropertyChanged(SetupGacela::protectedServices)) {
137
            $this->original->mergeProtectedServices($other->getProtectedServices());
138
        }
139
    }
140
141
    private function mergeAliases(SetupGacela $other): void
142
    {
143
        if ($other->isPropertyChanged(SetupGacela::aliases)) {
144
            $this->original->mergeAliases($other->getAliases());
145
        }
146
    }
147
}
148