Completed
Push — develop ( 67e161...8b0772 )
by Paul
05:50
created

GenerateCommand::getConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Console;
4
5
use PhpUnitGen\Configuration\ConsoleConfigFactoryInterface;
6
use PhpUnitGen\Configuration\ConsoleConfigInterface;
7
use PhpUnitGen\Configuration\JsonConsoleConfigFactory;
8
use PhpUnitGen\Configuration\PhpConsoleConfigFactory;
9
use PhpUnitGen\Configuration\YamlConsoleConfigFactory;
10
use PhpUnitGen\Exception\InvalidConfigException;
11
use Respect\Validation\Validator;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
15
/**
16
 * Class GenerateCommand.
17
 *
18
 * @author     Paul Thébaud <[email protected]>.
19
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
20
 * @license    https://opensource.org/licenses/MIT The MIT license.
21
 * @link       https://github.com/paul-thebaud/phpunit-generator
22
 * @since      Class available since Release 2.0.0.
23
 */
24
class GenerateCommand extends AbstractGenerateCommand
25
{
26
    /**
27
     * @var string[] CONSOLE_CONFIG_FACTORIES Mapping array between file extension and configuration factories.
28
     */
29
    const CONSOLE_CONFIG_FACTORIES = [
30
        'yml'  => YamlConsoleConfigFactory::class,
31
        'json' => JsonConsoleConfigFactory::class,
32
        'php'  => PhpConsoleConfigFactory::class
33
    ];
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function configure()
39
    {
40
        $this->setName("generate")
41
            ->setDescription("Generate unit tests skeletons")
42
            ->setHelp("Use it to generate your unit tests skeletons from a config file")
43
            ->addArgument('config-path', InputArgument::REQUIRED, 'The config file path.');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getConfiguration(InputInterface $input): ConsoleConfigInterface
50
    {
51
        $configPath = $input->getArgument('config-path');
52
53
        if (! file_exists($configPath)) {
54
            throw new InvalidConfigException(sprintf('Config file "%s" does not exists.', $configPath));
55
        }
56
57
        $extension = pathinfo($configPath, PATHINFO_EXTENSION);
58
        if (! Validator::key($extension)->validate(static::CONSOLE_CONFIG_FACTORIES)) {
59
            throw new InvalidConfigException(
60
                sprintf('Config file "%s" must have .yml, .json or .php extension.', $configPath)
61
            );
62
        }
63
64
        /** @var ConsoleConfigFactoryInterface $factory */
65
        $factoryClass = static::CONSOLE_CONFIG_FACTORIES[$extension];
66
        $factory = new $factoryClass();
67
        return $factory->invoke($configPath);
68
    }
69
}
70