Completed
Push — master ( 8e56f4...9f919b )
by Christian
03:16
created

fixCommandDefinition()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 45
rs 8.439
cc 5
eloc 30
nc 16
nop 1
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\Task\Composer;
22
23
use Composer\Command\BaseCommand;
24
use Composer\Factory;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Tenside\Core\Task\Composer\WrappedCommand\WrappedCommandTrait;
29
use Tenside\Core\Task\Task;
30
use Tenside\Core\Task\TaskOutput;
31
32
/**
33
 * This task provides the basic framework for building tasks that perform composer commands.
34
 */
35
abstract class AbstractComposerCommandTask extends Task
36
{
37
    /**
38
     * Prepare the Command instance to execute.
39
     *
40
     * @return BaseCommand
41
     */
42
    abstract protected function prepareCommand();
43
44
    /**
45
     * Prepare the input interface for the command.
46
     *
47
     * @return InputInterface
48
     */
49
    abstract protected function prepareInput();
50
51
    /**
52
     * Add missing definition options to the command usually defined by the application.
53
     *
54
     * See also composer/composer in src/Composer/Console/Application.php
55
     *
56
     * @param BaseCommand $command The command to fix.
57
     *
58
     * @return void
59
     */
60
    protected function fixCommandDefinition(BaseCommand $command)
61
    {
62
        $definition = $command->getDefinition();
63
64
        if (!$definition->hasOption('verbose')) {
65
            $definition->addOption(
66
                new InputOption(
67
                    'verbose',
68
                    'v|vv|vvv',
69
                    InputOption::VALUE_NONE,
70
                    'Shows more details including new commits pulled in when updating packages.'
71
                )
72
            );
73
        }
74
        if (!$definition->hasOption('profile')) {
75
            $definition->addOption(
76
                new InputOption(
77
                    'profile',
78
                    null,
79
                    InputOption::VALUE_NONE,
80
                    'Display timing and memory usage information'
81
                )
82
            );
83
        }
84
        if (!$definition->hasOption('no-plugins')) {
85
            $definition->addOption(
86
                new InputOption(
87
                    'no-plugins',
88
                    null,
89
                    InputOption::VALUE_NONE,
90
                    'Whether to disable plugins.'
91
                )
92
            );
93
        }
94
        if (!$definition->hasOption('working-dir')) {
95
            $definition->addOption(
96
                new InputOption(
97
                    'working-dir',
98
                    '-d',
99
                    InputOption::VALUE_REQUIRED,
100
                    'If specified, use the given directory as working directory.'
101
                )
102
            );
103
        }
104
    }
105
106
    /**
107
     * Attach the composer factory to the command.
108
     *
109
     * @param BaseCommand $command The command to patch.
110
     *
111
     * @return BaseCommand
0 ignored issues
show
Documentation introduced by
Should the return type not be WrappedCommandTrait?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
112
     *
113
     * @throws \InvalidArgumentException When no setComposerFactory method is declared.
114
     */
115
    protected function attachComposerFactory(BaseCommand $command)
116
    {
117
        if (!method_exists($command, 'setComposerFactory')) {
118
            throw new \InvalidArgumentException('The passed command does not implement method setComposerFactory()');
119
        }
120
121
        /** @var WrappedCommandTrait $command */
122
        $command->setComposerFactory(
123
            function () {
124
                return Factory::create($this->getIO());
125
            }
126
        );
127
128
        return $command;
129
    }
130
131
    /**
132
     * Execute the command and throw exceptions on errors.
133
     *
134
     * @param BaseCommand     $command The command to execute.
135
     *
136
     * @param InputInterface  $input   The input to use.
137
     *
138
     * @param OutputInterface $output The output to use.
139
     *
140
     * @return void
141
     *
142
     * @throws \RuntimeException On exceptions or when the command has an non zero exit code.
143
     */
144
    protected function executeCommand(BaseCommand $command, InputInterface $input, OutputInterface $output)
145
    {
146
        try {
147
            if (0 !== ($statusCode = $command->run($input, $output))) {
148
                throw new \RuntimeException('Error: command exit code was ' . $statusCode);
149
            }
150
        } catch (\Exception $exception) {
151
            throw new \RuntimeException($exception->getMessage(), $exception->getCode(), $exception);
152
        }
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     *
158
     * @return void
159
     */
160
    public function doPerform()
161
    {
162
        $command = $this->prepareCommand();
163
        $command->setIO($this->getIO());
164
        $this->fixCommandDefinition($command);
165
166
        $this->executeCommand($command, $this->prepareInput(), new TaskOutput($this));
167
    }
168
}
169