|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Console\Command; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Service\StoreNameFetcher; |
| 6 | +use Magento\Framework\App\Area; |
| 7 | +use Magento\Framework\App\State; |
| 8 | +use Magento\Framework\Exception\LocalizedException; |
| 9 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 10 | +use Symfony\Component\Console\Command\Command; |
| 11 | +use Symfony\Component\Console\Input\InputArgument; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 15 | + |
| 16 | +abstract class AbstractStoreCommand extends Command |
| 17 | +{ |
| 18 | + protected const STORE_ARGUMENT = 'store_id'; |
| 19 | + |
| 20 | + protected ?OutputInterface $output = null; |
| 21 | + protected ?InputInterface $input = null; |
| 22 | + |
| 23 | + abstract protected function getCommandName(): string; |
| 24 | + abstract protected function getCommandDescription(): string; |
| 25 | + abstract protected function getAdditionalDefinition(): array; |
| 26 | + abstract protected function getStoreArgumentDescription(): string; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + protected State $state, |
| 30 | + protected StoreNameFetcher $storeNameFetcher, |
| 31 | + ?string $name = null |
| 32 | + ) |
| 33 | + { |
| 34 | + parent::__construct($name); |
| 35 | + } |
| 36 | + |
| 37 | + protected function getCommandPrefix(): string |
| 38 | + { |
| 39 | + return 'algolia:'; |
| 40 | + } |
| 41 | + |
| 42 | + protected function getFullCommandName(): string |
| 43 | + { |
| 44 | + return $this->getCommandPrefix() . $this->getCommandName(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritDoc |
| 49 | + */ |
| 50 | + protected function configure(): void |
| 51 | + { |
| 52 | + $definition = [$this->getStoreArgumentDefinition()]; |
| 53 | + $definition = array_merge($definition, $this->getAdditionalDefinition()); |
| 54 | + |
| 55 | + $this->setName($this->getFullCommandName()) |
| 56 | + ->setDescription($this->getCommandDescription()) |
| 57 | + ->setDefinition($definition); |
| 58 | + |
| 59 | + parent::configure(); |
| 60 | + } |
| 61 | + |
| 62 | + protected function setAreaCode(): void |
| 63 | + { |
| 64 | + try { |
| 65 | + $this->state->setAreaCode(Area::AREA_CRONTAB); |
| 66 | + } catch (LocalizedException $e) { |
| 67 | + // Area code is already set - nothing to do - but report regardless |
| 68 | + $this->output->writeln("Unable to set area code due to the following error: " . $e->getMessage()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param InputInterface $input |
| 74 | + * @return int[] |
| 75 | + * @throws LocalizedException |
| 76 | + */ |
| 77 | + protected function getStoreIds(InputInterface $input): array |
| 78 | + { |
| 79 | + return $this->validateStoreIds((array) $input->getArgument(self::STORE_ARGUMENT)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param array $storeIds |
| 84 | + * @return int[] |
| 85 | + * @throws LocalizedException |
| 86 | + */ |
| 87 | + protected function validateStoreIds(array $storeIds): array |
| 88 | + { |
| 89 | + foreach ($storeIds as $storeId) { |
| 90 | + if (!ctype_digit($storeId) || (int) $storeId < 1) { |
| 91 | + throw new LocalizedException(__("Store ID argument must be an integer")); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return array_map('intval', $storeIds); |
| 96 | + } |
| 97 | + |
| 98 | + protected function getStoreArgumentDefinition(): InputArgument { |
| 99 | + return new InputArgument( |
| 100 | + self::STORE_ARGUMENT, |
| 101 | + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
| 102 | + $this->getStoreArgumentDescription() |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param int[] $storeIds |
| 108 | + * @return string |
| 109 | + */ |
| 110 | + protected function getOperationTargetLabel(array $storeIds): string |
| 111 | + { |
| 112 | + return ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : ''); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Generate a CLI operation announcement based on passed store arguments |
| 117 | + * @param string $msg Use {{target} in message as a placeholder for inserting the generated target label |
| 118 | + * @param int[] $storeIds |
| 119 | + * @return string |
| 120 | + * @throws NoSuchEntityException |
| 121 | + */ |
| 122 | + protected function decorateOperationAnnouncementMessage(string $msg, array $storeIds): string |
| 123 | + { |
| 124 | + $msg = str_replace('{{target}}', $this->getOperationTargetLabel($storeIds), $msg); |
| 125 | + return ($storeIds) |
| 126 | + ? "<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>' |
| 127 | + : "<info>$msg</info>"; |
| 128 | + } |
| 129 | + |
| 130 | + protected function confirmOperation(string $okMessage = '', string $cancelMessage = 'Operation cancelled'): bool |
| 131 | + { |
| 132 | + $helper = $this->getHelper('question'); |
| 133 | + $question = new ConfirmationQuestion('<question>Are you sure wish to proceed? (y/n)</question> ', false); |
| 134 | + if (!$helper->ask($this->input, $this->output, $question)) { |
| 135 | + if ($cancelMessage) { |
| 136 | + $this->output->writeln("<comment>$cancelMessage</comment>"); |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + if ($okMessage) { |
| 142 | + $this->output->writeln("<comment>$okMessage</comment>"); |
| 143 | + } |
| 144 | + return true; |
| 145 | + } |
| 146 | +} |
0 commit comments