Completed
Pull Request — master (#38)
by Jonathan
02:53
created

Generate::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 2
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPChunkit\Command;
6
7
use PHPChunkit\Events;
8
use PHPChunkit\GenerateTestClass;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\EventDispatcher\EventDispatcher;
15
16
class Generate implements CommandInterface
17
{
18
    /**
19
     * @var GenerateTestClass
20
     */
21
    private $generateTestClass;
22
23
    public function __construct(GenerateTestClass $generateTestClass)
24
    {
25
        $this->generateTestClass = $generateTestClass;
26
    }
27
28
    public function configure(Command $command)
29
    {
30
        $command
31
            ->setDescription('Generate a test skeleton from a class.')
32
            ->addArgument('class', InputArgument::REQUIRED, 'Class to generate test for.')
33
            ->addOption('file', null, InputOption::VALUE_REQUIRED, 'File path to write test to.')
34
        ;
35
    }
36
37
    public function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $class = $input->getArgument('class');
40
41
        $code = $this->generateTestClass->generate($class);
42
43
        if ($file = $input->getOption('file')) {
44
            if (file_exists($file)) {
45
                throw new \InvalidArgumentException(sprintf('%s already exists.', $file));
46
            }
47
48
            $output->writeln(sprintf('Writing test to <info>%s</info>', $file));
49
50
            file_put_contents($file, $code);
51
        } else {
52
            $output->write($code);
53
        }
54
    }
55
}
56