AbstractCommands   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 79
ccs 0
cts 25
cp 0
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A loadDefaultConfig() 0 40 5
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
                    $composerBinDir = $composerConfig->get('bin-dir', './vendor/bin');
72
                }
73
74
                if (false === mb_strpos($composerBinDir, './')) {
75
                    $composerBinDir = './' . $composerBinDir;
76
                }
77
78
                $composerBinDir = rtrim($composerBinDir, \DIRECTORY_SEPARATOR);
79
                $config->set('options.bin_dir', $composerBinDir);
80
            }
81
        }
82
83
        Robo::loadConfiguration(
84
            array_filter([
85
                realpath($this->getConfigurationFile()),
86
            ]),
87
            $config
88
        );
89
90
        $default_config = Taskman::createConfiguration(
91
            [$this->getDefaultConfigurationFile()]
92
        );
93
94
        // Re-build configuration.
95
        $processor = new ConfigProcessor();
96
        $processor->add($default_config->export());
97
        $processor->add($config->export());
98
99
        // Import newly built configuration.
100
        $config->replace($processor->export());
101
    }
102
}
103