ImportGeoNamesCommand.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryDistanceBasedSourceSelection\Console\Command;
  8. use Magento\InventoryDistanceBasedSourceSelection\Model\ImportGeoNames;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. /**
  14. * Import geo names from geonames.org
  15. *
  16. * {@inheritdoc}
  17. */
  18. class ImportGeoNamesCommand extends Command
  19. {
  20. private const COUNTRIES = 'countries'; // Parameter name for countries list
  21. /**
  22. * @var ImportGeoNames
  23. */
  24. private $importGeoNames;
  25. /**
  26. * ImportGeoNamesCommand constructor.
  27. *
  28. * @param ImportGeoNames $importGeoNames
  29. * @param null|string $name
  30. */
  31. public function __construct(
  32. ImportGeoNames $importGeoNames,
  33. ?string $name = null
  34. ) {
  35. parent::__construct($name);
  36. $this->importGeoNames = $importGeoNames;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. protected function configure()
  42. {
  43. $this->setName('inventory-geonames:import')
  44. ->setDescription('Download and import geo names for source selection algorithm')
  45. ->setDefinition([
  46. new InputArgument(
  47. self::COUNTRIES,
  48. InputArgument::IS_ARRAY | InputArgument::REQUIRED,
  49. 'List of country codes to import'
  50. )
  51. ]);
  52. parent::configure();
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. protected function execute(InputInterface $input, OutputInterface $output)
  58. {
  59. $countries = $input->getArgument(self::COUNTRIES);
  60. foreach ($countries as $country) {
  61. $output->write('Importing ' . $country . ': ');
  62. try {
  63. $this->importGeoNames->execute($country);
  64. $output->writeln('OK');
  65. } catch (\Exception $e) {
  66. $output->writeln($e->getMessage());
  67. }
  68. }
  69. $output->writeln('Done.');
  70. }
  71. }