Completed
Pull Request — master (#13)
by Pol
13:41
created

AbstractCommands::loadDefaultConfig()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 48
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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

61
    public function loadDefaultConfig(/** @scrutinizer ignore-unused */ ConsoleCommandEvent $event): void

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...
62
    {
63
        /** @var \Robo\Config\Config $config */
64
        $config = $this->getConfig();
65
66
        if (null === $config->get('options.bin_dir')) {
67
            if (null !== $composerConfig = Taskman::createJsonConfiguration([getcwd() . '/composer.json'])) {
68
                // The COMPOSER_BIN_DIR environment takes precedence over the value
69
                // defined in composer.json config, if any. Default to ./vendor/bin.
70
                if (!$composerBinDir = getenv('COMPOSER_BIN_DIR')) {
71
                    $defaultDir = './vendor/bin';
72
73
                    if ('phptaskman/core' === $composerConfig->get('name', '')) {
74
                        $defaultDir = './bin';
75
                    }
76
77
                    $composerBinDir = $composerConfig->get('config.bin-dir', $defaultDir);
78
                }
79
80
                if (false === mb_strpos($composerBinDir, './')) {
81
                    $composerBinDir = './' . $composerBinDir;
82
                }
83
84
                $composerBinDir = rtrim($composerBinDir, \DIRECTORY_SEPARATOR);
85
                $config->set('options.bin_dir', $composerBinDir);
86
            }
87
        }
88
89
        $config->set('options.bin', realpath($config->get('options.bin_dir') . \DIRECTORY_SEPARATOR . 'taskman'));
90
91
        Robo::loadConfiguration(
92
            array_filter([
93
                realpath($this->getConfigurationFile()),
94
            ]),
95
            $config
96
        );
97
98
        $default_config = Taskman::createConfiguration(
99
            [$this->getDefaultConfigurationFile()]
100
        );
101
102
        // Re-build configuration.
103
        $processor = new ConfigProcessor();
104
        $processor->add($default_config->export());
105
        $processor->add($config->export());
106
107
        // Import newly built configuration.
108
        $config->replace($processor->export());
109
    }
110
}
111