Completed
Push — master ( 60ef51...780387 )
by Nikola
02:46
created

CachingConfigLoader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 33
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 14 2
1
<?php
2
/**
3
 * This file is part of the Phoundation package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Phoundation\Config\Loader;
14
15
use Phoundation\Config\Config;
16
17
/**
18
 * @author Nikola Posa <[email protected]>
19
 */
20
final class CachingConfigLoader implements ConfigLoaderInterface
21
{
22
    /**
23
     * @var ConfigLoaderInterface
24
     */
25
    private $configLoader;
26
27
    /**
28
     * @var string
29
     */
30
    private $cachedConfigFile;
31
    
32
    public function __construct(ConfigLoaderInterface $configLoader, string $cachedConfigFile)
33
    {
34
        $this->configLoader = $configLoader;
35
        $this->cachedConfigFile = $cachedConfigFile;
36
    }
37
38
    public function __invoke() : Config
39
    {
40
        if (is_file($this->cachedConfigFile)) {
41
            return Config::fromArray(include $this->cachedConfigFile);
42
        }
43
44
        $configLoader = $this->configLoader;
45
        /* @var $config Config */
46
        $config = $configLoader();
47
48
        file_put_contents($this->cachedConfigFile, '<?php return ' . var_export($config->toArray(), true) . ';');
49
50
        return $config;
51
    }
52
}
53