Completed
Push — master ( da52cb...88c0e7 )
by Philip
09:03 queued 04:20
created

YamlReader::readFromCache()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 10
ccs 8
cts 9
cp 0.8889
rs 9.2
c 1
b 0
f 1
cc 4
eloc 7
nc 3
nop 1
crap 4.0218
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[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
namespace CRUDlex;
13
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * Reads Yaml files and caches them if a writable path is given. The cache is used first on the next read.
18
 * It is a simple PHP class internally.
19
 */
20
class YamlReader {
21
22
    /**
23
     * The path for the cache files.
24
     */
25
    protected $cachePath;
26
27
    /**
28
     * Gets an absolute path for a cache file.
29
     *
30
     * @param string $fileName
31
     * the file to cache
32
     * @return string
33
     * the absolute path of the cache file
34
     */
35 82
    protected function getCacheFile($fileName) {
36 82
        return $this->cachePath.'/'.basename($fileName).'CRUDlexCache.php';
37
    }
38
39
    /**
40
     * Reads the content of the cached file if it exists.
41
     *
42
     * @param string $fileName
43
     * the cache file to read from
44
     * @return null|array
45
     * the cached data structure or null if the cache file was not available
46
     */
47 82
    protected function readFromCache($fileName) {
48 82
        $cacheFile = $this->getCacheFile($fileName);
49 82
        if (file_exists($cacheFile) && is_readable($cacheFile)) {
50 1
            include($cacheFile);
51 1
            if (isset($crudlexCacheContent)) {
52 1
                return $crudlexCacheContent;
53
            }
54
        }
55 82
        return null;
56 1
    }
57
58
    /**
59
     * Writes the given content to a cached PHP file.
60
     *
61
     * @param string $fileName
62
     * the original filename
63
     * @param array $content
64
     * the content to cache
65
     */
66 80
    protected function writeToCache($fileName, $content) {
67 80
        if ($this->cachePath === null || !is_dir($this->cachePath) || !is_writable($this->cachePath)) {
68 79
            return;
69
        }
70 1
        $encoder    = new \Riimu\Kit\PHPEncoder\PHPEncoder();
71 1
        $contentPHP = $encoder->encode($content, [
72 1
            'whitespace' => false,
73
            'recursion.detect' => false
74 1
        ]);
75 1
        $cache      = '<?php $crudlexCacheContent = '.$contentPHP.';';
76 1
        file_put_contents($this->getCacheFile($fileName), $cache);
77 1
    }
78
79
    /**
80
     * YamlReader constructor.
81
     * @param string|null $cachePath
82
     * if given, the path for the cache files which should be a writable directory
83
     */
84 82
    public function __construct($cachePath) {
85 82
        $this->cachePath = $cachePath;
86 82
    }
87
88
    /**
89
     * Reads and returns the contents of the given Yaml file. If
90
     * it goes wrong, it throws an exception.
91
     *
92
     * @param string $fileName
93
     * the file to read
94
     *
95
     * @return array
96
     * the file contents
97
     *
98
     * @throws \RuntimeException
99
     * thrown if the file could not be read or parsed
100
     */
101 82
    public function read($fileName) {
102
103 82
        $parsedYaml = $this->readFromCache($fileName);
104 82
        if ($parsedYaml !== null) {
105 1
            return $parsedYaml;
106
        }
107
108
        try {
109 82
            $fileContent = file_get_contents($fileName);
110 80
            $parsedYaml  = Yaml::parse($fileContent);
111 80
            if (!is_array($parsedYaml)) {
112 1
                $parsedYaml = [];
113 1
            }
114 80
            $this->writeToCache($fileName, $parsedYaml);
115 80
            return $parsedYaml;
116 2
        } catch (\Exception $e) {
117 2
            throw new \RuntimeException('Could not read Yaml file '.$fileName, $e->getCode(), $e);
118
        }
119
    }
120
121
}
122