vendor/shopware/core/System/Snippet/Subscriber/CustomFieldSubscriber.php line 76

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Snippet\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12. * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  13. */
  14. #[Package('system-settings')]
  15. class CustomFieldSubscriber implements EventSubscriberInterface
  16. {
  17. private const CUSTOM_FIELD_ID_FIELD = 'custom_field_id';
  18. private Connection $connection;
  19. /**
  20. * @internal
  21. */
  22. public function __construct(Connection $connection)
  23. {
  24. $this->connection = $connection;
  25. }
  26. public static function getSubscribedEvents(): array
  27. {
  28. return [
  29. 'custom_field.written' => 'customFieldIsWritten',
  30. 'custom_field.deleted' => 'customFieldIsDeleted',
  31. ];
  32. }
  33. public function customFieldIsWritten(EntityWrittenEvent $event): void
  34. {
  35. $snippets = [];
  36. $snippetSets = null;
  37. foreach ($event->getWriteResults() as $writeResult) {
  38. if (!isset($writeResult->getPayload()['config']['label']) || empty($writeResult->getPayload()['config']['label'])) {
  39. continue;
  40. }
  41. if ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT) {
  42. if ($snippetSets === null) {
  43. $snippetSets = $this->connection->fetchAllAssociative('SELECT id, iso FROM snippet_set');
  44. }
  45. if (empty($snippetSets)) {
  46. return;
  47. }
  48. $this->setInsertSnippets($writeResult, $snippetSets, $snippets);
  49. }
  50. }
  51. if (empty($snippets)) {
  52. return;
  53. }
  54. foreach ($snippets as $snippet) {
  55. $this->connection->executeStatement(
  56. 'INSERT INTO snippet (`id`, `snippet_set_id`, `translation_key`, `value`, `author`, `custom_fields`, `created_at`)
  57. VALUES (:id, :setId, :translationKey, :value, :author, :customFields, :createdAt)
  58. ON DUPLICATE KEY UPDATE `value` = :value',
  59. $snippet
  60. );
  61. }
  62. }
  63. public function customFieldIsDeleted(EntityDeletedEvent $event): void
  64. {
  65. $this->connection->executeStatement(
  66. 'DELETE FROM `snippet`
  67. WHERE JSON_EXTRACT(`custom_fields`, "$.custom_field_id") IN (:customFieldIds)',
  68. ['customFieldIds' => $event->getIds()],
  69. ['customFieldIds' => Connection::PARAM_STR_ARRAY]
  70. );
  71. }
  72. /**
  73. * @param list<array<string, string>> $snippetSets
  74. * @param array<string, mixed> $snippets
  75. */
  76. private function setInsertSnippets(EntityWriteResult $writeResult, array $snippetSets, array &$snippets): void
  77. {
  78. $name = $writeResult->getPayload()['name'];
  79. $labels = $writeResult->getPayload()['config']['label'];
  80. foreach ($snippetSets as $snippetSet) {
  81. $label = $name;
  82. $iso = $snippetSet['iso'];
  83. if (isset($labels[$iso])) {
  84. $label = $labels[$iso];
  85. }
  86. $snippets[] = [
  87. 'id' => Uuid::randomBytes(),
  88. 'setId' => $snippetSet['id'],
  89. 'translationKey' => 'customFields.' . $name,
  90. 'value' => $label,
  91. 'author' => 'System',
  92. 'customFields' => json_encode([
  93. self::CUSTOM_FIELD_ID_FIELD => $writeResult->getPrimaryKey(),
  94. ]),
  95. 'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  96. ];
  97. }
  98. }
  99. }