Passed
Pull Request — master (#6)
by Koldo
02:55
created

ConfigAggregator::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 2
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 as BaseAggregator;
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 is_string;
20
use function json_decode;
21
use function json_encode;
22
use function sprintf;
23
use function str_replace;
24
use function var_export;
25
26
class ConfigAggregator extends BaseAggregator
27
{
28
    public const CACHE_TEMPLATE = <<< 'EOT'
29
<?php
30
/**
31
 * This configuration cache file was generated by %s
32
 * at %s
33
 */
34
return %s;
35
36
EOT;
37
38
    private $config;
39
40
    public function __construct(array $providers = [], $cachedConfigFile = null, array $postProcessors = [])
41
    {
42
        if (is_string($cachedConfigFile)) {
43
            if (file_exists($cachedConfigFile)) {
44
                $this->config = include $cachedConfigFile;
45
                return;
46
            }
47
            parent::__construct($providers);
48
            $this->checkCacheConfig($providers, $cachedConfigFile);
49
        } else {
50
            parent::__construct($providers, $cachedConfigFile, $postProcessors);
51
            $this->config = $this->mergeConfig(parent::getMergedConfig());
52
        }
53
    }
54
55
    public function getMergedConfig(): array
56
    {
57
        return $this->config;
58
    }
59
60
    private function checkCacheConfig(array $providers, string $cachedConfigFile): void
0 ignored issues
show
Unused Code introduced by
The parameter $providers is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
    private function checkCacheConfig(/** @scrutinizer ignore-unused */ array $providers, string $cachedConfigFile): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        $this->config = $this->mergeConfig(parent::getMergedConfig());
63
64
        $this->cacheConfig(
65
            $this->config,
66
            $cachedConfigFile
67
        );
68
    }
69
70
    private function parse(array $defaultConfig): array
71
    {
72
        return array_merge_recursive(
73
            (new FactoryTranslator())->process($defaultConfig),
74
            (new ConditionalTranslator())->process($defaultConfig),
75
            (new AliasTranslator())->process($defaultConfig['services']),
76
            (new InvokableTranslator())->process($defaultConfig['services']),
77
            $defaultConfig
78
        );
79
    }
80
81
    private function cacheConfig(array $config, string $cachedConfigFile): void
82
    {
83
        file_put_contents($cachedConfigFile, sprintf(
84
            self::CACHE_TEMPLATE,
85
            get_class($this),
86
            date('c'),
87
            var_export($config, true)
88
        ));
89
    }
90
91
    private function mergeConfig(array $config): array
92
    {
93
        if (false === empty($config['parameters'])) {
94
            $config['parameters'] = json_decode(
95
                str_replace(
96
                    '%date%',
97
                    (new DateTimeImmutable())->format('Y-m-d'),
98
                    json_encode($config['parameters']) ?: ''
99
                ),
100
                true
101
            );
102
            $config = array_replace_recursive($config, $config['parameters'] ?? []);
103
            unset($config['parameters']);
104
        }
105
106
        if (false === empty($config['services'])) {
107
            $config = $this->parse($config);
108
            unset($config['services']);
109
        }
110
111
        return $config;
112
    }
113
}
114