Completed
Push — master ( 00d60f...aabe5c )
by Dion
14s
created

GenerateSectionCommand::generateWhatSection()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.4426
c 0
b 0
f 0
cc 7
nc 22
nop 2
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\Command;
15
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
use Tardigrades\SectionField\Generator\Writer\GeneratorFileWriter;
21
use Tardigrades\SectionField\Generator\Writer\Writable;
22
use Tardigrades\SectionField\Generator\GeneratorsInterface;
23
use Tardigrades\SectionField\Service\SectionManagerInterface;
24
use Tardigrades\SectionField\Service\SectionNotFoundException;
25
26
class GenerateSectionCommand extends SectionCommand
27
{
28
    /** @var GeneratorsInterface */
29
    private $entityGenerator;
30
31
    public function __construct(
32
        SectionManagerInterface $sectionManager,
33
        GeneratorsInterface $entityGenerator
34
    ) {
35
        $this->entityGenerator = $entityGenerator;
36
37
        parent::__construct($sectionManager, 'sf:generate-section');
38
    }
39
40
    protected function configure(): void
41
    {
42
        // @codingStandardsIgnoreStart
43
        $this
44
            ->setDescription('Generate a section.')
45
            ->setHelp('After creating a section, you can generate the accompanying files and tables (when using doctrine settings).')
46
            ->addOption(
47
                'yes-mode',
48
                null,
49
                InputOption::VALUE_NONE,
50
                'Automatically say yes when a confirmation is asked'
51
            )
52
            ->addOption(
53
                'all',
54
                'a',
55
                InputOption::VALUE_NONE,
56
                'Generate all available sections'
57
            )
58
        ;
59
        // @codingStandardsIgnoreEnd
60
    }
61
62
    protected function execute(InputInterface $input, OutputInterface $output): void
63
    {
64
        try {
65
            $sections = $this->sectionManager->readAll();
66
            $this->renderTable($output, $sections, 'Available sections.');
67
            $this->generateWhatSection($input, $output);
68
        } catch (SectionNotFoundException $exception) {
69
            $output->writeln("Section not found.");
70
        }
71
    }
72
73
    private function generateWhatSection(InputInterface $input, OutputInterface $output): void
74
    {
75
        if ($input->getOption('all')) {
76
            $sections = $this->sectionManager->readAll();
77
        } else {
78
            $sections = $this->getSections($input, $output);
79
        }
80
81
        foreach ($sections as $section) {
82
            $writables = $this->entityGenerator->generateBySection($section);
83
84
            /** @var Writable $writable */
85
            foreach ($writables as $writable) {
86
                $output->writeln(
87
                    '<info>------------ * TEMPLATE: ' .
88
                    $writable->getNamespace() . $writable->getFilename() .
89
                    ' * ------------</info>'
90
                );
91
                $output->writeln($writable->getTemplate());
92
            }
93
94
            if (!$input->getOption('yes-mode')) {
95
                $sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false);
96
                if (!$this->getHelper('question')->ask($input, $output, $sure)) {
97
                    $output->writeln('<comment>Cancelled, nothing generated.</comment>');
98
                    return;
99
                }
100
            }
101
            foreach ($writables as $writable) {
102
                GeneratorFileWriter::write($writable);
103
            }
104
        }
105
106
        $output->writeln($this->entityGenerator->getBuildMessages());
107
    }
108
}
109