Passed
Push — main ( 1a17f8...8155a4 )
by Chema
10:50 queued 09:35
created

SetupCombinator::combineBindings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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 readonly 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->combineBindings($other);
25 29
        $this->combineExternalServices($other);
26 29
        $this->combineProjectNamespaces($other);
27 29
        $this->combineConfigKeyValues($other);
28 29
        $this->combineEventDispatcher($other);
29 29
        $this->combineServicesToExtend($other);
30 29
        $this->combinePlugins($other);
31
        $this->combineGacelaConfigsToExtend($other);
32 29
33
        return $this->original;
34
    }
35 29
36
    private function overrideResetInMemoryCache(SetupGacela $other): void
37 29
    {
38 1
        if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) {
39
            $this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache());
40
        }
41
    }
42 29
43
    private function overrideFileCacheSettings(SetupGacela $other): void
44 29
    {
45 2
        if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) {
46
            $this->original->setFileCacheEnabled($other->isFileCacheEnabled());
47 29
        }
48 1
49
        if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) {
50
            $this->original->setFileCacheDirectory($other->getFileCacheDirectory());
51
        }
52 29
    }
53
54 29
    private function combineExternalServices(SetupGacela $other): void
55 29
    {
56
        if ($other->isPropertyChanged(SetupGacela::externalServices)) {
57
            $this->original->combineExternalServices($other->externalServices());
58
        }
59 29
    }
60
61 29
    private function combineProjectNamespaces(SetupGacela $other): void
62 1
    {
63
        if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) {
64
            $this->original->combineProjectNamespaces($other->getProjectNamespaces());
65
        }
66 29
    }
67
68 29
    private function combineConfigKeyValues(SetupGacela $other): void
69 1
    {
70
        if ($other->isPropertyChanged(SetupGacela::configKeyValues)) {
71
            $this->original->combineConfigKeyValues($other->getConfigKeyValues());
72
        }
73 29
    }
74
75 29
    private function combineEventDispatcher(SetupGacela $other): void
76 3
    {
77 1
        if ($other->canCreateEventDispatcher()) {
78
            if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) {
79 2
                $eventDispatcher = $this->original->getEventDispatcher();
80
            } else {
81
                $eventDispatcher = new ConfigurableEventDispatcher();
82 3
            }
83
84 3
            /** @var ConfigurableEventDispatcher $eventDispatcher */
85 2
            $eventDispatcher->registerGenericListeners((array)$other->getGenericListeners());
86 2
87
            foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) {
88
                foreach ($listeners as $callable) {
89
                    $eventDispatcher->registerSpecificListener($event, $callable);
90 26
                }
91
            }
92 29
        } else {
93
            $eventDispatcher = $this->original->getEventDispatcher();
94
        }
95 29
96
        $this->original->setEventDispatcher($eventDispatcher);
97 29
    }
98 29
99 1
    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 29
            }
105
        }
106 29
    }
107 1
108
    private function combinePlugins(SetupGacela $other): void
109
    {
110
        if ($other->isPropertyChanged(SetupGacela::plugins)) {
111 29
            $this->original->combinePlugins($other->getPlugins());
112
        }
113 29
    }
114 1
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