|
| 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\Paypal\Setup\Patch\Data; |
| 9 | + |
| 10 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 11 | +use Magento\Framework\Setup\Patch\DataPatchInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Update bml config settings to the equivalent paylater settings |
| 15 | + */ |
| 16 | +class UpdateBmltoPayLater implements DataPatchInterface |
| 17 | +{ |
| 18 | + /** |
| 19 | + * BML config path |
| 20 | + */ |
| 21 | + private const BMLPATH = 'payment/paypal_express_bml/'; |
| 22 | + |
| 23 | + /** |
| 24 | + * PayLater config path |
| 25 | + */ |
| 26 | + private const PAYLATERPATH = 'payment/paypal_paylater/'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var array |
| 30 | + */ |
| 31 | + private $bmlToPayLater = [ |
| 32 | + [ |
| 33 | + 'pages' => ['productpage'], |
| 34 | + 'data' => [ |
| 35 | + 'position' => [ |
| 36 | + 'name' =>'position', |
| 37 | + 'values' => [['options' =>['0' => 'header', '1' => 'near_pp_button']]], |
| 38 | + 'requires' => [ |
| 39 | + 'header' => ['name' => 'stylelayout', 'value' => 'flex'], |
| 40 | + 'near_pp_button' => ['name' => 'stylelayout', 'value' => 'text'] |
| 41 | + ] |
| 42 | + ], |
| 43 | + 'size' => [ |
| 44 | + 'name' => 'ratio', |
| 45 | + 'values' => [ |
| 46 | + [ |
| 47 | + 'options' => [ |
| 48 | + '190x100' => '8x1', |
| 49 | + '234x60' => '8x1', |
| 50 | + '300x50' => '8x1', |
| 51 | + '468x60' => '8x1', |
| 52 | + '728x90' => '20x1', |
| 53 | + '800x66' => '20x1' |
| 54 | + ], |
| 55 | + 'depends' => ['name' => 'position', 'value' => '0'] |
| 56 | + ] |
| 57 | + ] |
| 58 | + ] |
| 59 | + ] |
| 60 | + ], |
| 61 | + [ |
| 62 | + 'pages' => ['homepage'], |
| 63 | + 'data' => [ |
| 64 | + 'position' => [ |
| 65 | + 'name' =>'position', |
| 66 | + 'values' => [['options' => ['0' => 'header', '1' => 'sidebar']]], |
| 67 | + 'requires' => [ |
| 68 | + 'header' => ['name' => 'stylelayout', 'value' => 'flex'], |
| 69 | + 'sidebar' => ['name' => 'stylelayout', 'value' => 'flex'] |
| 70 | + ] |
| 71 | + ], |
| 72 | + 'size' => [ |
| 73 | + 'name' => 'ratio', |
| 74 | + 'values' => [ |
| 75 | + [ |
| 76 | + 'options' => [ |
| 77 | + '190x100' => '8x1', |
| 78 | + '234x60' => '8x1', |
| 79 | + '300x50' => '8x1', |
| 80 | + '468x60' => '8x1', |
| 81 | + '728x90' => '20x1', |
| 82 | + '800x66' => '20x1' |
| 83 | + ], |
| 84 | + 'depends' => ['name' => 'position', 'value' => '0'] |
| 85 | + ], |
| 86 | + [ |
| 87 | + 'options' => [ |
| 88 | + '120x90' => '1x1', |
| 89 | + '190x100' => '1x1', |
| 90 | + '234x60' => '1x1', |
| 91 | + '120x240' => '1x1', |
| 92 | + '120x600' => '1x4', |
| 93 | + '234x400' => '1x1', |
| 94 | + '250x250' => '1x1' |
| 95 | + ], |
| 96 | + 'depends' => ['name' => 'position', 'value' => '1'] |
| 97 | + ] |
| 98 | + ] |
| 99 | + ] |
| 100 | + ] |
| 101 | + ] |
| 102 | + ]; |
| 103 | + |
| 104 | + /** |
| 105 | + * @var ModuleDataSetupInterface |
| 106 | + */ |
| 107 | + private $moduleDataSetup; |
| 108 | + |
| 109 | + /** |
| 110 | + * PrepareInitialConfig constructor. |
| 111 | + * @param ModuleDataSetupInterface $moduleDataSetup |
| 112 | + */ |
| 113 | + public function __construct( |
| 114 | + ModuleDataSetupInterface $moduleDataSetup |
| 115 | + ) { |
| 116 | + $this->moduleDataSetup = $moduleDataSetup; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @inheritdoc |
| 121 | + */ |
| 122 | + public function apply() |
| 123 | + { |
| 124 | + $this->moduleDataSetup->getConnection()->startSetup(); |
| 125 | + |
| 126 | + $select = $this->moduleDataSetup->getConnection()->select() |
| 127 | + ->from( |
| 128 | + $this->moduleDataSetup->getTable('core_config_data'), |
| 129 | + ['path', 'value'] |
| 130 | + ) |
| 131 | + ->where('path LIKE ?', self::BMLPATH . '%'); |
| 132 | + $bmlSettings = $this->moduleDataSetup->getConnection()->fetchPairs($select); |
| 133 | + |
| 134 | + foreach ($bmlSettings as $bmlPath => $bmlValue) { |
| 135 | + $setting = str_replace(self::BMLPATH, '', $bmlPath); |
| 136 | + $settingParts = explode('_', $setting); |
| 137 | + $page = $settingParts[0]; |
| 138 | + $setting = $settingParts[1]; |
| 139 | + $payLaterPath = self::PAYLATERPATH . $page; |
| 140 | + |
| 141 | + if (array_key_exists(self::BMLPATH . $page . '_display', $bmlSettings) |
| 142 | + && $bmlSettings[self::BMLPATH . $page . '_display'] === '0' |
| 143 | + ) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + foreach ($this->bmlToPayLater as $bmlToPayLaterSetting) { |
| 148 | + if (in_array($page, $bmlToPayLaterSetting['pages']) |
| 149 | + && array_key_exists($setting, $bmlToPayLaterSetting['data']) |
| 150 | + ) { |
| 151 | + $pageSetting = $bmlToPayLaterSetting['data'][$setting]; |
| 152 | + |
| 153 | + $this->convertAndSaveConfigValues($bmlSettings, $pageSetting, $payLaterPath, $page, $bmlValue); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return $this->moduleDataSetup->getConnection()->endSetup(); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Convert BML settings to PayLater and save |
| 163 | + * |
| 164 | + * @param array $bmlSettings |
| 165 | + * @param array $pageSetting |
| 166 | + * @param string $payLaterPath |
| 167 | + * @param string $page |
| 168 | + * @param string $bmlValue |
| 169 | + */ |
| 170 | + private function convertAndSaveConfigValues( |
| 171 | + array $bmlSettings, |
| 172 | + array $pageSetting, |
| 173 | + string $payLaterPath, |
| 174 | + string $page, |
| 175 | + string $bmlValue |
| 176 | + ) { |
| 177 | + foreach ($pageSetting['values'] as $pageSettingValues) { |
| 178 | + $dependsPath = isset($pageSettingValues['depends']) |
| 179 | + ? self::BMLPATH . $page . '_' . $pageSettingValues['depends']['name'] |
| 180 | + : ''; |
| 181 | + |
| 182 | + if (!array_key_exists('depends', $pageSettingValues) |
| 183 | + || (array_key_exists($dependsPath, $bmlSettings) |
| 184 | + && $bmlSettings[$dependsPath] === $pageSettingValues['depends']['value']) |
| 185 | + ) { |
| 186 | + $path = $payLaterPath . '_' . $pageSetting['name']; |
| 187 | + $value = $pageSettingValues['options'][$bmlValue]; |
| 188 | + $this->moduleDataSetup->getConnection()->insertOnDuplicate( |
| 189 | + $this->moduleDataSetup->getTable('core_config_data'), |
| 190 | + [ |
| 191 | + 'scope' => 'default', |
| 192 | + 'scope_id' => 0, |
| 193 | + 'path' => $path, |
| 194 | + 'value' => $value |
| 195 | + ] |
| 196 | + ); |
| 197 | + if (array_key_exists('requires', $pageSetting) |
| 198 | + && array_key_exists($value, $pageSetting['requires']) |
| 199 | + ) { |
| 200 | + $requiredPath = $payLaterPath . '_' . $pageSetting['requires'][$value]['name']; |
| 201 | + $requiredValue = $pageSetting['requires'][$value]['value']; |
| 202 | + $this->moduleDataSetup->getConnection()->insertOnDuplicate( |
| 203 | + $this->moduleDataSetup->getTable('core_config_data'), |
| 204 | + [ |
| 205 | + 'scope' => 'default', |
| 206 | + 'scope_id' => 0, |
| 207 | + 'path' => $requiredPath, |
| 208 | + 'value' => $requiredValue |
| 209 | + ] |
| 210 | + ); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * @inheritdoc |
| 218 | + */ |
| 219 | + public static function getDependencies() |
| 220 | + { |
| 221 | + return []; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * @inheritdoc |
| 226 | + */ |
| 227 | + public function getAliases() |
| 228 | + { |
| 229 | + return []; |
| 230 | + } |
| 231 | +} |
0 commit comments