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

GenerateOneCommand::getConfiguration()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 6
nop 1
dl 0
loc 24
rs 8.6845
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 GenerateOneCommand.
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 GenerateOneCommand extends AbstractGenerateCommand
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function configure()
27
    {
28
        $this->setName("gen-one")
29
            ->setDescription("Generate unit tests skeletons with a custom configuration for only one file")
30
            ->setHelp("Use it to generate your unit tests skeletons from a configuration file and for only one file")
31
            ->addArgument('source-file-path', InputArgument::REQUIRED, 'The source file path.')
32
            ->addArgument('target-file-path', InputArgument::REQUIRED, 'The target file path.')
33
            ->addArgument('config-path', InputArgument::OPTIONAL, 'The configuration file path.');
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getConfiguration(InputInterface $input): ConsoleConfigInterface
40
    {
41
        $configPath = 'phpunitgen.yml';
42
        if (($inputConfigPath = $input->getArgument('config-path')) !== null) {
43
            $configPath = $inputConfigPath;
44
        }
45
        $sourceFile = $input->getArgument('source-file-path');
46
        $targetFile = $input->getArgument('target-file-path');
47
48
        if (! file_exists($configPath)) {
49
            throw new InvalidConfigException(sprintf('Config file "%s" does not exists.', $configPath));
50
        }
51
52
        $extension = pathinfo($configPath, PATHINFO_EXTENSION);
53
        if (! Validator::key($extension)->validate(static::CONSOLE_CONFIG_FACTORIES)) {
54
            throw new InvalidConfigException(
55
                sprintf('Config file "%s" must have .yml, .json or .php extension.', $configPath)
56
            );
57
        }
58
59
        /** @var ConsoleConfigFactoryInterface $factory */
60
        $factoryClass = static::CONSOLE_CONFIG_FACTORIES[$extension];
61
        $factory      = new $factoryClass();
62
        return $factory->invokeOneFile($configPath, $sourceFile, $targetFile);
63
    }
64
}
65