UpdateStoreGroupCodes.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Setup\Patch\Data;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\Setup\Patch\DataPatchInterface;
  9. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  10. /**
  11. * Class UpdateStoreGroupCodes
  12. * @package Magento\Store\Setup\Patch
  13. */
  14. class UpdateStoreGroupCodes implements DataPatchInterface, PatchVersionInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  18. */
  19. private $moduleDataSetup;
  20. /**
  21. * UpdateStoreGroupCodes constructor.
  22. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  23. */
  24. public function __construct(
  25. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  26. ) {
  27. $this->moduleDataSetup = $moduleDataSetup;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function apply()
  33. {
  34. $this->updateStoreGroupCodes();
  35. }
  36. /**
  37. * Upgrade codes for store groups
  38. */
  39. private function updateStoreGroupCodes()
  40. {
  41. $connection = $this->moduleDataSetup->getConnection();
  42. $storeGroupTable = $this->moduleDataSetup->getTable('store_group');
  43. $select = $connection->select()->from(
  44. $storeGroupTable,
  45. ['group_id', 'name']
  46. );
  47. $groupList = $connection->fetchPairs($select);
  48. $codes = [];
  49. foreach ($groupList as $groupId => $groupName) {
  50. $code = preg_replace('/\s+/', '_', $groupName);
  51. $code = preg_replace('/[^a-z0-9-_]/', '', strtolower($code));
  52. $code = preg_replace('/^[^a-z]+/', '', $code);
  53. if (empty($code)) {
  54. $code = 'store_group';
  55. }
  56. if (array_key_exists($code, $codes)) {
  57. $codes[$code]++;
  58. $code = $code . $codes[$code];
  59. }
  60. $codes[$code] = 1;
  61. $connection->update(
  62. $storeGroupTable,
  63. ['code' => $code],
  64. ['group_id = ?' => $groupId]
  65. );
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public static function getDependencies()
  72. {
  73. return [];
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public static function getVersion()
  79. {
  80. return '2.1.0';
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getAliases()
  86. {
  87. return [];
  88. }
  89. }