Passed
Push — master ( 3afc9a...773baf )
by Pol
02:15
created

Config   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 79.59%

Importance

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