Passed
Push — bugfix/support-windows ( 517fb4...0e99e7 )
by Chema
04:47
created

SetupCombinator::combineEventDispatcher()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 3
nop 1
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 5
rs 9.5555
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 26
    public function __construct(private SetupGacela $original)
16
    {
17 26
    }
18
19 26
    public function combine(SetupGacela $other): SetupGacela
20
    {
21 26
        $this->overrideResetInMemoryCache($other);
22 26
        $this->overrideFileCacheSettings($other);
23
24 26
        $this->combineExternalServices($other);
25 26
        $this->combineProjectNamespaces($other);
26 26
        $this->combineConfigKeyValues($other);
27 26
        $this->combineEventDispatcher($other);
28 26
        $this->combineServicesToExtend($other);
29
30 26
        return $this->original;
31
    }
32
33 26
    private function overrideResetInMemoryCache(SetupGacela $other): void
34
    {
35 26
        if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) {
36 1
            $this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache());
37
        }
38
    }
39
40 26
    private function overrideFileCacheSettings(SetupGacela $other): void
41
    {
42 26
        if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) {
43 1
            $this->original->setFileCacheEnabled($other->isFileCacheEnabled());
44
        }
45 26
        if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) {
46 1
            $this->original->setFileCacheDirectory($other->getFileCacheDirectory());
47
        }
48
    }
49
50 26
    private function combineExternalServices(SetupGacela $other): void
51
    {
52 26
        if ($other->isPropertyChanged(SetupGacela::externalServices)) {
53 26
            $this->original->combineExternalServices($other->externalServices());
54
        }
55
    }
56
57 26
    private function combineProjectNamespaces(SetupGacela $other): void
58
    {
59 26
        if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) {
60 1
            $this->original->combineProjectNamespaces($other->getProjectNamespaces());
61
        }
62
    }
63
64 26
    private function combineConfigKeyValues(SetupGacela $other): void
65
    {
66 26
        if ($other->isPropertyChanged(SetupGacela::configKeyValues)) {
67 1
            $this->original->combineConfigKeyValues($other->getConfigKeyValues());
68
        }
69
    }
70
71 26
    private function combineEventDispatcher(SetupGacela $other): void
72
    {
73 26
        if ($other->canCreateEventDispatcher()) {
74 3
            if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) {
75 1
                $eventDispatcher = $this->original->getEventDispatcher();
76
            } else {
77 2
                $eventDispatcher = new ConfigurableEventDispatcher();
78
            }
79
            /** @var ConfigurableEventDispatcher $eventDispatcher */
80 3
            $eventDispatcher->registerGenericListeners((array)$other->getGenericListeners());
81
82 3
            foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) {
83 2
                foreach ($listeners as $callable) {
84 2
                    $eventDispatcher->registerSpecificListener($event, $callable);
85
                }
86
            }
87
        } else {
88 23
            $eventDispatcher = new NullEventDispatcher();
89
        }
90 26
        $this->original->setEventDispatcher($eventDispatcher);
91
    }
92
93 26
    private function combineServicesToExtend(SetupGacela $other): void
94
    {
95 26
        if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) {
96 26
            foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) {
97 1
                $this->original->addServicesToExtend($serviceId, $otherServiceToExtend);
98
            }
99
        }
100
    }
101
}
102