Passed
Push — combine-plugins ( 53fd61 )
by Chema
04:20
created

SetupCombinator   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 94
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 23

10 Methods

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