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