Completed
Push — develop ( 1b34c6...481302 )
by Paul
02:47
created

GenerateCommand::getConfiguration()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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