Issues (34)

src/Command/DeleteFieldCommand.php (3 issues)

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\Field;
22
use Tardigrades\SectionField\SectionFieldInterface\FieldManager;
0 ignored issues
show
The type Tardigrades\SectionField...dInterface\FieldManager 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\FieldManagerInterface;
24
use Tardigrades\SectionField\Service\FieldNotFoundException;
25
use Tardigrades\SectionField\ValueObject\Id;
26
27
class DeleteFieldCommand extends FieldCommand
28
{
29
    /** @var QuestionHelper */
30
    private $questionHelper;
31
32
    /** @var FieldManagerInterface */
33
    private $fieldManager;
34
35
    public function __construct(
36
        FieldManagerInterface $fieldManager
37
    ) {
38
        $this->fieldManager = $fieldManager;
39
40
        parent::__construct('sf:delete-field');
41
    }
42
43
    protected function configure(): void
44
    {
45
        $this
46
            ->setDescription('Delete field.')
47
            ->setHelp('Shows a list of installed fields, choose the field you would like to delete.');
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output): void
51
    {
52
        $this->questionHelper = $this->getHelper('question');
53
54
        try {
55
            $this->showInstalledFields($input, $output);
56
        } catch (FieldNotFoundException $exception) {
57
            $output->writeln("Field not found.");
58
        }
59
    }
60
61
    private function showInstalledFields(InputInterface $input, OutputInterface $output): void
62
    {
63
        $fields = $this->fieldManager->readAll();
64
65
        $this->renderTable($output, $fields, 'All installed Fields');
66
        $this->deleteWhatRecord($input, $output);
67
    }
68
69
    private function deleteWhatRecord(InputInterface $input, OutputInterface $output): void
70
    {
71
        $field = $this->getField($input, $output);
72
73
        $output->writeln('<info>Record with id #' . $field->getId() . ' will be deleted</info>');
74
75
        $sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false);
76
77
        if (!$this->questionHelper->ask($input, $output, $sure)) {
78
            $output->writeln('<comment>Cancelled, nothing deleted.</comment>');
79
            return;
80
        }
81
        $this->fieldManager->delete($field);
0 ignored issues
show
It seems like $field can also be of type null; however, parameter $entity of Tardigrades\SectionField...agerInterface::delete() does only seem to accept Tardigrades\Entity\FieldInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $this->fieldManager->delete(/** @scrutinizer ignore-type */ $field);
Loading history...
82
83
        $output->writeln('<info>Removed!</info>');
84
    }
85
86
    private function getField(InputInterface $input, OutputInterface $output): ?Field
87
    {
88
        $question = new Question('<question>What record do you want to delete?</question> (#id): ');
89
        $question->setValidator(function ($id) {
90
            try {
91
                return $this->fieldManager->read(Id::fromInt((int) $id));
92
            } catch (FieldNotFoundException $exception) {
93
                return null;
94
            }
95
        });
96
97
        $field = $this->questionHelper->ask($input, $output, $question);
98
        if (!$field) {
99
            throw new FieldNotFoundException();
100
        }
101
        return $field;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $field could return the type string|true which is incompatible with the type-hinted return Tardigrades\Entity\Field|null. Consider adding an additional type-check to rule them out.
Loading history...
102
    }
103
}
104