|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Meta\Catalog\Setup\Patch\Data; |
| 6 | + |
| 7 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 8 | +use Magento\Framework\Setup\Patch\DataPatchInterface; |
| 9 | +use Meta\BusinessExtension\Model\System\Config as SystemConfig; |
| 10 | + |
| 11 | +class AddCatalogSwitch implements DataPatchInterface |
| 12 | +{ |
| 13 | + private const CORE_CONFIG_TABLE = "core_config_data"; |
| 14 | + /** |
| 15 | + * @var ModuleDataSetupInterface |
| 16 | + */ |
| 17 | + private ModuleDataSetupInterface $moduleDataSetup; |
| 18 | + /** |
| 19 | + * @var SystemConfig |
| 20 | + */ |
| 21 | + private SystemConfig $systemConfig; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param ModuleDataSetupInterface $moduleDataSetup |
| 25 | + * @param SystemConfig $systemConfig |
| 26 | + */ |
| 27 | + public function __construct( |
| 28 | + ModuleDataSetupInterface $moduleDataSetup, |
| 29 | + SystemConfig $systemConfig |
| 30 | + ) { |
| 31 | + $this->moduleDataSetup = $moduleDataSetup; |
| 32 | + $this->systemConfig = $systemConfig; |
| 33 | + } |
| 34 | + |
| 35 | + public static function getDependencies() |
| 36 | + { |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + public function getAliases() |
| 41 | + { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + public function apply(): void |
| 46 | + { |
| 47 | + $connection = $this->moduleDataSetup->getConnection(); |
| 48 | + $connection->startSetup(); |
| 49 | + |
| 50 | + $stores = $this->systemConfig |
| 51 | + ->getStoreManager() |
| 52 | + ->getStores(false, true); |
| 53 | + |
| 54 | + // update for default config as well |
| 55 | + $this->updateStoreCatalogIntegration(0); |
| 56 | + foreach ($stores as $store) { |
| 57 | + $this->updateStoreCatalogIntegration($store->getId()); |
| 58 | + } |
| 59 | + |
| 60 | + $connection->endSetup(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Updates Store catalog integration |
| 65 | + * |
| 66 | + * @param $storeId |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + private function updateStoreCatalogIntegration($storeId): void |
| 70 | + { |
| 71 | + $connection = $this->moduleDataSetup->getConnection(); |
| 72 | + $coreConfigTable = $connection->getTableName(self::CORE_CONFIG_TABLE); |
| 73 | + |
| 74 | + $isDailyFeedSyncEnabled = $this->fetchValue( |
| 75 | + $storeId, |
| 76 | + "facebook/catalog_management/daily_product_feed" |
| 77 | + ); |
| 78 | + $isCatalogSyncEnabled = $this->fetchValue( |
| 79 | + $storeId, |
| 80 | + "facebook/catalog_management/enable_catalog_sync" |
| 81 | + ); |
| 82 | + $outOfStockThresholdOld = $this->fetchValue( |
| 83 | + $storeId, |
| 84 | + "facebook/inventory_management/out_of_stock_threshold" |
| 85 | + ); |
| 86 | + $outOfStockThresholdNew = $this->fetchValue( |
| 87 | + $storeId, |
| 88 | + "facebook/catalog_management/out_of_stock_threshold" |
| 89 | + ); |
| 90 | + |
| 91 | + if ($isCatalogSyncEnabled == null && $isDailyFeedSyncEnabled != null) { |
| 92 | + $connection->insert($coreConfigTable, [ |
| 93 | + "scope" => $storeId ? "stores" : "default", |
| 94 | + "scope_id" => $storeId, |
| 95 | + "path" => "facebook/catalog_management/enable_catalog_sync", |
| 96 | + "value" => $isDailyFeedSyncEnabled, |
| 97 | + ]); |
| 98 | + } |
| 99 | + |
| 100 | + if ($outOfStockThresholdNew == null && $outOfStockThresholdOld != null) { |
| 101 | + $connection->insert($coreConfigTable, [ |
| 102 | + "scope" => $storeId ? "stores" : "default", |
| 103 | + "scope_id" => $storeId, |
| 104 | + "path" => "facebook/catalog_management/out_of_stock_threshold", |
| 105 | + "value" => $outOfStockThresholdOld, |
| 106 | + ]); |
| 107 | + } |
| 108 | + |
| 109 | + $connection->delete($coreConfigTable, [ |
| 110 | + "scope_id = ?" => $storeId, |
| 111 | + "path = ?" => "facebook/catalog_management/daily_product_feed", |
| 112 | + ]); |
| 113 | + $connection->delete($coreConfigTable, [ |
| 114 | + "scope_id = ?" => $storeId, |
| 115 | + "path = ?" => |
| 116 | + "facebook/inventory_management/out_of_stock_threshold", |
| 117 | + ]); |
| 118 | + $connection->delete($coreConfigTable, [ |
| 119 | + "scope_id = ?" => $storeId, |
| 120 | + "path = ?" => |
| 121 | + "facebook/catalog_management/incremental_product_updates", |
| 122 | + ]); |
| 123 | + $connection->delete($coreConfigTable, [ |
| 124 | + "scope_id = ?" => $storeId, |
| 125 | + "path = ?" => |
| 126 | + "facebook/inventory_management/enable_inventory_upload", |
| 127 | + ]); |
| 128 | + $connection->delete($coreConfigTable, [ |
| 129 | + "scope_id = ?" => $storeId, |
| 130 | + "path = ?" => "facebook/catalog_management/feed_upload_method", |
| 131 | + ]); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Fetch store config value |
| 136 | + * |
| 137 | + * @param $storeId |
| 138 | + * @param $config_path |
| 139 | + * @return mixed|null |
| 140 | + */ |
| 141 | + private function fetchValue($storeId, $config_path): mixed |
| 142 | + { |
| 143 | + $connection = $this->moduleDataSetup->getConnection(); |
| 144 | + $scopeCondition = $connection->prepareSqlCondition("scope_id", [ |
| 145 | + "eq" => $storeId, |
| 146 | + ]); |
| 147 | + $pathCondition = $connection->prepareSqlCondition("path", [ |
| 148 | + "eq" => $config_path, |
| 149 | + ]); |
| 150 | + $query = $connection |
| 151 | + ->select() |
| 152 | + ->from($connection->getTableName(self::CORE_CONFIG_TABLE)) |
| 153 | + ->where($scopeCondition) |
| 154 | + ->where($pathCondition); |
| 155 | + |
| 156 | + $result = $connection->fetchRow($query); |
| 157 | + |
| 158 | + return $result ? $result['value'] : null; |
| 159 | + } |
| 160 | +} |
0 commit comments