|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Indexer\Console\Command; |
| 9 | + |
| 10 | +use Magento\Framework\App\ObjectManagerFactory; |
| 11 | +use Magento\Framework\Console\Cli; |
| 12 | +use Magento\Framework\Exception\AlreadyExistsException; |
| 13 | +use Magento\Framework\Indexer\IndexerInterface; |
| 14 | +use Magento\Framework\Indexer\StateInterface; |
| 15 | +use Magento\Indexer\Model\ResourceModel\Indexer\State; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Command for setting index status for indexers. |
| 23 | + */ |
| 24 | +class IndexerSetStatusCommand extends AbstractIndexerManageCommand |
| 25 | +{ |
| 26 | + /**#@+ |
| 27 | + * Names of input arguments or options |
| 28 | + */ |
| 29 | + private const INPUT_KEY_STATUS = 'status'; |
| 30 | + /**#@- */ |
| 31 | + |
| 32 | + /** |
| 33 | + * @var State |
| 34 | + */ |
| 35 | + private State $stateResourceModel; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param State $stateResourceModel |
| 39 | + * @param ObjectManagerFactory $objectManagerFactory |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + State $stateResourceModel, |
| 43 | + ObjectManagerFactory $objectManagerFactory |
| 44 | + ) { |
| 45 | + $this->stateResourceModel = $stateResourceModel; |
| 46 | + parent::__construct($objectManagerFactory); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @inheritdoc |
| 51 | + */ |
| 52 | + protected function configure() |
| 53 | + { |
| 54 | + $this->setName('indexer:set-status') |
| 55 | + ->setDescription('Sets the specified indexer status') |
| 56 | + ->setDefinition($this->getInputList()); |
| 57 | + |
| 58 | + parent::configure(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @inheritdoc |
| 63 | + */ |
| 64 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 65 | + { |
| 66 | + $errors = $this->validate($input); |
| 67 | + if ($errors) { |
| 68 | + throw new \InvalidArgumentException(implode("\n", $errors)); |
| 69 | + } |
| 70 | + |
| 71 | + $newStatus = $input->getArgument(self::INPUT_KEY_STATUS); |
| 72 | + $indexers = $this->getIndexers($input); |
| 73 | + $returnValue = Cli::RETURN_SUCCESS; |
| 74 | + |
| 75 | + foreach ($indexers as $indexer) { |
| 76 | + try { |
| 77 | + $this->updateIndexerStatus($indexer, $newStatus, $output); |
| 78 | + } catch (\Exception $e) { |
| 79 | + $output->writeln($e->getMessage()); |
| 80 | + $returnValue = Cli::RETURN_FAILURE; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return $returnValue; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Gets list of arguments for the command. |
| 89 | + * |
| 90 | + * @return InputOption[] |
| 91 | + */ |
| 92 | + public function getInputList(): array |
| 93 | + { |
| 94 | + $modeOptions[] = new InputArgument( |
| 95 | + self::INPUT_KEY_STATUS, |
| 96 | + InputArgument::REQUIRED, |
| 97 | + 'Indexer status type [' . StateInterface::STATUS_INVALID |
| 98 | + . '|' . StateInterface::STATUS_SUSPENDED . '|' . StateInterface::STATUS_VALID . ']' |
| 99 | + ); |
| 100 | + |
| 101 | + return array_merge($modeOptions, parent::getInputList()); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Checks if all CLI command options are provided. |
| 106 | + * |
| 107 | + * @param InputInterface $input |
| 108 | + * @return string[] |
| 109 | + */ |
| 110 | + private function validate(InputInterface $input): array |
| 111 | + { |
| 112 | + $errors = []; |
| 113 | + $acceptedValues = [ |
| 114 | + StateInterface::STATUS_INVALID, |
| 115 | + StateInterface::STATUS_SUSPENDED, |
| 116 | + StateInterface::STATUS_VALID |
| 117 | + ]; |
| 118 | + $inputStatus = $input->getArgument(self::INPUT_KEY_STATUS); |
| 119 | + |
| 120 | + if (!in_array($inputStatus, $acceptedValues, true)) { |
| 121 | + $acceptedValuesString = '"' . implode('", "', $acceptedValues) . '"'; |
| 122 | + $errors[] = sprintf( |
| 123 | + 'Invalid status "%s". Accepted values are %s.', |
| 124 | + $inputStatus, |
| 125 | + $acceptedValuesString |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + return $errors; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Updates the status of a specified indexer. |
| 134 | + * |
| 135 | + * @param IndexerInterface $indexer |
| 136 | + * @param string $newStatus |
| 137 | + * @param OutputInterface $output |
| 138 | + * @return void |
| 139 | + * @throws AlreadyExistsException |
| 140 | + */ |
| 141 | + private function updateIndexerStatus(IndexerInterface $indexer, string $newStatus, OutputInterface $output): void |
| 142 | + { |
| 143 | + $state = $indexer->getState(); |
| 144 | + $previousStatus = $state->getStatus(); |
| 145 | + $this->stateResourceModel->save($state->setStatus($newStatus)); |
| 146 | + $currentStatus = $state->getStatus(); |
| 147 | + |
| 148 | + if ($previousStatus !== $currentStatus) { |
| 149 | + $output->writeln( |
| 150 | + sprintf( |
| 151 | + "Index status for Indexer '%s' was changed from '%s' to '%s'.", |
| 152 | + $indexer->getTitle(), |
| 153 | + $previousStatus, |
| 154 | + $currentStatus |
| 155 | + ) |
| 156 | + ); |
| 157 | + } else { |
| 158 | + $output->writeln(sprintf("Index status for Indexer '%s' has not been changed.", $indexer->getTitle())); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments