Completed
Push — develop ( 8b0772...a0b2b6 )
by Paul
04:54
created

GenerateOneCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A getConfiguration() 0 21 3
1
<?php
2
3
namespace PhpUnitGen\Console;
4
5
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigFactoryInterface;
6
use PhpUnitGen\Configuration\ConfigurationInterface\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 GenerateOneCommand.
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 GenerateOneCommand 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-one")
41
            ->setDescription("Generate unit tests skeletons with a custom configuration for only one file")
42
            ->setHelp("Use it to generate your unit tests skeletons from a configuration file and for only one file")
43
            ->addArgument('config-path', InputArgument::REQUIRED, 'The configuration file path.')
44
            ->addArgument('source-file-path', InputArgument::REQUIRED, 'The source file path.')
45
            ->addArgument('target-file-path', InputArgument::REQUIRED, 'The target file path.');
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getConfiguration(InputInterface $input): ConsoleConfigInterface
52
    {
53
        $configPath = $input->getArgument('config-path');
54
        $sourceFile = $input->getArgument('source-file-path');
55
        $targetFile = $input->getArgument('target-file-path');
56
57
        if (! file_exists($configPath)) {
58
            throw new InvalidConfigException(sprintf('Config file "%s" does not exists.', $configPath));
59
        }
60
61
        $extension = pathinfo($configPath, PATHINFO_EXTENSION);
62
        if (! Validator::key($extension)->validate(static::CONSOLE_CONFIG_FACTORIES)) {
63
            throw new InvalidConfigException(
64
                sprintf('Config file "%s" must have .yml, .json or .php extension.', $configPath)
65
            );
66
        }
67
68
        /** @var ConsoleConfigFactoryInterface $factory */
69
        $factoryClass = static::CONSOLE_CONFIG_FACTORIES[$extension];
70
        $factory = new $factoryClass();
71
        return $factory->invokeOneFile($configPath, $sourceFile, $targetFile);
72
    }
73
}
74