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\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\LanguageInterface; |
||||
| 22 | use Tardigrades\SectionField\Service\LanguageManagerInterface; |
||||
| 23 | use Tardigrades\SectionField\Service\LanguageNotFoundException; |
||||
| 24 | use Tardigrades\SectionField\ValueObject\Id; |
||||
| 25 | |||||
| 26 | class DeleteLanguageCommand extends LanguageCommand |
||||
| 27 | { |
||||
| 28 | /** @var LanguageManagerInterface */ |
||||
| 29 | private $languageManager; |
||||
| 30 | |||||
| 31 | /** @var QuestionHelper */ |
||||
| 32 | private $questionHelper; |
||||
| 33 | |||||
| 34 | public function __construct( |
||||
| 35 | LanguageManagerInterface $languageManager |
||||
| 36 | ) { |
||||
| 37 | $this->languageManager = $languageManager; |
||||
| 38 | |||||
| 39 | parent::__construct('sf:delete-language'); |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | protected function configure() |
||||
| 43 | { |
||||
| 44 | $this |
||||
| 45 | ->setDescription('Delete language') |
||||
| 46 | ->setHelp('Shows a list of installed languages, choose the language you would like to delete.'); |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | protected function execute(InputInterface $input, OutputInterface $output) |
||||
| 50 | { |
||||
| 51 | $this->questionHelper = $this->getHelper('question'); |
||||
| 52 | |||||
| 53 | $this->showInstalledLanguages($input, $output); |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | private function showInstalledLanguages(InputInterface $input, OutputInterface $output): void |
||||
| 57 | { |
||||
| 58 | $languages = $this->languageManager->readAll(); |
||||
| 59 | |||||
| 60 | $this->renderTable($output, $languages, 'All installed languages'); |
||||
| 61 | $this->deleteWhatRecord($input, $output); |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | private function getLanguageRecord(InputInterface $input, OutputInterface $output): ?LanguageInterface |
||||
| 65 | { |
||||
| 66 | $question = new Question('<question>What record do you want to delete?</question> (#id): '); |
||||
| 67 | $question->setValidator(function ($id) { |
||||
| 68 | try { |
||||
| 69 | return $this->languageManager->read(Id::fromInt((int) $id)); |
||||
| 70 | } catch (LanguageNotFoundException $exception) { |
||||
| 71 | return null; |
||||
| 72 | } |
||||
| 73 | }); |
||||
| 74 | |||||
| 75 | $languageRecord = $this->questionHelper->ask($input, $output, $question); |
||||
| 76 | if (!$languageRecord) { |
||||
| 77 | throw new LanguageNotFoundException(); |
||||
| 78 | } |
||||
| 79 | return $languageRecord; |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 80 | } |
||||
| 81 | |||||
| 82 | private function deleteWhatRecord(InputInterface $input, OutputInterface $output): void |
||||
| 83 | { |
||||
| 84 | try { |
||||
| 85 | $language = $this->getLanguageRecord($input, $output); |
||||
| 86 | } catch (LanguageNotFoundException $exception) { |
||||
| 87 | $output->writeln("Language not found."); |
||||
| 88 | return; |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | $output->writeln('<info>Record with id #' . $language->getId() . ' will be deleted</info>'); |
||||
| 92 | |||||
| 93 | $sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false); |
||||
| 94 | |||||
| 95 | if (!$this->questionHelper->ask($input, $output, $sure)) { |
||||
| 96 | $output->writeln('<comment>Cancelled, nothing deleted.</comment>'); |
||||
| 97 | return; |
||||
| 98 | } |
||||
| 99 | $this->languageManager->delete($language); |
||||
|
0 ignored issues
–
show
It seems like
$language can also be of type null; however, parameter $entity of Tardigrades\SectionField...agerInterface::delete() does only seem to accept Tardigrades\Entity\LanguageInterface, 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
Loading history...
|
|||||
| 100 | |||||
| 101 | try { |
||||
| 102 | $this->renderTable($output, $this->languageManager->readAll(), 'Removed!'); |
||||
| 103 | } catch (LanguageNotFoundException $exception) { |
||||
| 104 | $output->writeln('No languages left'); |
||||
| 105 | } |
||||
| 106 | } |
||||
| 107 | } |
||||
| 108 |