Passed
Push — add-with-all-to-modules-list ( 82b81d...0edb38 )
by Chema
03:40
created

SetupCombinator   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 103
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0
wmc 25

11 Methods

Rating   Name   Duplication   Size   Complexity  
A combine() 0 14 1
A overrideFileCacheSettings() 0 7 3
A combineExternalServices() 0 4 2
A combinePlugins() 0 4 2
A overrideResetInMemoryCache() 0 4 2
A combineEventDispatcher() 0 20 5
A combineProjectNamespaces() 0 4 2
A combineConfigKeyValues() 0 4 2
A combineServicesToExtend() 0 5 3
A __construct() 0 3 1
A combineGacelaConfigsToExtend() 0 4 2
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 29
    public function __construct(
15
        private SetupGacela $original,
16
    ) {
17 29
    }
18
19 29
    public function combine(SetupGacela $other): SetupGacela
20
    {
21 29
        $this->overrideResetInMemoryCache($other);
22 29
        $this->overrideFileCacheSettings($other);
23
24 29
        $this->combineExternalServices($other);
25 29
        $this->combineProjectNamespaces($other);
26 29
        $this->combineConfigKeyValues($other);
27 29
        $this->combineEventDispatcher($other);
28 29
        $this->combineServicesToExtend($other);
29 29
        $this->combinePlugins($other);
30 29
        $this->combineGacelaConfigsToExtend($other);
31
32 29
        return $this->original;
33
    }
34
35 29
    private function overrideResetInMemoryCache(SetupGacela $other): void
36
    {
37 29
        if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) {
38 1
            $this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache());
39
        }
40
    }
41
42 29
    private function overrideFileCacheSettings(SetupGacela $other): void
43
    {
44 29
        if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) {
45 2
            $this->original->setFileCacheEnabled($other->isFileCacheEnabled());
46
        }
47 29
        if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) {
48 1
            $this->original->setFileCacheDirectory($other->getFileCacheDirectory());
49
        }
50
    }
51
52 29
    private function combineExternalServices(SetupGacela $other): void
53
    {
54 29
        if ($other->isPropertyChanged(SetupGacela::externalServices)) {
55 29
            $this->original->combineExternalServices($other->externalServices());
56
        }
57
    }
58
59 29
    private function combineProjectNamespaces(SetupGacela $other): void
60
    {
61 29
        if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) {
62 1
            $this->original->combineProjectNamespaces($other->getProjectNamespaces());
63
        }
64
    }
65
66 29
    private function combineConfigKeyValues(SetupGacela $other): void
67
    {
68 29
        if ($other->isPropertyChanged(SetupGacela::configKeyValues)) {
69 1
            $this->original->combineConfigKeyValues($other->getConfigKeyValues());
70
        }
71
    }
72
73 29
    private function combineEventDispatcher(SetupGacela $other): void
74
    {
75 29
        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 26
            $eventDispatcher = $this->original->getEventDispatcher();
91
        }
92 29
        $this->original->setEventDispatcher($eventDispatcher);
93
    }
94
95 29
    private function combineServicesToExtend(SetupGacela $other): void
96
    {
97 29
        if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) {
98 29
            foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) {
99 1
                $this->original->addServicesToExtend($serviceId, $otherServiceToExtend);
100
            }
101
        }
102
    }
103
104 29
    private function combinePlugins(SetupGacela $other): void
105
    {
106 29
        if ($other->isPropertyChanged(SetupGacela::plugins)) {
107 1
            $this->original->combinePlugins($other->getPlugins());
108
        }
109
    }
110
111 29
    private function combineGacelaConfigsToExtend(SetupGacela $other): void
112
    {
113 29
        if ($other->isPropertyChanged(SetupGacela::gacelaConfigsToExtend)) {
114 1
            $this->original->combineGacelaConfigsToExtend($other->getGacelaConfigsToExtend());
115
        }
116
    }
117
}
118