Passed
Push — master ( 034b15...36859c )
by Caen
04:33 queued 14s
created

alreadyHasEnvironmentVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Internal;
6
7
use Illuminate\Support\Env;
8
use Hyde\Foundation\Application;
9
10
use function filled;
11
12
/**
13
 * @internal Inject environment variables parsed from the YAML configuration file.
14
 */
15
class LoadYamlEnvironmentVariables
16
{
17
    protected YamlConfigurationRepository $yaml;
18
19
    public function bootstrap(Application $app): void
20
    {
21
        $this->yaml = $app->make(YamlConfigurationRepository::class);
22
23
        if ($this->yaml->hasYamlConfigFile()) {
24
            $this->injectEnvironmentVariables();
25
        }
26
    }
27
28
    protected function injectEnvironmentVariables(): void
29
    {
30
        if ($this->canInjectSiteNameEnvironmentVariable()) {
31
            $this->injectSiteNameEnvironmentVariable();
32
        }
33
    }
34
35
    protected function canInjectSiteNameEnvironmentVariable(): bool
36
    {
37
        return $this->yamlHasSiteNameSet() && ! $this->alreadyHasEnvironmentVariable();
38
    }
39
40
    protected function alreadyHasEnvironmentVariable(): bool
41
    {
42
        return filled(Env::get('SITE_NAME'));
43
    }
44
45
    protected function injectSiteNameEnvironmentVariable(): void
46
    {
47
        $name = $this->getSiteNameFromYaml();
48
49
        Env::getRepository()->set('SITE_NAME', $name);
50
    }
51
52
    protected function yamlHasSiteNameSet(): bool
53
    {
54
        return isset($this->yaml->getData()['hyde']['name']);
55
    }
56
57
    protected function getSiteNameFromYaml(): string
58
    {
59
        return $this->yaml->getData()['hyde']['name'];
60
    }
61
}
62