Passed
Push — master ( 7d76d6...1230fe )
by Caen
03:19 queued 13s
created

LoadYamlConfiguration::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
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Services;
6
7
use Hyde\Hyde;
8
use Illuminate\Support\Facades\Config;
9
use LaravelZero\Framework\Application;
10
use Symfony\Component\Yaml\Yaml;
11
use function array_merge;
12
use function file_exists;
13
use function file_get_contents;
14
use function is_array;
15
16
/**
17
 * @internal
18
 *
19
 * @see \Hyde\Framework\Testing\Feature\YamlConfigurationServiceTest
20
 */
21
class LoadYamlConfiguration
22
{
23
    /**
24
     * Performs a core task that needs to be performed on
25
     * early stages of the framework.
26
     */
27
    public function bootstrap(Application $app): void
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

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

27
    public function bootstrap(/** @scrutinizer ignore-unused */ Application $app): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        if ($this->hasYamlConfigFile()) {
30
            $this->mergeParsedConfiguration();
31
        }
32
    }
33
34
    protected function hasYamlConfigFile(): bool
35
    {
36
        return file_exists(Hyde::path('hyde.yml'))
37
            || file_exists(Hyde::path('hyde.yaml'));
38
    }
39
40
    protected function mergeParsedConfiguration(): void
41
    {
42
        Config::set('site', array_merge(
43
            Config::get('site', []),
44
            $this->getYaml()
45
        ));
46
    }
47
48
    protected function getYaml(): array
49
    {
50
        $yaml = Yaml::parse(file_get_contents($this->getFile()));
51
52
        return is_array($yaml) ? $yaml : [];
53
    }
54
55
    protected function getFile(): string
56
    {
57
        return file_exists(Hyde::path('hyde.yml'))
58
            ? Hyde::path('hyde.yml')
59
            : Hyde::path('hyde.yaml');
60
    }
61
}
62