Passed
Push — main ( 0c23a7...251707 )
by Chema
03:52
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->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
48 1
        if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) {
49
            $this->original->setFileCacheDirectory($other->getFileCacheDirectory());
50
        }
51
    }
52 29
53
    private function combineExternalServices(SetupGacela $other): void
54 29
    {
55 29
        if ($other->isPropertyChanged(SetupGacela::externalServices)) {
56
            $this->original->combineExternalServices($other->externalServices());
57
        }
58
    }
59 29
60
    private function combineProjectNamespaces(SetupGacela $other): void
61 29
    {
62 1
        if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) {
63
            $this->original->combineProjectNamespaces($other->getProjectNamespaces());
64
        }
65
    }
66 29
67
    private function combineConfigKeyValues(SetupGacela $other): void
68 29
    {
69 1
        if ($other->isPropertyChanged(SetupGacela::configKeyValues)) {
70
            $this->original->combineConfigKeyValues($other->getConfigKeyValues());
71
        }
72
    }
73 29
74
    private function combineEventDispatcher(SetupGacela $other): void
75 29
    {
76 3
        if ($other->canCreateEventDispatcher()) {
77 1
            if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) {
78
                $eventDispatcher = $this->original->getEventDispatcher();
79 2
            } else {
80
                $eventDispatcher = new ConfigurableEventDispatcher();
81
            }
82 3
83
            /** @var ConfigurableEventDispatcher $eventDispatcher */
84 3
            $eventDispatcher->registerGenericListeners((array)$other->getGenericListeners());
85 2
86 2
            foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) {
87
                foreach ($listeners as $callable) {
88
                    $eventDispatcher->registerSpecificListener($event, $callable);
89
                }
90 26
            }
91
        } else {
92 29
            $eventDispatcher = $this->original->getEventDispatcher();
93
        }
94
95 29
        $this->original->setEventDispatcher($eventDispatcher);
96
    }
97 29
98 29
    private function combineServicesToExtend(SetupGacela $other): void
99 1
    {
100
        if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) {
101
            foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) {
102
                $this->original->addServicesToExtend($serviceId, $otherServiceToExtend);
103
            }
104 29
        }
105
    }
106 29
107 1
    private function combinePlugins(SetupGacela $other): void
108
    {
109
        if ($other->isPropertyChanged(SetupGacela::plugins)) {
110
            $this->original->combinePlugins($other->getPlugins());
111 29
        }
112
    }
113 29
114 1
    private function combineGacelaConfigsToExtend(SetupGacela $other): void
115
    {
116
        if ($other->isPropertyChanged(SetupGacela::gacelaConfigsToExtend)) {
117
            $this->original->combineGacelaConfigsToExtend($other->getGacelaConfigsToExtend());
118
        }
119
    }
120
}
121