Passed
Push — bugfix/support-windows ( 0e99e7...b5ecf8 )
by Jesús
06:33 queued 02:55
created

SetupCombinator::combineServicesToExtend()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap;
6
7
use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher;
8
use Gacela\Framework\Event\Dispatcher\NullEventDispatcher;
9
10
/**
11
 * @psalm-suppress MixedArgumentTypeCoercion
12
 */
13
final class SetupCombinator
14
{
15 27
    public function __construct(
16
        private SetupGacela $original,
17
    ) {
18 27
    }
19
20 27
    public function combine(SetupGacela $other): SetupGacela
21
    {
22 27
        $this->overrideResetInMemoryCache($other);
23 27
        $this->overrideFileCacheSettings($other);
24
25 27
        $this->combineExternalServices($other);
26 27
        $this->combineProjectNamespaces($other);
27 27
        $this->combineConfigKeyValues($other);
28 27
        $this->combineEventDispatcher($other);
29 27
        $this->combineServicesToExtend($other);
30 27
        $this->combinePlugins($other);
31 27
        $this->combineGacelaConfigsToExtend($other);
32
33 27
        return $this->original;
34
    }
35
36 27
    private function overrideResetInMemoryCache(SetupGacela $other): void
37
    {
38 27
        if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) {
39 1
            $this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache());
40
        }
41
    }
42
43 27
    private function overrideFileCacheSettings(SetupGacela $other): void
44
    {
45 27
        if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) {
46 1
            $this->original->setFileCacheEnabled($other->isFileCacheEnabled());
47
        }
48 27
        if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) {
49 1
            $this->original->setFileCacheDirectory($other->getFileCacheDirectory());
50
        }
51
    }
52
53 27
    private function combineExternalServices(SetupGacela $other): void
54
    {
55 27
        if ($other->isPropertyChanged(SetupGacela::externalServices)) {
56 27
            $this->original->combineExternalServices($other->externalServices());
57
        }
58
    }
59
60 27
    private function combineProjectNamespaces(SetupGacela $other): void
61
    {
62 27
        if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) {
63 1
            $this->original->combineProjectNamespaces($other->getProjectNamespaces());
64
        }
65
    }
66
67 27
    private function combineConfigKeyValues(SetupGacela $other): void
68
    {
69 27
        if ($other->isPropertyChanged(SetupGacela::configKeyValues)) {
70 1
            $this->original->combineConfigKeyValues($other->getConfigKeyValues());
71
        }
72
    }
73
74 27
    private function combineEventDispatcher(SetupGacela $other): void
75
    {
76 27
        if ($other->canCreateEventDispatcher()) {
77 3
            if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) {
78 1
                $eventDispatcher = $this->original->getEventDispatcher();
79
            } else {
80 2
                $eventDispatcher = new ConfigurableEventDispatcher();
81
            }
82
            /** @var ConfigurableEventDispatcher $eventDispatcher */
83 3
            $eventDispatcher->registerGenericListeners((array)$other->getGenericListeners());
84
85 3
            foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) {
86 2
                foreach ($listeners as $callable) {
87 2
                    $eventDispatcher->registerSpecificListener($event, $callable);
88
                }
89
            }
90
        } else {
91 24
            $eventDispatcher = new NullEventDispatcher();
92
        }
93 27
        $this->original->setEventDispatcher($eventDispatcher);
94
    }
95
96 27
    private function combineServicesToExtend(SetupGacela $other): void
97
    {
98 27
        if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) {
99 27
            foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) {
100 1
                $this->original->addServicesToExtend($serviceId, $otherServiceToExtend);
101
            }
102
        }
103
    }
104
105 27
    private function combinePlugins(SetupGacela $other): void
106
    {
107 27
        if ($other->isPropertyChanged(SetupGacela::plugins)) {
108 1
            $this->original->combinePlugins($other->getPlugins());
109
        }
110
    }
111
112 27
    private function combineGacelaConfigsToExtend(SetupGacela $other): void
113
    {
114 27
        if ($other->isPropertyChanged(SetupGacela::gacelaConfigsToExtend)) {
115
            $this->original->combineGacelaConfigsToExtend($other->getGacelaConfigsToExtend());
116
        }
117
    }
118
}
119