|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * CoreShop |
| 7 | + * |
| 8 | + * This source file is available under two different licenses: |
| 9 | + * - GNU General Public License version 3 (GPLv3) |
| 10 | + * - CoreShop Commercial License (CCL) |
| 11 | + * Full copyright and license information is available in |
| 12 | + * LICENSE.md which is distributed with this source code. |
| 13 | + * |
| 14 | + * @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.com) |
| 15 | + * @license https://www.coreshop.com/license GPLv3 and CCL |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +namespace CoreShop\Bundle\CoreBundle\Command; |
| 20 | + |
| 21 | +use CoreShop\Component\Address\Model\StateInterface; |
| 22 | +use CoreShop\Component\Address\Repository\CountryRepositoryInterface; |
| 23 | +use CoreShop\Component\Resource\Factory\FactoryInterface; |
| 24 | +use CoreShop\Component\Resource\Repository\RepositoryInterface; |
| 25 | +use Doctrine\ORM\EntityManagerInterface; |
| 26 | +use Pimcore\Tool; |
| 27 | +use Rinvex\Country\CountryLoader; |
| 28 | +use Symfony\Component\Console\Command\Command; |
| 29 | +use Symfony\Component\Console\Input\InputArgument; |
| 30 | +use Symfony\Component\Console\Input\InputInterface; |
| 31 | +use Symfony\Component\Console\Input\InputOption; |
| 32 | +use Symfony\Component\Console\Output\OutputInterface; |
| 33 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 34 | + |
| 35 | +final class SetupStatesCommand extends Command |
| 36 | +{ |
| 37 | + public function __construct( |
| 38 | + private CountryRepositoryInterface $countryRepository, |
| 39 | + private RepositoryInterface $stateRepository, |
| 40 | + private FactoryInterface $stateFactory, |
| 41 | + private EntityManagerInterface $entityManager, |
| 42 | + ) { |
| 43 | + parent::__construct(); |
| 44 | + } |
| 45 | + |
| 46 | + protected function configure(): void |
| 47 | + { |
| 48 | + $this |
| 49 | + ->setName('coreshop:setup:states') |
| 50 | + ->setDescription('Create states/regions for specified countries.') |
| 51 | + ->addArgument( |
| 52 | + 'countries', |
| 53 | + InputArgument::REQUIRED, |
| 54 | + 'Comma-separated list of country ISO codes (e.g., DE,US,FR)', |
| 55 | + ) |
| 56 | + ->addOption( |
| 57 | + 'activate-country', |
| 58 | + null, |
| 59 | + InputOption::VALUE_NONE, |
| 60 | + 'Also activate the country if not already active', |
| 61 | + ) |
| 62 | + ->setHelp( |
| 63 | + <<<EOT |
| 64 | +The <info>%command.name%</info> command creates states/regions for specified countries. |
| 65 | +
|
| 66 | +Examples: |
| 67 | + <info>php bin/console %command.name% DE</info> |
| 68 | + <info>php bin/console %command.name% DE,US,FR</info> |
| 69 | + <info>php bin/console %command.name% DE --activate-country</info> |
| 70 | +EOT |
| 71 | + ) |
| 72 | + ; |
| 73 | + } |
| 74 | + |
| 75 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 76 | + { |
| 77 | + $io = new SymfonyStyle($input, $output); |
| 78 | + |
| 79 | + $countriesArg = $input->getArgument('countries'); |
| 80 | + $activateCountry = $input->getOption('activate-country'); |
| 81 | + $countryCodes = array_map('strtoupper', array_map('trim', explode(',', $countriesArg))); |
| 82 | + |
| 83 | + $languages = Tool::getValidLanguages(); |
| 84 | + |
| 85 | + $io->title('CoreShop States Setup'); |
| 86 | + $io->writeln(sprintf('Setting up states for countries: <info>%s</info>', implode(', ', $countryCodes))); |
| 87 | + |
| 88 | + $createdStates = 0; |
| 89 | + $skippedStates = 0; |
| 90 | + $countriesProcessed = 0; |
| 91 | + $activatedCountries = 0; |
| 92 | + |
| 93 | + foreach ($countryCodes as $countryCode) { |
| 94 | + $io->section(sprintf('Processing country: %s', $countryCode)); |
| 95 | + |
| 96 | + // Check if country exists in database |
| 97 | + $country = $this->countryRepository->findByCode($countryCode); |
| 98 | + if ($country === null) { |
| 99 | + $io->warning(sprintf('Country with code "%s" not found in database. Run fixtures first or install CoreShop.', $countryCode)); |
| 100 | + |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + // Load country data from Rinvex |
| 105 | + try { |
| 106 | + $rinvexCountry = CountryLoader::country($countryCode); |
| 107 | + } catch (\Exception $e) { |
| 108 | + $io->warning(sprintf('Country data not found for code "%s" in Rinvex data: %s', $countryCode, $e->getMessage())); |
| 109 | + |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + // Activate country if requested |
| 114 | + if ($activateCountry && !$country->getActive()) { |
| 115 | + $country->setActive(true); |
| 116 | + $this->entityManager->persist($country); |
| 117 | + $activatedCountries++; |
| 118 | + $io->writeln(sprintf(' <comment>Activated country: %s</comment>', $countryCode)); |
| 119 | + } |
| 120 | + |
| 121 | + // Get divisions (states/regions) |
| 122 | + $divisions = $rinvexCountry->getDivisions(); |
| 123 | + |
| 124 | + if (!is_array($divisions) || empty($divisions)) { |
| 125 | + $io->writeln(sprintf(' <comment>No divisions/states found for %s</comment>', $countryCode)); |
| 126 | + $countriesProcessed++; |
| 127 | + |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + foreach ($divisions as $isoCode => $division) { |
| 132 | + if (!$division['name']) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + // Check if state already exists |
| 137 | + $existingState = $this->stateRepository->findOneBy([ |
| 138 | + 'isoCode' => $isoCode, |
| 139 | + 'country' => $country, |
| 140 | + ]); |
| 141 | + |
| 142 | + if ($existingState !== null) { |
| 143 | + $skippedStates++; |
| 144 | + $io->writeln(sprintf(' <comment>Skipping existing state: %s (%s)</comment>', $division['name'], $isoCode), OutputInterface::VERBOSITY_VERBOSE); |
| 145 | + |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @var StateInterface $state |
| 151 | + */ |
| 152 | + $state = $this->stateFactory->createNew(); |
| 153 | + |
| 154 | + foreach ($languages as $lang) { |
| 155 | + $state->setName($division['name'], $lang); |
| 156 | + } |
| 157 | + |
| 158 | + $state->setIsoCode($isoCode); |
| 159 | + $state->setCountry($country); |
| 160 | + $state->setActive(true); |
| 161 | + |
| 162 | + $this->entityManager->persist($state); |
| 163 | + $createdStates++; |
| 164 | + |
| 165 | + $io->writeln(sprintf(' <info>Created state: %s (%s)</info>', $division['name'], $isoCode), OutputInterface::VERBOSITY_VERBOSE); |
| 166 | + } |
| 167 | + |
| 168 | + $countriesProcessed++; |
| 169 | + } |
| 170 | + |
| 171 | + $this->entityManager->flush(); |
| 172 | + |
| 173 | + $io->success([ |
| 174 | + sprintf('Countries processed: %d', $countriesProcessed), |
| 175 | + sprintf('States created: %d', $createdStates), |
| 176 | + sprintf('States skipped (already exist): %d', $skippedStates), |
| 177 | + sprintf('Countries activated: %d', $activatedCountries), |
| 178 | + ]); |
| 179 | + |
| 180 | + return Command::SUCCESS; |
| 181 | + } |
| 182 | +} |
0 commit comments