Passed
Push — master ( 9e0b2c...e0b23c )
by Pol
03:34
created

AbstractCommands::loadDefaultConfig()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 39
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 16
nop 0
dl 0
loc 39
ccs 0
cts 26
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Robo\Plugin\Commands;
6
7
use Consolidation\Config\Loader\ConfigProcessor;
8
use PhpTaskman\Core\Contract\ComposerAwareInterface;
9
use PhpTaskman\Core\Traits\ComposerAwareTrait;
10
use Robo\Common\ConfigAwareTrait;
11
use Robo\Common\IO;
12
use Robo\Contract\ConfigAwareInterface;
13
use Robo\Contract\IOAwareInterface;
14
use Robo\Contract\BuilderAwareInterface;
15
use Robo\LoadAllTasks;
16
use Robo\Robo;
17
18
/**
19
 * Class AbstractCommands.
20
 */
21
abstract class AbstractCommands implements
22
    BuilderAwareInterface,
23
    IOAwareInterface,
24
    ConfigAwareInterface,
25
    ComposerAwareInterface
26
{
27
    use ComposerAwareTrait;
28
    use ConfigAwareTrait;
29
    use IO;
30
    use LoadAllTasks;
31
32
    /**
33
     * Path to YAML configuration file containing command defaults.
34
     *
35
     * Command classes should implement this method.
36
     *
37
     * @return string
38
     */
39
    abstract public function getConfigurationFile(): string;
40
41
    /**
42
     * Path to YAML configuration file containing command defaults.
43
     *
44
     * Command classes should implement this method.
45
     *
46
     * @return string
47
     *   The path of the default configuration file.
48
     */
49
    abstract public function getDefaultConfigurationFile(): string;
50
51
    /**
52
     * Load default configuration.
53
     *
54
     * PHP Tasks does not allow to provide default configuration for
55
     * commands. In this hook we load Toolkit default configuration and re-apply
56
     * it back.
57
     *
58
     * @hook pre-command-event *
59
     */
60
    public function loadDefaultConfig()
61
    {
62
        // Get Tasks configuration.
63
        $config = $this->getConfig();
64
65
        if (null === $config->get('taskman.bin_dir')) {
66
            if (null !== $composer = $this->getComposer()) {
67
                // The COMPOSER_BIN_DIR environment takes precedence over the value
68
                // defined in composer.json config, if any. Default to ./vendor/bin.
69
                if (!$composerBinDir = \getenv('COMPOSER_BIN_DIR')) {
70
                    if (!$composerBinDir = $composer->getConfig('bin-dir')) {
71
                        $composerBinDir = './vendor/bin';
72
                    }
73
                }
74
75
                if (false === \strpos($composerBinDir, './')) {
76
                    $composerBinDir = './' . $composerBinDir;
77
                }
78
79
                $composerBinDir = \rtrim($composerBinDir, \DIRECTORY_SEPARATOR);
80
                $config->set('taskman.bin_dir', $composerBinDir);
81
            }
82
        }
83
84
        $configurationFilePath = \realpath($this->getConfigurationFile());
85
86
        if (false !== $config) {
0 ignored issues
show
introduced by
The condition false !== $config is always true.
Loading history...
87
            Robo::loadConfiguration([$configurationFilePath], $config);
88
        }
89
90
        $default_config = Robo::createConfiguration([$this->getDefaultConfigurationFile()]);
91
92
        // Re-build configuration.
93
        $processor = new ConfigProcessor();
94
        $processor->add($default_config->export());
95
        $processor->add($config->export());
96
97
        // Import newly built configuration.
98
        $config->import($processor->export());
99
    }
100
}
101