Completed
Push — develop ( 67e161...8b0772 )
by Paul
05:50
created

AbstractGenerateCommand::generateForDirectory()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 4
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Console;
4
5
use League\Flysystem\FilesystemInterface;
6
use PhpUnitGen\Configuration\ConsoleConfigInterface;
7
use PhpUnitGen\Container\ConsoleContainerFactoryInterface;
8
use PhpUnitGen\Container\ContainerInterface;
9
use PhpUnitGen\Exception\InvalidConfigException;
10
use PhpUnitGen\Exception\ParsingException;
11
use PhpUnitGen\Parser\ParserInterface\DirectoryParserInterface;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Class AbstractGenerateCommand.
18
 *
19
 * @author     Paul Thébaud <[email protected]>.
20
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
21
 * @license    https://opensource.org/licenses/MIT The MIT license.
22
 * @link       https://github.com/paul-thebaud/phpunit-generator
23
 * @since      Class available since Release 2.0.0.
24
 */
25
abstract class AbstractGenerateCommand extends Command
26
{
27
    /**
28
     * @var ConsoleContainerFactoryInterface $containerFactory A container factory to create container.
29
     */
30
    protected $containerFactory;
31
32
    /**
33
     * @var DirectoryParserInterface $directoryParser A directory parser to parse each files in directory.
34
     */
35
    protected $directoryParser;
36
37
    /**
38
     * @var FilesystemInterface $fileSystem A file system to navigate through files.
39
     */
40
    protected $fileSystem;
41
42
    /**
43
     * GenerateCommand constructor.
44
     *
45
     * @param ConsoleContainerFactoryInterface $containerFactory A container factory to create container.
46
     */
47
    public function __construct(ConsoleContainerFactoryInterface $containerFactory)
48
    {
49
        parent::__construct();
50
51
        $this->containerFactory = $containerFactory;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        try {
60
            $config = $this->getConfiguration($input);
61
        } catch (InvalidConfigException $exception) {
62
            $output->writeln(
63
                sprintf("<error>Error during configuration parsing:\n\n%s</error>", $exception->getMessage())
64
            );
65
            return -1;
66
        }
67
68
        $container = $this->containerFactory->invoke($config, $output);
69
70
        $this->directoryParser = $container->get(DirectoryParserInterface::class);
71
        $this->fileSystem = $container->get(FilesystemInterface::class);
72
73
        /** @todo Extract this in another class */
74
75
        foreach ($config->getDirectories() as $sourceDirectory => $targetDirectory) {
76
            if ($this->generateForDirectory($output, $config, $sourceDirectory, $targetDirectory)) {
77
                return -1;
78
            }
79
        }
80
81
        return 1;
82
    }
83
84
    /**
85
     * Generate unit tests skeletons for a source directory to a target directory.
86
     *
87
     * @param OutputInterface        $output          An output to display message.
88
     * @param ConsoleConfigInterface $config          A configuration.
89
     * @param string                 $sourceDirectory A source directory to get files to parse.
90
     * @param string                 $targetDirectory A target directory to put generated files.
91
     *
92
     * @return bool True there was no (important) error during parsing.
93
     */
94
    protected function generateForDirectory(
95
        OutputInterface $output,
96
        ConsoleConfigInterface $config,
97
        string $sourceDirectory,
98
        string $targetDirectory
99
    ): bool {
100
        try {
101
            $directoryModel = $this->directoryParser->parse($sourceDirectory, $targetDirectory);
0 ignored issues
show
Unused Code introduced by
The assignment to $directoryModel is dead and can be removed.
Loading history...
102
        } catch (ParsingException $exception) {
103
            $output->writeln(sprintf(
104
                "<error>Parsing directory \"%s\" failed for the following reason:\n\n%s</error>",
105
                $sourceDirectory,
106
                $exception->getMessage()
107
            ));
108
            if (! $config->hasIgnore()) {
109
                return false;
110
            }
111
        }
112
113
        /** @todo Parse the $directoryModel to generate unit tests skeletons */
114
115
        $output->writeln(sprintf('<info>Parsing directory "%s" completed.</info>', $sourceDirectory));
116
117
        return true;
118
    }
119
120
    /**
121
     * Build a configuration from a configuration file path.
122
     *
123
     * @param InputInterface $input An input interface to retrieve command argument.
124
     *
125
     * @return ConsoleConfigInterface The created configuration.
126
     *
127
     * @throws InvalidConfigException If an error occurs during process.
128
     */
129
    protected abstract function getConfiguration(InputInterface $input): ConsoleConfigInterface;
130
}
131