DeleteSectionCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 4
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\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
use Tardigrades\Entity\SectionInterface;
20
use Tardigrades\SectionField\Service\SectionManagerInterface;
21
use Tardigrades\SectionField\Service\SectionNotFoundException;
22
23
class DeleteSectionCommand extends SectionCommand
24
{
25
    public function __construct(
26
        SectionManagerInterface $sectionManager
27
    ) {
28
        parent::__construct($sectionManager, 'sf:delete-section');
29
    }
30
31
    protected function configure()
32
    {
33
        $this
34
            ->setDescription('Delete section.')
35
            ->setHelp('Delete section.')
36
        ;
37
    }
38
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        try {
42
            $sections = $this->sectionManager->readAll();
43
            $this->renderTable($output, $sections, 'All installed Sections');
44
            $this->deleteWhatRecord($input, $output);
45
        } catch (SectionNotFoundException $exception) {
46
            $output->writeln("Section not found.");
47
        }
48
    }
49
50
    private function deleteWhatRecord(InputInterface $input, OutputInterface $output): void
51
    {
52
        /** @var SectionInterface $section */
53
        $sections = $this->getSections($input, $output);
54
55
        foreach ($sections as $section) {
56
            $output->writeln('<info>Record with id #' . $section->getId() . ' will be deleted</info>');
57
58
            $sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false);
59
60
            if (!$this->getHelper('question')->ask($input, $output, $sure)) {
61
                $output->writeln('<comment>Cancelled, nothing deleted.</comment>');
62
                return;
63
            }
64
65
            $this->sectionManager->delete($section);
66
67
            $output->writeln('<info>Removed!</info>');
68
        }
69
    }
70
}
71