ImportGeoNamesCommand.php 2.12 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\InventoryDistanceBasedSourceSelection\Console\Command;

use Magento\InventoryDistanceBasedSourceSelection\Model\ImportGeoNames;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Import geo names from geonames.org
 *
 * {@inheritdoc}
 */
class ImportGeoNamesCommand extends Command
{
    private const COUNTRIES = 'countries'; // Parameter name for countries list

    /**
     * @var ImportGeoNames
     */
    private $importGeoNames;

    /**
     * ImportGeoNamesCommand constructor.
     *
     * @param ImportGeoNames $importGeoNames
     * @param null|string $name
     */
    public function __construct(
        ImportGeoNames $importGeoNames,
        ?string $name = null
    ) {
        parent::__construct($name);
        $this->importGeoNames = $importGeoNames;
    }

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this->setName('inventory-geonames:import')
            ->setDescription('Download and import geo names for source selection algorithm')
            ->setDefinition([
                new InputArgument(
                    self::COUNTRIES,
                    InputArgument::IS_ARRAY | InputArgument::REQUIRED,
                    'List of country codes to import'
                )
            ]);

        parent::configure();
    }

    /**
     * @inheritdoc
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $countries = $input->getArgument(self::COUNTRIES);

        foreach ($countries as $country) {
            $output->write('Importing ' . $country . ': ');
            try {
                $this->importGeoNames->execute($country);
                $output->writeln('OK');
            } catch (\Exception $e) {
                $output->writeln($e->getMessage());
            }
        }

        $output->writeln('Done.');
    }
}