Completed
Push — master ( c2b77e...59e393 )
by Pol
04:17 queued 29s
created

Config   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 129
ccs 40
cts 48
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocalConfigurationFilepath() 0 15 4
B findFilesToIncludeInConfiguration() 0 74 7
A resolveImports() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Config;
6
7
use PhpTaskman\Core\Taskman;
8
use Symfony\Component\Finder\SplFileInfo;
9
10
final class Config
11
{
12
    /**
13
     * Find the files to include in the configuration.
14
     *
15
     * @param string $cwd
16
     *   The current working directory.
17
     *
18
     * @return string[]
19
     *   The list of all the YAML files to include.
20
     */
21 1
    public static function findFilesToIncludeInConfiguration($cwd)
22
    {
23
        // Get the vendor-bin property from the composer.json.
24 1
        $composer = Taskman::getComposerFromDirectory($cwd);
25
26 1
        if (false === $composer) {
0 ignored issues
show
introduced by
The condition false === $composer is always false.
Loading history...
27
            return [];
28
        }
29
30 1
        $vendorDir = $composer->get('vendor-dir') ?? $cwd . '/vendor';
31
32
        // Keep a reference of the default filename that we need to load from
33
        // each packages.
34
        $filesToLoad = [
35 1
            'taskman.yml.dist',
36
            'taskman.yml',
37
        ];
38
39
        // Load default paths.
40
        $filesystemPaths = [
41 1
            __DIR__ . '/../config/default.yml',
42
            __DIR__ . '/../default.yml',
43 1
            static::getLocalConfigurationFilepath(),
44
        ];
45
46 1
        $composerLock = \json_decode(
47 1
            \file_get_contents($cwd . '/composer.lock'),
48 1
            true
49 1
        ) + ['packages' => [], 'packages-dev' => []];
50
51 1
        $packageDirectories = \array_filter(
52 1
            \array_map(
53
                static function ($package) use ($cwd, $vendorDir) {
0 ignored issues
show
Unused Code introduced by
The import $cwd is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
54 1
                    return \realpath($vendorDir . '/' . $package['name']);
55 1
                },
56 1
                \array_merge(
57 1
                    $composerLock['packages'],
58 1
                    $composerLock['packages-dev']
59
                )
60
            )
61
        );
62
63 1
        $packageDirectories[] = $cwd;
64
65
        // Loop over each composer.json, deduct the package directory and probe for files to include.
66
        // @var SplFileInfo $file
67 1
        foreach ($packageDirectories as $packageDirectory) {
68 1
            foreach ($filesToLoad as $taskmanFile) {
69 1
                foreach ([$packageDirectory, $cwd] as $directory) {
70 1
                    $candidateFile = $directory . '/' . $taskmanFile;
71 1
                    $filesystemPaths[$candidateFile] = $candidateFile;
72
                }
73
            }
74
75 1
            $composer = Taskman::getComposerFromDirectory(
76 1
                $packageDirectory
77
            );
78
79 1
            $extraFiles = $composer->get('extra.taskman.files', []);
80
81 1
            if (empty($extraFiles)) {
82 1
                continue;
83
            }
84
85
            foreach ($extraFiles as $commandFile) {
86
                $filesystemPaths[$commandFile] = $commandFile;
87
                $commandFile = $packageDirectory . '/' . $commandFile;
88
                $filesystemPaths[$commandFile] = $commandFile;
89
            }
90
        }
91
92 1
        return \array_filter(
93 1
            static::resolveImports(...\array_values($filesystemPaths)),
94 1
            'file_exists'
95
        );
96
    }
97
98
    /**
99
     * Get the local configuration filepath.
100
     *
101
     * @param string $configuration_file
102
     *   The default filepath.
103
     *
104
     * @return null|string
105
     *   The local configuration file path, or null if it doesn't exist.
106
     */
107 1
    public static function getLocalConfigurationFilepath($configuration_file = 'phptaskman/taskman.yml')
108
    {
109 1
        if ($config = \getenv('PHPTASKMAN_CONFIG')) {
110
            return $config;
111
        }
112
113 1
        if ($config = \getenv('XDG_CONFIG_HOME')) {
114
            return $config . '/' . $configuration_file;
115
        }
116
117 1
        if ($home = \getenv('HOME')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $home is dead and can be removed.
Loading history...
118 1
            return \getenv('HOME') . '/.config/' . $configuration_file;
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * Resolve YAML configurations files containing imports.
126
     *
127
     * Handles circular dependencies by ignoring them.
128
     *
129
     * @param string[] ...$filepaths
130
     *   A list of YML filepath to parse.
131
     *
132
     * @return string[]
133
     *   The list of all the YAML files to include.
134
     */
135 1
    public static function resolveImports(...$filepaths)
136
    {
137 1
        return (new YamlRecursivePathsFinder($filepaths))
138 1
            ->getAllPaths();
139
    }
140
}
141