Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
LoadYamlEnvironmentVariables
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
7 / 7
10
100.00% covered (success)
100.00%
1 / 1
 bootstrap
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 injectEnvironmentVariables
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 canInjectSiteNameEnvironmentVariable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 alreadyHasEnvironmentVariable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 injectSiteNameEnvironmentVariable
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 yamlHasSiteNameSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSiteNameFromYaml
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Foundation\Internal;
6
7use Illuminate\Support\Env;
8use Hyde\Foundation\Application;
9
10use function filled;
11
12/**
13 * @internal Inject environment variables parsed from the YAML configuration file.
14 */
15class 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}