Passed
Branch issue/#10 (cd600f)
by Koldo
05:42
created

ConfigAggregator::getMergedConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\SymfonyConfigTranslator\Container\Config;
6
7
use Antidot\SymfonyConfigTranslator\AliasTranslator;
8
use Antidot\SymfonyConfigTranslator\ConditionalTranslator;
9
use Antidot\SymfonyConfigTranslator\FactoryTranslator;
10
use Antidot\SymfonyConfigTranslator\InvokableTranslator;
11
use Antidot\SymfonyConfigTranslator\TagTranslator;
12
use DateTimeImmutable;
13
use RuntimeException;
14
use Zend\ConfigAggregator\ConfigAggregator as BaseAggregator;
15
16
use function array_replace_recursive;
17
use function date;
18
use function file_exists;
19
use function file_put_contents;
20
use function get_class;
21
use function is_string;
22
use function json_decode;
23
use function json_encode;
24
use function sprintf;
25
use function str_replace;
26
use function var_export;
27
28
class ConfigAggregator extends BaseAggregator
29
{
30
    public const CACHE_TEMPLATE = <<< 'EOT'
31
<?php
32
/**
33
 * This configuration cache file was generated by %s
34
 * at %s
35
 */
36
return %s;
37
38
EOT;
39
40
    private $config;
41
42 2
    public function __construct(array $providers = [], $cachedConfigFile = null, array $postProcessors = [])
43
    {
44 2
        if (is_string($cachedConfigFile)) {
45
            if (file_exists($cachedConfigFile)) {
46
                $this->config = include $cachedConfigFile;
47
                return;
48
            }
49
            parent::__construct($providers);
50
            $this->checkCacheConfig($cachedConfigFile);
51
        } else {
52 2
            parent::__construct($providers, $cachedConfigFile, $postProcessors);
53 2
            $this->config = $this->mergeConfig(parent::getMergedConfig());
54
        }
55 2
    }
56
57 2
    public function getMergedConfig(): array
58
    {
59 2
        return $this->config;
60
    }
61
62
    private function checkCacheConfig(string $cachedConfigFile): void
63
    {
64
        $this->config = $this->mergeConfig(parent::getMergedConfig());
65
66
        $this->cacheConfig(
67
            $this->config,
68
            $cachedConfigFile
69
        );
70
    }
71
72 1
    private function parse(array $defaultConfig): array
73
    {
74 1
        $config = array_replace_recursive(
75 1
            (new TagTranslator())->process($defaultConfig['services']),
76 1
            (new FactoryTranslator())->process($defaultConfig),
77 1
            (new ConditionalTranslator())->process($defaultConfig),
78 1
            (new AliasTranslator())->process($defaultConfig['services']),
79 1
            (new InvokableTranslator())->process($defaultConfig['services']),
80 1
            $defaultConfig
81
        );
82
83 1
        if (empty($config)) {
84
            throw new RuntimeException('Error occurred merging configuration');
85
        }
86 1
        return $config;
87
    }
88
89
    private function cacheConfig(array $config, string $cachedConfigFile): void
90
    {
91
        if (true === $config['config_cache_enabled']) {
92
            file_put_contents($cachedConfigFile, sprintf(
93
                self::CACHE_TEMPLATE,
94
                get_class($this),
95
                date('c'),
96
                var_export($config, true)
97
            ));
98
        }
99
    }
100
101 2
    private function mergeConfig(array $config): array
102
    {
103 2
        if (false === empty($config['parameters'])) {
104 1
            $config['parameters'] = json_decode(
105 1
                str_replace(
106 1
                    '%date%',
107 1
                    (new DateTimeImmutable())->format('Y-m-d'),
108 1
                    json_encode($config['parameters']) ?: ''
109
                ),
110 1
                true
111
            );
112 1
            $config = array_replace_recursive($config, $config['parameters'] ?? []);
113 1
            unset($config['parameters']);
114
        }
115
116 2
        if (false === empty($config['services'])) {
117 1
            $config = $this->parse($config);
118 1
            unset($config['services']);
119
        }
120
121 2
        return $config;
122
    }
123
}
124