Completed
Push — develop ( a0b2b6...96900c )
by Paul
02:40
created

AbstractGenerateCommand::generateForDirectory()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 4
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Console;
4
5
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
6
use PhpUnitGen\Container\ContainerInterface\ConsoleContainerFactoryInterface;
7
use PhpUnitGen\Exception\InvalidConfigException;
8
use PhpUnitGen\Executor\ExecutorInterface\ConsoleExecutorInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
/**
15
 * Class AbstractGenerateCommand.
16
 *
17
 * @author     Paul Thébaud <[email protected]>.
18
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
19
 * @license    https://opensource.org/licenses/MIT The MIT license.
20
 * @link       https://github.com/paul-thebaud/phpunit-generator
21
 * @since      Class available since Release 2.0.0.
22
 */
23
abstract class AbstractGenerateCommand extends Command
24
{
25
    /**
26
     * @var ConsoleContainerFactoryInterface $containerFactory A container factory to create container.
27
     */
28
    protected $containerFactory;
29
30
    /**
31
     * @var ConsoleExecutorInterface $consoleExecutor A executor to execute PhpUnitGen task.
32
     */
33
    protected $consoleExecutor;
34
35
    /**
36
     * GenerateCommand constructor.
37
     *
38
     * @param ConsoleContainerFactoryInterface $containerFactory A container factory to create container.
39
     */
40
    public function __construct(ConsoleContainerFactoryInterface $containerFactory)
41
    {
42
        parent::__construct();
43
44
        $this->containerFactory = $containerFactory;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $styledOutput = new SymfonyStyle($input, $output);
53
        try {
54
            $config = $this->getConfiguration($input);
55
        } catch (InvalidConfigException $exception) {
56
            $styledOutput->error($exception->getMessage());
57
            return -1;
58
        }
59
60
        $container = $this->containerFactory->invoke($config, $styledOutput);
61
62
        $this->consoleExecutor = $container->get(ConsoleExecutorInterface::class);
63
64
        $this->consoleExecutor->execute();
65
66
        /** @todo detailed stats */
67
        $styledOutput->success('Parsing finished.');
68
69
        return 1;
70
    }
71
72
    /**
73
     * Build a configuration from a configuration file path.
74
     *
75
     * @param InputInterface $input An input interface to retrieve command argument.
76
     *
77
     * @return ConsoleConfigInterface The created configuration.
78
     *
79
     * @throws InvalidConfigException If an error occurs during process.
80
     */
81
    protected abstract function getConfiguration(InputInterface $input): ConsoleConfigInterface;
82
}
83