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

YamlConfigurationService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 12
c 6
b 0
f 0
dl 0
loc 30
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 2
A hasFile() 0 4 2
A getFile() 0 5 2
A getYaml() 0 5 2
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