forked from oroinc/OroAkeneoBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImportProductProcessor.php
134 lines (107 loc) · 4.46 KB
/
ImportProductProcessor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Oro\Bundle\AkeneoBundle\Async;
use Doctrine\ORM\EntityManagerInterface;
use Oro\Bundle\AkeneoBundle\Tools\CacheProviderTrait;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\IntegrationBundle\Authentication\Token\IntegrationTokenAwareTrait;
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
use Oro\Bundle\IntegrationBundle\Entity\FieldsChanges;
use Oro\Bundle\IntegrationBundle\Provider\SyncProcessorRegistry;
use Oro\Bundle\MessageQueueBundle\Entity\Job;
use Oro\Component\MessageQueue\Client\TopicSubscriberInterface;
use Oro\Component\MessageQueue\Consumption\MessageProcessorInterface;
use Oro\Component\MessageQueue\Job\JobRunner;
use Oro\Component\MessageQueue\Transport\MessageInterface;
use Oro\Component\MessageQueue\Transport\SessionInterface;
use Oro\Component\MessageQueue\Util\JSON;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ImportProductProcessor implements MessageProcessorInterface, TopicSubscriberInterface
{
use CacheProviderTrait;
use IntegrationTokenAwareTrait;
/** @var DoctrineHelper */
private $doctrineHelper;
/** @var JobRunner */
private $jobRunner;
/** @var LoggerInterface */
private $logger;
/** @var SyncProcessorRegistry */
private $syncProcessorRegistry;
public function __construct(
DoctrineHelper $doctrineHelper,
JobRunner $jobRunner,
TokenStorageInterface $tokenStorage,
LoggerInterface $logger,
SyncProcessorRegistry $syncProcessorRegistry
) {
$this->doctrineHelper = $doctrineHelper;
$this->jobRunner = $jobRunner;
$this->tokenStorage = $tokenStorage;
$this->logger = $logger;
$this->syncProcessorRegistry = $syncProcessorRegistry;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedTopics()
{
return [Topics::IMPORT_PRODUCTS];
}
/**
* {@inheritdoc}
*/
public function process(MessageInterface $message, SessionInterface $session)
{
$body = JSON::decode($message->getBody());
$body = array_replace_recursive(['integrationId' => null, 'jobId' => null], $body);
if (!$body['integrationId']) {
$this->logger->critical('The message invalid. It must have integrationId set');
return self::REJECT;
}
/** @var EntityManagerInterface $em */
$em = $this->doctrineHelper->getEntityManagerForClass(Integration::class);
/** @var Integration $integration */
$integration = $em->find(Integration::class, $body['integrationId']);
if (!$integration) {
$this->logger->error(
sprintf('The integration not found: %s', $body['integrationId'])
);
return self::REJECT;
}
if (!$integration->isEnabled()) {
$this->logger->error(
sprintf('The integration is not enabled: %s', $body['integrationId'])
);
return self::REJECT;
}
$this->setTemporaryIntegrationToken($integration);
$result = $this->jobRunner->runDelayed(
$body['jobId'],
function (JobRunner $jobRunner, Job $child) use ($integration, $body) {
$this->doctrineHelper->refreshIncludingUnitializedRelations($integration);
$processor = $this->syncProcessorRegistry->getProcessorForIntegration($integration);
$em = $this->doctrineHelper->getEntityManager(FieldsChanges::class);
/** @var FieldsChanges $fieldsChanges */
$fieldsChanges = $em
->getRepository(FieldsChanges::class)
->findOneBy(['entityId' => $child->getId(), 'entityClass' => Job::class]);
if (!$fieldsChanges) {
$this->logger->error(
sprintf('Source data from Akeneo not found for job: %s', $child->getId())
);
return false;
}
$this->cacheProvider->save('akeneo', $fieldsChanges->getChangedFields());
$status = $processor->process(
$integration,
$body['connector'] ?? null,
$body['connector_parameters'] ?? []
);
$em->clear(FieldsChanges::class);
return $status;
}
);
return $result ? self::ACK : self::REJECT;
}
}