dionsnoeijen /
sexy-field
| 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\InputOption; |
||||
| 18 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||||
| 19 | use Symfony\Component\Console\Question\Question; |
||||
| 20 | use Symfony\Component\Yaml\Yaml; |
||||
| 21 | use Tardigrades\Entity\Field; |
||||
| 22 | use Symfony\Component\Console\Input\InputArgument; |
||||
| 23 | use Symfony\Component\Console\Input\InputInterface; |
||||
| 24 | use Symfony\Component\Console\Output\OutputInterface; |
||||
| 25 | use Tardigrades\SectionField\Service\FieldManagerInterface; |
||||
| 26 | use Tardigrades\SectionField\Service\FieldNotFoundException; |
||||
| 27 | use Tardigrades\SectionField\ValueObject\FieldConfig; |
||||
| 28 | use Tardigrades\SectionField\ValueObject\Id; |
||||
| 29 | |||||
| 30 | class UpdateFieldCommand extends FieldCommand |
||||
| 31 | { |
||||
| 32 | /** @var QuestionHelper */ |
||||
| 33 | private $questionHelper; |
||||
| 34 | |||||
| 35 | /** @var FieldManagerInterface */ |
||||
| 36 | private $fieldManager; |
||||
| 37 | |||||
| 38 | public function __construct( |
||||
| 39 | FieldManagerInterface $fieldManager |
||||
| 40 | ) { |
||||
| 41 | $this->fieldManager = $fieldManager; |
||||
| 42 | |||||
| 43 | parent::__construct('sf:update-field'); |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | protected function configure() |
||||
| 47 | { |
||||
| 48 | $this |
||||
| 49 | ->setDescription('Updates an existing field.') |
||||
| 50 | ->setHelp('Update field by giving a new or updated field config file.') |
||||
| 51 | ->addArgument('config', InputArgument::REQUIRED, 'The field configuration yml') |
||||
| 52 | ->addOption( |
||||
| 53 | 'yes-mode', |
||||
| 54 | null, |
||||
| 55 | InputOption::VALUE_NONE, |
||||
| 56 | 'Automatically say yes when a field handle is found' |
||||
| 57 | ); |
||||
| 58 | ; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | protected function execute(InputInterface $input, OutputInterface $output) |
||||
| 62 | { |
||||
| 63 | try { |
||||
| 64 | $this->questionHelper = $this->getHelper('question'); |
||||
| 65 | |||||
| 66 | $this->showInstalledFields($input, $output); |
||||
| 67 | } catch (FieldNotFoundException $exception) { |
||||
| 68 | $output->writeln("Field not found"); |
||||
| 69 | } |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | private function showInstalledFields(InputInterface $input, OutputInterface $output): void |
||||
| 73 | { |
||||
| 74 | if (!$input->getOption('yes-mode')) { |
||||
| 75 | $this->renderTable($output, $this->fieldManager->readAll(), 'All installed Fields'); |
||||
| 76 | } |
||||
| 77 | $this->updateWhatRecord($input, $output); |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | private function getField(InputInterface $input, OutputInterface $output): Field |
||||
| 81 | { |
||||
| 82 | $question = new Question('<question>What record do you want to update?</question> (#id): '); |
||||
| 83 | $question->setValidator(function ($id) { |
||||
| 84 | try { |
||||
| 85 | return $this->fieldManager->read(Id::fromInt((int) $id)); |
||||
| 86 | } catch (FieldNotFoundException $exception) { |
||||
| 87 | return null; |
||||
| 88 | } |
||||
| 89 | }); |
||||
| 90 | |||||
| 91 | $field = $this->questionHelper->ask($input, $output, $question); |
||||
| 92 | if (!$field) { |
||||
| 93 | throw new FieldNotFoundException(); |
||||
| 94 | } |
||||
| 95 | return $field; |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 96 | } |
||||
| 97 | |||||
| 98 | private function updateWhatRecord(InputInterface $input, OutputInterface $output): void |
||||
| 99 | { |
||||
| 100 | $config = $input->getArgument('config'); |
||||
| 101 | |||||
| 102 | try { |
||||
| 103 | $fieldConfig = FieldConfig::fromArray( |
||||
| 104 | Yaml::parse( |
||||
| 105 | file_get_contents($config) |
||||
| 106 | ) |
||||
| 107 | ); |
||||
| 108 | } catch (\Exception $exception) { |
||||
| 109 | $output->writeln("<error>Invalid configuration file. {$exception->getMessage()}</error>"); |
||||
| 110 | return; |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | try { |
||||
| 114 | $field = $this->fieldManager->readByHandle($fieldConfig->getHandle()); |
||||
| 115 | |||||
| 116 | if (!$input->getOption('yes-mode')) { |
||||
| 117 | $sure = new ConfirmationQuestion( |
||||
| 118 | '<comment>Do you want to update the field with id: ' . $field->getId() . '?</comment> (y/n) ', |
||||
| 119 | false |
||||
| 120 | ); |
||||
| 121 | |||||
| 122 | if (!$this->getHelper('question')->ask($input, $output, $sure)) { |
||||
| 123 | $output->writeln('<comment>Cancelled, nothing updated.</comment>', false); |
||||
|
0 ignored issues
–
show
false of type false is incompatible with the type integer expected by parameter $options of Symfony\Component\Consol...putInterface::writeln().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 124 | return; |
||||
| 125 | } |
||||
| 126 | } |
||||
| 127 | } catch (FieldNotFoundException $exception) { |
||||
| 128 | $output->writeln( |
||||
| 129 | 'You are trying to update a field with handle: ' . $fieldConfig->getHandle() . '. No field with ' . |
||||
| 130 | 'that handle exists in the database, use sf:create-field if you actually need a new field, or ' . |
||||
| 131 | 'select an existing field id that will be overwritten with this config.' |
||||
| 132 | ); |
||||
| 133 | |||||
| 134 | $sure = new ConfirmationQuestion( |
||||
| 135 | '<comment>Do you want to continue to select a field that will be overwritten?</comment> (y/n) ', |
||||
| 136 | false |
||||
| 137 | ); |
||||
| 138 | |||||
| 139 | if (!$this->getHelper('question')->ask($input, $output, $sure)) { |
||||
| 140 | $output->writeln('<comment>Cancelled, nothing updated.</comment>'); |
||||
| 141 | return; |
||||
| 142 | } |
||||
| 143 | |||||
| 144 | $field = $this->getField($input, $output); |
||||
| 145 | } |
||||
| 146 | |||||
| 147 | $this->fieldManager->updateByConfig($fieldConfig, $field); |
||||
| 148 | if (!$input->getOption('yes-mode')) { |
||||
| 149 | $this->renderTable($output, $this->fieldManager->readAll(), 'Field updated!'); |
||||
| 150 | } else { |
||||
| 151 | $output->writeln('Field updated!'); |
||||
| 152 | } |
||||
| 153 | } |
||||
| 154 | } |
||||
| 155 |