getComposerJsonFilePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Dock\Plugins\ExtraHostname;
4
5
use Dock\Docker\Compose\Project;
6
use Symfony\Component\PropertyAccess\PropertyAccess;
7
8
class HostnameFromComposerResolver implements HostnameResolver
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    public function getExtraHostnameConfigurations(Project $project)
14
    {
15
        $composerFile = $this->getComposerJsonFilePath($project);
16
        if (!file_exists($composerFile)) {
17
            return [];
18
        }
19
20
        $contents = json_decode(file_get_contents($composerFile), true);
21
        $accessor = PropertyAccess::createPropertyAccessor();
22
        $componentsConfiguration = $accessor->getValue($contents, '[extra][dock-cli][extra-hostname]');
23
        if (!is_array($componentsConfiguration)) {
24
            return [];
25
        }
26
27
        $configurations = [];
28
        foreach ($componentsConfiguration as $componentName => $hostNames) {
29
            if (!is_array($hostNames)) {
30
                $hostNames = [$hostNames];
31
            }
32
33
            foreach ($hostNames as $hostname) {
34
                $configurations[] = new HostnameConfiguration($componentName, $hostname);
35
            }
36
        }
37
38
        return $configurations;
39
    }
40
41
    /**
42
     * @param Project $project
43
     *
44
     * @return string
45
     */
46
    private function getComposerJsonFilePath(Project $project)
47
    {
48
        return $project->getProjectBasePath().DIRECTORY_SEPARATOR.'composer.json';
49
    }
50
}
51