Failed Conditions
Push — develop ( c92f1b...efe073 )
by Maxime
18:38
created

CachedConfigurationLoader::getConfiguration()   B

Complexity

Conditions 4
Paths 8

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 28
Code Lines 14

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
ccs 11
cts 12
cp 0.9167
rs 8.5806
cc 4
eloc 14
nc 8
nop 0
crap 4.0092
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Services\Config\Files\Loaders;
14
15
use Illuminate\Support\Arr;
16
use Rocketeer\Services\Config\Files\ConfigurationCache;
17
use Rocketeer\Traits\ContainerAwareTrait;
18
use Symfony\Component\Config\Resource\FileResource;
19
use Symfony\Component\Finder\SplFileInfo;
20
21
/**
22
 * Decorator around a ConfigurationLoader that caches its contents
23
 * and checks for freshness.
24
 */
25
class CachedConfigurationLoader implements ConfigurationLoaderInterface
26
{
27
    use ContainerAwareTrait;
28
29
    /**
30
     * @var ConfigurationCache
31
     */
32
    protected $cache;
33
34
    /**
35
     * @var ConfigurationLoaderInterface
36
     */
37
    protected $loader;
38
39
    /**
40
     * @var FileResource[]
41
     */
42
    protected $resources = [];
43
44
    /**
45
     * @param ConfigurationCache           $cache
46
     * @param ConfigurationLoaderInterface $loader
47
     */
48
    public function __construct(ConfigurationCache $cache, ConfigurationLoaderInterface $loader)
49 433
    {
50
        $this->cache = $cache;
51 433
        $this->loader = $loader;
52 433
    }
53 433
54
    /**
55
     * @param string[] $folders
56
     */
57
    public function setFolders(array $folders)
58 8
    {
59
        $this->loader->setFolders($folders);
60 8
    }
61
62
    /**
63 8
     * @return string[]
64 8
     */
65 8
    public function getFolders()
66 8
    {
67 8
        return $this->loader->getFolders();
68
    }
69
70
    /**
71
     * @return SplFileInfo[]
72 1
     */
73
    public function getFiles()
74 1
    {
75
        return $this->loader->getFiles();
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getConfiguration()
82
    {
83
        // Gather paths to files to load
84
        $files = $this->loader->getFiles();
85
        foreach ($files as &$file) {
86
            $file = $file->getPathname();
87
        }
88 433
89
        // Create resources
90
        $this->resources = [];
91 433
        $filePaths = array_values($files);
92 433
        foreach ($filePaths as $path) {
93 433
            $this->resources[] = new FileResource($path);
94 433
        }
95
96
        if ($cached = $this->getCachedConfiguration($filePaths)) {
97 433
            return $cached;
98 433
        }
99 433
100
        // Load the configuration
101
        $configuration = $this->loader->getConfiguration();
102
        $configuration['meta'] = $filePaths;
103 12
104 12
        // Cache the configuration
105
        $this->cache->write($configuration, $this->resources);
106
107 12
        return $configuration;
108
    }
109 12
110
    //////////////////////////////////////////////////////////////////////
111
    ////////////////////////////// CACHING ///////////////////////////////
112
    //////////////////////////////////////////////////////////////////////
113
114
    /**
115
     * @param string[] $filePaths
116
     *
117
     * @return array
0 ignored issues
show
Documentation introduced by Maxime Fabre
Should the return type not be null|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
118
     */
119
    protected function getCachedConfiguration($filePaths)
120
    {
121 433
        // Return cached version if available
122
        if (!$this->cache->isFresh()) {
123
            return;
124 433
        }
125 6
126
        // If the files the configuration was cached from
127
        // match the ones we have, return the cache
128
        $cache = $this->cache->getContents();
129
        if (Arr::get($cache, 'meta') === $filePaths) {
130 433
            return $cache;
131 433
        }
132 433
    }
133
}
134