OperationRepository.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\AsynchronousOperations\Model\ResourceModel\Operation;
  8. use Magento\AsynchronousOperations\Api\Data\OperationInterface;
  9. use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
  10. use Magento\Framework\MessageQueue\MessageValidator;
  11. use Magento\Framework\MessageQueue\MessageEncoder;
  12. use Magento\Framework\Serialize\Serializer\Json;
  13. use Magento\Framework\EntityManager\EntityManager;
  14. /**
  15. * Create operation for list of bulk operations.
  16. */
  17. class OperationRepository
  18. {
  19. /**
  20. * @var \Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory
  21. */
  22. private $operationFactory;
  23. /**
  24. * @var Json
  25. */
  26. private $jsonSerializer;
  27. /**
  28. * @var EntityManager
  29. */
  30. private $entityManager;
  31. /**
  32. * @var MessageEncoder
  33. */
  34. private $messageEncoder;
  35. /**
  36. * @var MessageValidator
  37. */
  38. private $messageValidator;
  39. /**
  40. * @param OperationInterfaceFactory $operationFactory
  41. * @param EntityManager $entityManager
  42. * @param MessageValidator $messageValidator
  43. * @param MessageEncoder $messageEncoder
  44. * @param Json $jsonSerializer
  45. */
  46. public function __construct(
  47. OperationInterfaceFactory $operationFactory,
  48. EntityManager $entityManager,
  49. MessageValidator $messageValidator,
  50. MessageEncoder $messageEncoder,
  51. Json $jsonSerializer
  52. ) {
  53. $this->operationFactory = $operationFactory;
  54. $this->jsonSerializer = $jsonSerializer;
  55. $this->messageEncoder = $messageEncoder;
  56. $this->messageValidator = $messageValidator;
  57. $this->entityManager = $entityManager;
  58. }
  59. /**
  60. * @param $topicName
  61. * @param $entityParams
  62. * @param $groupId
  63. * @return mixed
  64. */
  65. public function createByTopic($topicName, $entityParams, $groupId)
  66. {
  67. $this->messageValidator->validate($topicName, $entityParams);
  68. $encodedMessage = $this->messageEncoder->encode($topicName, $entityParams);
  69. $serializedData = [
  70. 'entity_id' => null,
  71. 'entity_link' => '',
  72. 'meta_information' => $encodedMessage,
  73. ];
  74. $data = [
  75. 'data' => [
  76. OperationInterface::BULK_ID => $groupId,
  77. OperationInterface::TOPIC_NAME => $topicName,
  78. OperationInterface::SERIALIZED_DATA => $this->jsonSerializer->serialize($serializedData),
  79. OperationInterface::STATUS => OperationInterface::STATUS_TYPE_OPEN,
  80. ],
  81. ];
  82. /** @var \Magento\AsynchronousOperations\Api\Data\OperationInterface $operation */
  83. $operation = $this->operationFactory->create($data);
  84. return $this->entityManager->save($operation);
  85. }
  86. }