Completed
Push — master ( 742ff7...9d59a2 )
by Nikola
09:00
created

CachingConfigLoader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phoundation\Config;
6
7
final class CachingConfigLoader implements ConfigLoader
8
{
9
    /** @var ConfigLoader */
10
    private $configLoader;
11
12
    /** @var string */
13
    private $cachedConfigFile;
14
    
15
    public function __construct(ConfigLoader $configLoader, string $cachedConfigFile)
16
    {
17
        $this->configLoader = $configLoader;
18
        $this->cachedConfigFile = $cachedConfigFile;
19
    }
20
21
    public function load(): array
22
    {
23
        if (is_file($this->cachedConfigFile)) {
24
            return require $this->cachedConfigFile;
25
        }
26
27
        $config = $this->configLoader->load();
28
29
        file_put_contents($this->cachedConfigFile, '<?php return ' . var_export($config, true) . ';');
30
31
        return $config;
32
    }
33
}
34