Completed
Push — develop ( daf290...9a283c )
by Paul
02:03
created

AbstractGenerateCommand::getSymfonyStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Console;
4
5
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
6
use PhpUnitGen\Configuration\JsonConsoleConfigFactory;
7
use PhpUnitGen\Configuration\PhpConsoleConfigFactory;
8
use PhpUnitGen\Configuration\YamlConsoleConfigFactory;
9
use PhpUnitGen\Container\ContainerInterface\ConsoleContainerFactoryInterface;
10
use PhpUnitGen\Exception\Exception;
11
use PhpUnitGen\Exception\InvalidConfigException;
12
use PhpUnitGen\Executor\ExecutorInterface\ConsoleExecutorInterface;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Style\SymfonyStyle;
17
use Symfony\Component\Stopwatch\Stopwatch;
18
19
/**
20
 * Class AbstractGenerateCommand.
21
 *
22
 * @author     Paul Thébaud <[email protected]>.
23
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
24
 * @license    https://opensource.org/licenses/MIT The MIT license.
25
 * @link       https://github.com/paul-thebaud/phpunit-generator
26
 * @since      Class available since Release 2.0.0.
27
 */
28
abstract class AbstractGenerateCommand extends Command
29
{
30
    /**
31
     * @var string[] CONSOLE_CONFIG_FACTORIES Mapping array between file extension and configuration factories.
32
     */
33
    protected const CONSOLE_CONFIG_FACTORIES = [
34
        'yml'  => YamlConsoleConfigFactory::class,
35
        'json' => JsonConsoleConfigFactory::class,
36
        'php'  => PhpConsoleConfigFactory::class
37
    ];
38
39
    /**
40
     * @var ConsoleContainerFactoryInterface $containerFactory A container factory to create container.
41
     */
42
    protected $containerFactory;
43
44
    /**
45
     * @var ConsoleExecutorInterface $consoleExecutor A executor to execute PhpUnitGen task.
46
     */
47
    protected $consoleExecutor;
48
49
    /**
50
     * @var Stopwatch $stopwatch The stopwatch to measure duration and memory usage.
51
     */
52
    protected $stopwatch;
53
54
    /**
55
     * GenerateCommand constructor.
56
     *
57
     * @param ConsoleContainerFactoryInterface $containerFactory A container factory to create container.
58
     * @param Stopwatch                        $stopwatch        The stopwatch component for execution stats.
59
     */
60
    public function __construct(ConsoleContainerFactoryInterface $containerFactory, Stopwatch $stopwatch)
61
    {
62
        parent::__construct();
63
64
        $this->containerFactory = $containerFactory;
65
        $this->stopwatch        = $stopwatch;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function execute(InputInterface $input, OutputInterface $output): int
72
    {
73
        $this->stopwatch->start('command');
74
75
        $styledOutput = $this->getSymfonyStyle($input, $output);
76
        try {
77
            $config = $this->getConfiguration($input);
78
79
            $container = $this->containerFactory->invoke($config, $styledOutput, $this->stopwatch);
80
81
            $this->consoleExecutor = $container->get(ConsoleExecutorInterface::class);
82
83
            $this->consoleExecutor->invoke();
84
        } catch (Exception $exception) {
85
            $styledOutput->error($exception->getMessage());
86
            return -1;
87
        }
88
89
        return 0;
90
    }
91
92
    /**
93
     * Build a new instance of SymfonyStyle.
94
     *
95
     * @param InputInterface  $input  The input.
96
     * @param OutputInterface $output The output.
97
     *
98
     * @return SymfonyStyle The created symfony style i/o.
99
     */
100
    protected function getSymfonyStyle(InputInterface $input, OutputInterface $output): SymfonyStyle
101
    {
102
        return new SymfonyStyle($input, $output);
103
    }
104
105
    /**
106
     * Build a configuration from a configuration file path.
107
     *
108
     * @param InputInterface $input An input interface to retrieve command argument.
109
     *
110
     * @return ConsoleConfigInterface The created configuration.
111
     *
112
     * @throws InvalidConfigException If an error occurs during process.
113
     */
114
    abstract protected function getConfiguration(InputInterface $input): ConsoleConfigInterface;
115
}
116