Passed
Branch issue/#5 (8da8d0)
by Koldo
05:30
created

ConfigTranslator::checkCacheConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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