Passed
Push — master ( a91ab9...0e37d9 )
by Pol
02:27
created

AbstractCommands::loadDefaultConfig()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 40
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 8
nop 1
dl 0
loc 40
ccs 0
cts 26
cp 0
crap 42
rs 9.0444
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\Taskman;
10
use PhpTaskman\Core\Traits\ComposerAwareTrait;
11
use Robo\Common\ConfigAwareTrait;
12
use Robo\Common\IO;
13
use Robo\Contract\ConfigAwareInterface;
14
use Robo\Contract\IOAwareInterface;
15
use Robo\Contract\BuilderAwareInterface;
16
use Robo\LoadAllTasks;
17
use Robo\Robo;
18
use Symfony\Component\Console\Event\ConsoleCommandEvent;
19
use Symfony\Component\EventDispatcher\Event;
20
21
/**
22
 * Class AbstractCommands.
23
 */
24
abstract class AbstractCommands implements
25
    BuilderAwareInterface,
26
    IOAwareInterface,
27
    ConfigAwareInterface,
28
    ComposerAwareInterface
29
{
30
    use ComposerAwareTrait;
31
    use ConfigAwareTrait;
32
    use IO;
33
    use LoadAllTasks;
34
35
    /**
36
     * Path to YAML configuration file containing command defaults.
37
     *
38
     * Command classes should implement this method.
39
     *
40
     * @return string
41
     */
42
    abstract public function getConfigurationFile(): string;
43
44
    /**
45
     * Path to YAML configuration file containing command defaults.
46
     *
47
     * Command classes should implement this method.
48
     *
49
     * @return string
50
     *   The path of the default configuration file.
51
     */
52
    abstract public function getDefaultConfigurationFile(): string;
53
54
    /**
55
     * Load default configuration.
56
     *
57
     * PHP Tasks does not allow to provide default configuration for
58
     * commands. In this hook we load Toolkit default configuration and re-apply
59
     * it back.
60
     *
61
     * @hook pre-command-event *
62
     */
63
    public function loadDefaultConfig(ConsoleCommandEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function loadDefaultConfig(/** @scrutinizer ignore-unused */ ConsoleCommandEvent $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        // Get Tasks configuration.
66
        $config = $this->getConfig();
67
68
        if (null === $config->get('taskman.bin_dir')) {
69
            if (null !== $composer = $this->getComposer()) {
70
                // The COMPOSER_BIN_DIR environment takes precedence over the value
71
                // defined in composer.json config, if any. Default to ./vendor/bin.
72
                if (!$composerBinDir = \getenv('COMPOSER_BIN_DIR')) {
73
                    if (!$composerBinDir = $composer->getConfig('bin-dir')) {
74
                        $composerBinDir = './vendor/bin';
75
                    }
76
                }
77
78
                if (false === \strpos($composerBinDir, './')) {
79
                    $composerBinDir = './' . $composerBinDir;
80
                }
81
82
                $composerBinDir = \rtrim($composerBinDir, \DIRECTORY_SEPARATOR);
83
                $config->set('taskman.bin_dir', $composerBinDir);
84
            }
85
        }
86
87
        // Refactor this.
88
        $configurationFilePath = \realpath($this->getConfigurationFile());
89
90
        Robo::loadConfiguration([$configurationFilePath], $config);
91
92
        $default_config = Taskman::createConfiguration(
93
            [$this->getDefaultConfigurationFile()]
94
        );
95
96
        // Re-build configuration.
97
        $processor = new ConfigProcessor();
98
        $processor->add($default_config->export());
99
        $processor->add($config->export());
100
101
        // Import newly built configuration.
102
        $config->import($processor->export());
103
    }
104
}
105