Passed
Push — master ( 5896bb...74353f )
by Caen
02:58 queued 12s
created

YamlConfigurationService::getFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Hyde\Framework\Services;
4
5
use Hyde\Framework\Hyde;
6
use Illuminate\Support\Facades\Config;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * @see \Hyde\Framework\Testing\Feature\YamlConfigurationServiceTest
11
 */
12
class YamlConfigurationService
13
{
14
    public static function boot(): void
15
    {
16
        if (static::hasFile()) {
17
            Config::set('site', array_merge(
18
                Config::get('site', []),
19
                static::getYaml()
20
            ));
21
        }
22
    }
23
24
    public static function hasFile(): bool
25
    {
26
        return file_exists(Hyde::path('hyde.yml'))
27
            || file_exists(Hyde::path('hyde.yaml'));
28
    }
29
30
    protected static function getFile(): string
31
    {
32
        return file_exists(Hyde::path('hyde.yml'))
33
            ? Hyde::path('hyde.yml')
34
            : Hyde::path('hyde.yaml');
35
    }
36
37
    protected static function getYaml(): array
38
    {
39
        $yaml = Yaml::parse(file_get_contents(static::getFile()));
40
41
        return is_array($yaml) ? $yaml : [];
42
    }
43
}
44