Passed
Push — master ( 40a4c6...6a0114 )
by Koldo
02:21
created

ConfigAggregator::cacheConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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 4
    public function __construct(array $providers = [], $cachedConfigFile = null, array $postProcessors = [])
43
    {
44 4
        if (is_string($cachedConfigFile)) {
45 2
            if (file_exists($cachedConfigFile)) {
46 1
                $this->config = include $cachedConfigFile;
47 1
                return;
48
            }
49 2
            parent::__construct($providers);
50 2
            $this->checkCacheConfig($cachedConfigFile);
51
        } else {
52 2
            parent::__construct($providers, $cachedConfigFile, $postProcessors);
53 2
            $this->config = $this->mergeConfig(parent::getMergedConfig());
54
        }
55 4
    }
56
57 4
    public function getMergedConfig(): array
58
    {
59 4
        return $this->config;
60
    }
61
62 2
    private function checkCacheConfig(string $cachedConfigFile): void
63
    {
64 2
        $this->config = $this->mergeConfig(parent::getMergedConfig());
65
66 2
        $this->cacheConfig(
67 2
            $this->config,
68 2
            $cachedConfigFile
69
        );
70 2
    }
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 1
        ) ?? [];
82
83 1
        return $config;
84
    }
85
86 2
    private function cacheConfig(array $config, string $cachedConfigFile): void
87
    {
88 2
        if (true === $config['config_cache_enabled']) {
89 2
            file_put_contents($cachedConfigFile, sprintf(
90 2
                self::CACHE_TEMPLATE,
91 2
                get_class($this),
92 2
                date('c'),
93 2
                var_export($config, true)
94
            ));
95
        }
96 2
    }
97
98 4
    private function mergeConfig(array $config): array
99
    {
100 4
        if (false === empty($config['parameters'])) {
101 1
            $config['parameters'] = json_decode(
102 1
                str_replace(
103 1
                    '%date%',
104 1
                    (new DateTimeImmutable())->format('Y-m-d'),
105 1
                    json_encode($config['parameters']) ?: ''
106
                ),
107 1
                true
108
            );
109 1
            $config = array_replace_recursive($config, $config['parameters'] ?? []);
110 1
            unset($config['parameters']);
111
        }
112
113 4
        if (false === empty($config['services'])) {
114 1
            $config = $this->parse($config);
115 1
            unset($config['services']);
116
        }
117
118 4
        return $config;
119
    }
120
}
121