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

Generate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 40
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 8 1
A execute() 0 18 3
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