|
| 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\Directory\Setup\Patch\Data; |
| 9 | + |
| 10 | +use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory; |
| 11 | +use Magento\Framework\AppInterface; |
| 12 | +use Magento\Framework\DataObject; |
| 13 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 14 | +use Magento\Framework\Setup\Patch\DataPatchInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Update region names for Switzerland. |
| 18 | + */ |
| 19 | +class UpdateRegionNamesForSwitzerland implements DataPatchInterface |
| 20 | +{ |
| 21 | + public const SWITZERLAND_COUNTRY_CODE = 'CH'; |
| 22 | + /** |
| 23 | + * Array structure: |
| 24 | + * - key - region code (like the 'code' field in the 'directory_country_region' table) |
| 25 | + * - value - new default name for the region (like the 'default_name' field in the 'directory_country_region' table) |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + public const SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE = [ |
| 29 | + 'FR' => 'Friburg', |
| 30 | + 'GE' => 'Geneva', |
| 31 | + 'LU' => 'Lucerne', |
| 32 | + 'NE' => 'Neuchâtel', |
| 33 | + 'TI' => 'Ticino', |
| 34 | + 'VD' => 'Vaud', |
| 35 | + ]; |
| 36 | + |
| 37 | + private const REGION_KEY_REGION_ID = 'region_id'; |
| 38 | + private const REGION_KEY_COUNTRY_ID = 'country_id'; |
| 39 | + private const REGION_KEY_CODE = 'code'; |
| 40 | + private const REGION_KEY_DEFAULT_NAME = 'default_name'; |
| 41 | + |
| 42 | + private const REGION_NAME_REGION_ID = 'region_id'; |
| 43 | + private const REGION_NAME_KEY_LOCALE = 'locale'; |
| 44 | + private const REGION_NAME_KEY_NAME = 'name'; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var ModuleDataSetupInterface |
| 48 | + */ |
| 49 | + private $moduleDataSetup; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var RegionCollectionFactory |
| 53 | + */ |
| 54 | + private $regionCollectionFactory; |
| 55 | + |
| 56 | + /** |
| 57 | + * @param ModuleDataSetupInterface $moduleDataSetup |
| 58 | + * @param RegionCollectionFactory $regionCollectionFactory |
| 59 | + */ |
| 60 | + public function __construct( |
| 61 | + ModuleDataSetupInterface $moduleDataSetup, |
| 62 | + RegionCollectionFactory $regionCollectionFactory |
| 63 | + ) { |
| 64 | + $this->moduleDataSetup = $moduleDataSetup; |
| 65 | + $this->regionCollectionFactory = $regionCollectionFactory; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @inheritdoc |
| 70 | + */ |
| 71 | + public function apply(): DataPatchInterface |
| 72 | + { |
| 73 | + $regionItems = $this->getRegionItemsToUpdate(); |
| 74 | + if (!count($regionItems)) { |
| 75 | + return $this; |
| 76 | + } |
| 77 | + |
| 78 | + $countryRegionDataToUpdate = []; |
| 79 | + $countryRegionNameDataToUpdate = []; |
| 80 | + $connection = $this->moduleDataSetup->getConnection(); |
| 81 | + foreach ($regionItems as $regionItem) { |
| 82 | + $code = $regionItem->getData(self::REGION_KEY_CODE); |
| 83 | + $newRegionName = self::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE[$code] ?? null; |
| 84 | + if ($newRegionName === null) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + // Collect data to insert into the 'directory_country_region' table |
| 88 | + $countryRegionDataToUpdate[] = [ |
| 89 | + self::REGION_KEY_REGION_ID => $regionItem->getData(self::REGION_KEY_REGION_ID), |
| 90 | + self::REGION_KEY_COUNTRY_ID => $regionItem->getData(self::REGION_KEY_COUNTRY_ID), |
| 91 | + self::REGION_KEY_CODE => $code, |
| 92 | + self::REGION_KEY_DEFAULT_NAME => $newRegionName, |
| 93 | + ]; |
| 94 | + // Collect data to insert into the 'directory_country_region_name' table |
| 95 | + $countryRegionNameDataToUpdate[] = [ |
| 96 | + self::REGION_NAME_KEY_LOCALE => AppInterface::DISTRO_LOCALE_CODE, |
| 97 | + self::REGION_NAME_REGION_ID => $regionItem->getData(self::REGION_KEY_REGION_ID), |
| 98 | + self::REGION_NAME_KEY_NAME => $newRegionName |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + // Update region tables with new region names |
| 103 | + $connection->insertOnDuplicate( |
| 104 | + $this->moduleDataSetup->getTable('directory_country_region'), |
| 105 | + $countryRegionDataToUpdate, |
| 106 | + [self::REGION_KEY_DEFAULT_NAME] |
| 107 | + ); |
| 108 | + $connection->insertOnDuplicate( |
| 109 | + $this->moduleDataSetup->getTable('directory_country_region_name'), |
| 110 | + $countryRegionNameDataToUpdate, |
| 111 | + [self::REGION_NAME_KEY_NAME] |
| 112 | + ); |
| 113 | + |
| 114 | + return $this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @inheritdoc |
| 119 | + */ |
| 120 | + public static function getDependencies(): array |
| 121 | + { |
| 122 | + return [ |
| 123 | + InitializeDirectoryData::class, |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @inheritdoc |
| 129 | + */ |
| 130 | + public function getAliases(): array |
| 131 | + { |
| 132 | + return []; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get region items filtered by 'CH' country and region codes (data to update). |
| 137 | + * |
| 138 | + * @return DataObject[] |
| 139 | + */ |
| 140 | + private function getRegionItemsToUpdate(): array |
| 141 | + { |
| 142 | + $regionCollection = $this->regionCollectionFactory->create(); |
| 143 | + $regionCollection->addCountryFilter(self::SWITZERLAND_COUNTRY_CODE); |
| 144 | + $regionCollection->addRegionCodeFilter(array_keys(self::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE)); |
| 145 | + |
| 146 | + return $regionCollection->getItems(); |
| 147 | + } |
| 148 | +} |
0 commit comments