UpdateFieldTypeCommand::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\Question\ConfirmationQuestion;
18
use Symfony\Component\Console\Question\Question;
19
use Tardigrades\Entity\FieldType;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Tardigrades\SectionField\Service\FieldNotFoundException;
23
use Tardigrades\SectionField\Service\FieldTypeManagerInterface;
24
use Tardigrades\SectionField\Service\FieldTypeNotFoundException;
25
use Tardigrades\SectionField\ValueObject\FullyQualifiedClassName;
26
use Tardigrades\SectionField\ValueObject\Id;
27
28
class UpdateFieldTypeCommand extends FieldTypeCommand
29
{
30
    /** @var QuestionHelper */
31
    private $questionHelper;
32
33
    /** @var FieldTypeManagerInterface */
34
    private $fieldTypeManager;
35
36
    public function __construct(
37
        FieldTypeManagerInterface $fieldTypeManager
38
    ) {
39
        $this->fieldTypeManager = $fieldTypeManager;
40
41
        parent::__construct('sf:update-field-type');
42
    }
43
44
    protected function configure()
45
    {
46
        $this
47
            ->setDescription('Updates a field type.')
48
            ->setHelp('Update a field type based on new fully qualified class name.')
49
        ;
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        try {
55
            $this->questionHelper = $this->getHelper('question');
56
57
            $this->showInstalledFieldTypes($input, $output);
58
        } catch (FieldTypeNotFoundException $exception) {
59
            $output->writeln("Field type not found");
60
        }
61
    }
62
63
    private function showInstalledFieldTypes(InputInterface $input, OutputInterface $output)
64
    {
65
        $fieldTypes = $this->fieldTypeManager->readAll();
66
67
        $this->renderTable($output, $fieldTypes, 'The * column is what can be updated, type is updated automatically.');
68
        $this->updateWhatRecord($input, $output);
69
    }
70
71
    private function getFieldType(InputInterface $input, OutputInterface $output): FieldType
72
    {
73
        $question = new Question('<question>What record do you want to update?</question> (#id): ');
74
        $question->setValidator(function ($id) {
75
            try {
76
                return $this->fieldTypeManager->read(Id::fromInt((int) $id));
77
            } catch (FieldTypeNotFoundException $exception) {
78
                return null;
79
            }
80
        });
81
82
        $fieldType = $this->questionHelper->ask($input, $output, $question);
83
        if (!$fieldType) {
84
            throw new FieldTypeNotFoundException();
85
        }
86
        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...
87
    }
88
89
    private function getNamespace(
90
        InputInterface $input,
91
        OutputInterface $output,
92
        FieldType $fieldType
93
    ): FullyQualifiedClassName {
94
        $updateQuestion = new Question(
95
            '<question>Give a new fully qualified class name</question> (old: ' .
96
            $fieldType->getFullyQualifiedClassName() .
97
            '): '
98
        );
99
        $updateQuestion->setValidator(function ($fullyQualifiedClassName) {
100
            return FullyQualifiedClassName::fromString($fullyQualifiedClassName);
101
        });
102
103
        return $this->questionHelper->ask($input, $output, $updateQuestion);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->questionHe...utput, $updateQuestion) could return the type boolean|null|string which is incompatible with the type-hinted return Tardigrades\SectionField...FullyQualifiedClassName. Consider adding an additional type-check to rule them out.
Loading history...
104
    }
105
106
    private function updateWhatRecord(InputInterface $input, OutputInterface $output)
107
    {
108
        $fieldType = $this->getFieldType($input, $output);
109
        $namespace = $this->getNamespace($input, $output, $fieldType);
110
111
        $output->writeln(
112
            '<info>Record with id #' .
113
            $fieldType->getId() .
114
            ' will be updated with namespace: </info>' .
115
            (string) $namespace
116
        );
117
118
        $sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false);
119
120
        if (!$this->questionHelper->ask($input, $output, $sure)) {
121
            $output->writeln('<comment>Cancelled, nothing updated.</comment>');
122
            return;
123
        }
124
125
        $this->updateRecord($input, $output, $fieldType, $namespace);
126
    }
127
128
    private function updateRecord(
129
        InputInterface $input,
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed. ( Ignorable by Annotation )

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

129
        /** @scrutinizer ignore-unused */ InputInterface $input,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
        OutputInterface $output,
131
        FieldType $fieldType,
132
        FullyQualifiedClassName $fullyQualifiedClassName
133
    ) {
134
        $fieldType->setType($fullyQualifiedClassName->getClassName());
135
        $fieldType->setFullyQualifiedClassName((string) $fullyQualifiedClassName);
136
        $this->fieldTypeManager->update();
137
        $this->renderTable(
138
            $output,
139
            [$fieldType],
140
            'The * column is what can be updated, type is updated automatically.'
141
        );
142
143
        $output->writeln('<info>FieldTypeInterface Updated!</info>');
144
    }
145
}
146