Test Failed
Push — main ( ad999b...e26c51 )
by Rafael
06:04
created

YamlSchema::getYamlFileName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (C) 2022-2023  Rafael San José Tovar   <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Alxarafe\Database;
20
21
use Alxarafe\Core\Utils\Utils;
22
use Symfony\Component\Yaml\Yaml;
23
24
abstract class YamlSchema
25
{
26
    const YAML_CACHE_DIR = TMP_DIR . 'yamlcache/';
27
28
    private static $yamlPath;
0 ignored issues
show
introduced by
The private property $yamlPath is not used, and could be removed.
Loading history...
29
30
    public static function clearYamlCache(): bool
31
    {
32
        return Utils::delTree(self::YAML_CACHE_DIR) && Utils::createDir(self::YAML_CACHE_DIR);
33
    }
34
35
    public static function getYamlFileName(string $folder, string $filename): string
36
    {
37
        $path = self::YAML_CACHE_DIR . $folder . '/';
38
        if (!file_exists($path)) {
39
            if (!Utils::createDir($path)) {
40
                debug_message('No se ha podido crear la carpeta ' . $path);
0 ignored issues
show
Bug introduced by
The function debug_message was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

40
                /** @scrutinizer ignore-call */ 
41
                debug_message('No se ha podido crear la carpeta ' . $path);
Loading history...
41
            }
42
        }
43
        return $path . $filename . '.yaml';;
44
    }
45
46
    public static function loadYamlFile(string $filename): array
47
    {
48
        if (!file_exists($filename) || !is_readable($filename)) {
49
            return [];
50
        }
51
        return Yaml::parseFile($filename);
52
    }
53
54
    public static function saveCacheYamlFile(string $folder, string $filename, array $content, int $maxLevel = 3): bool
55
    {
56
        $path = self::getYamlFileName($folder, $filename);
57
        return file_put_contents($path, Yaml::dump($content, $maxLevel)) !== false;
58
    }
59
60
    public static function loadCacheYamlFile(string $folder, string $filename): array
61
    {
62
        $path = self::getYamlFileName($folder, $filename);
63
        if (empty($path) || !file_exists($path) || !is_readable($path)) {
64
            return [];
65
        }
66
        if (!isset($path)) {
67
            dump($path);
68
        }
69
        return Yaml::parseFile($path);
70
    }
71
72
}
73