DeleteFieldTypeCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
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\Helper\QuestionHelper;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
use Symfony\Component\Console\Question\Question;
21
use Tardigrades\Entity\FieldType;
22
use Tardigrades\SectionField\SectionFieldInterface\FieldTypeManager;
0 ignored issues
show
Bug introduced by
The type Tardigrades\SectionField...erface\FieldTypeManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Tardigrades\SectionField\Service\FieldTypeManagerInterface;
24
use Tardigrades\SectionField\Service\FieldTypeNotFoundException;
25
use Tardigrades\SectionField\ValueObject\Id;
26
27
class DeleteFieldTypeCommand extends FieldTypeCommand
28
{
29
    /** @var QuestionHelper */
30
    private $questionHelper;
31
32
    /** @var FieldTypeManagerInterface */
33
    private $fieldTypeManager;
34
35
    public function __construct(
36
        FieldTypeManagerInterface $fieldTypeManager
37
    ) {
38
        $this->fieldTypeManager = $fieldTypeManager;
39
40
        parent::__construct('sf:delete-field-type');
41
    }
42
43
    protected function configure(): void
44
    {
45
        $this
46
            ->setDescription('Delete field type.')
47
            ->setHelp('Delete field type.')
48
        ;
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output): void
52
    {
53
        try {
54
            $this->questionHelper = $this->getHelper('question');
55
56
            $this->showInstalledFieldTypes($input, $output);
57
        } catch (FieldTypeNotFoundException $exception) {
58
            $output->writeln("Field type not found");
59
        }
60
    }
61
62
    private function showInstalledFieldTypes(InputInterface $input, OutputInterface $output): void
63
    {
64
        $fieldTypes = $this->fieldTypeManager->readAll();
65
66
        $this->renderTable($output, $fieldTypes, 'Installed field types');
67
        $this->deleteWhatRecord($input, $output);
68
    }
69
70
    private function deleteWhatRecord(InputInterface $input, OutputInterface $output): void
71
    {
72
        $fieldType = $this->getFieldType($input, $output);
73
74
        if ($fieldType->hasFields()) {
75
            $fields = PHP_EOL;
76
            foreach ($fieldType->getFields() as $field) {
77
                $fields .= ' - ' . $field->getHandle() . ':' . PHP_EOL;
78
            }
79
80
            $output->writeln(
81
                '<info>This FieldType has fields that use this type, delete them first. ' .
82
                $fields .
83
                '</info>'
84
            );
85
            return;
86
        }
87
88
        $output->writeln('<info>Record with id #' . $fieldType->getId() . ' will be deleted</info>');
89
90
        $sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false);
91
92
        if (!$this->questionHelper->ask($input, $output, $sure)) {
93
            $output->writeln('<comment>Cancelled, nothing deleted.</comment>');
94
            return;
95
        }
96
97
        $this->fieldTypeManager->delete($fieldType);
98
99
        $output->writeln('<info>Removed!</info>');
100
    }
101
102
    private function getFieldType(InputInterface $input, OutputInterface $output): FieldType
103
    {
104
        $question = new Question('<question>What record do you want to delete?</question> (#id): ');
105
        $question->setValidator(function ($id) {
106
            try {
107
                return $this->fieldTypeManager->read(Id::fromInt((int) $id));
108
            } catch (FieldTypeNotFoundException $exception) {
109
                return null;
110
            }
111
        });
112
113
        $fieldType = $this->questionHelper->ask($input, $output, $question);
114
        if (!$fieldType) {
115
            throw new FieldTypeNotFoundException();
116
        }
117
        return $fieldType;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fieldType could return the type string|true which is incompatible with the type-hinted return Tardigrades\Entity\FieldType. Consider adding an additional type-check to rule them out.
Loading history...
118
    }
119
}
120