Skip to content

Commit 2fb949e

Browse files
committed
magento/adobe-stock-integration#1724: Support batches processing for synchronization queue messages - updated pull request with requested changes
1 parent 7a166c2 commit 2fb949e

File tree

6 files changed

+186
-4
lines changed

6 files changed

+186
-4
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentSynchronization\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\FlagManager;
12+
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
13+
use Magento\MediaContentSynchronizationApi\Api\SynchronizeIdentitiesInterface;
14+
use Magento\MediaContentSynchronizationApi\Model\SynchronizerIdentitiesPool;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* Batch Synchronize content with assets
19+
*/
20+
class SynchronizeIdentities implements SynchronizeIdentitiesInterface
21+
{
22+
private const LAST_EXECUTION_TIME_CODE = 'media_content_last_execution';
23+
24+
/**
25+
* @var DateTimeFactory
26+
*/
27+
private $dateFactory;
28+
29+
/**
30+
* @var FlagManager
31+
*/
32+
private $flagManager;
33+
34+
/**
35+
* @var LoggerInterface
36+
*/
37+
private $log;
38+
39+
/**
40+
* @var SynchronizerIdentitiesPool
41+
*/
42+
private $synchronizerIdentitiesPool;
43+
44+
/**
45+
* @var RemoveObsoleteContentAsset
46+
*/
47+
private $removeObsoleteContent;
48+
49+
/**
50+
* @param RemoveObsoleteContentAsset $removeObsoleteContent
51+
* @param DateTimeFactory $dateFactory
52+
* @param FlagManager $flagManager
53+
* @param LoggerInterface $log
54+
* @param SynchronizerIdentitiesPool $synchronizerIdentitiesPool
55+
*/
56+
public function __construct(
57+
RemoveObsoleteContentAsset $removeObsoleteContent,
58+
DateTimeFactory $dateFactory,
59+
FlagManager $flagManager,
60+
LoggerInterface $log,
61+
SynchronizerIdentitiesPool $synchronizerIdentitiesPool
62+
) {
63+
$this->removeObsoleteContent = $removeObsoleteContent;
64+
$this->dateFactory = $dateFactory;
65+
$this->flagManager = $flagManager;
66+
$this->log = $log;
67+
$this->synchronizerIdentitiesPool = $synchronizerIdentitiesPool;
68+
}
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
public function execute(array $mediaContentIdentities): void
74+
{
75+
$failed = [];
76+
77+
foreach ($this->synchronizerIdentitiesPool->get() as $name => $synchronizers) {
78+
try {
79+
$synchronizers->execute($mediaContentIdentities);
80+
} catch (\Exception $exception) {
81+
$this->log->critical($exception);
82+
$failed[] = $name;
83+
}
84+
}
85+
86+
if (!empty($failed)) {
87+
throw new LocalizedException(
88+
__(
89+
'Failed to execute the following content synchronizers: %synchronizers',
90+
[
91+
'synchronizers' => implode(', ', $failed)
92+
]
93+
)
94+
);
95+
}
96+
97+
$this->setLastExecutionTime();
98+
$this->removeObsoleteContent->execute();
99+
}
100+
101+
/**
102+
* Set last synchronizer execution time
103+
*/
104+
private function setLastExecutionTime(): void
105+
{
106+
$currentTime = $this->dateFactory->create()->gmtDate();
107+
$this->flagManager->saveFlag(self::LAST_EXECUTION_TIME_CODE, $currentTime);
108+
}
109+
}

app/code/Magento/MediaContentSynchronization/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\MediaContentSynchronizationApi\Api\SynchronizeInterface" type="Magento\MediaContentSynchronization\Model\Synchronize"/>
10+
<preference for="Magento\MediaContentSynchronizationApi\Api\SynchronizeIdentitiesInterface" type="Magento\MediaContentSynchronization\Model\SynchronizeIdentities"/>
1011
<type name="Magento\Framework\Console\CommandListInterface">
1112
<arguments>
1213
<argument name="commands" xsi:type="array">
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentSynchronizationApi\Model;
9+
10+
use Magento\MediaContentSynchronizationApi\Api\SynchronizeIdentitiesInterface;
11+
12+
class SynchronizerIdentitiesPool
13+
{
14+
/**
15+
* Content with assets synchronizers
16+
*
17+
* @var SynchronizeIdentitiesInterface[]
18+
*/
19+
private $synchronizers;
20+
21+
/**
22+
* @param SynchronizeIdentitiesInterface[] $synchronizers
23+
*/
24+
public function __construct(
25+
array $synchronizers = []
26+
) {
27+
foreach ($synchronizers as $synchronizer) {
28+
if (!$synchronizer instanceof SynchronizeIdentitiesInterface) {
29+
throw new \InvalidArgumentException(
30+
get_class($synchronizer) . ' must implement ' . SynchronizeIdentitiesInterface::class
31+
);
32+
}
33+
}
34+
35+
$this->synchronizers = $synchronizers;
36+
}
37+
38+
/**
39+
* Get all synchronizers from the pool
40+
*
41+
* @return SynchronizeIdentitiesInterface[]
42+
*/
43+
public function get(): array
44+
{
45+
return $this->synchronizers;
46+
}
47+
}

app/code/Magento/MediaContentSynchronizationCatalog/Model/Synchronizer/SynchronizeIdentitiesCatalog.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
class SynchronizeIdentitiesCatalog implements SynchronizeIdentitiesInterface
1515
{
16+
private const FIELD_CATALOG_PRODUCT = 'catalog_product';
17+
private const FIELD_CATALOG_CATEGORY = 'catalog_category';
18+
1619
/**
1720
* @var UpdateContentAssetLinksInterface
1821
*/
@@ -41,10 +44,14 @@ public function __construct(
4144
public function execute(array $mediaContentIdentities): void
4245
{
4346
foreach ($mediaContentIdentities as $identity) {
44-
$this->updateContentAssetLinks->execute(
45-
$identity,
46-
implode(PHP_EOL, $this->getEntityContents->execute($identity))
47-
);
47+
if ($identity->getEntityType() === self::FIELD_CATALOG_PRODUCT
48+
|| $identity->getEntityType() === self::FIELD_CATALOG_CATEGORY
49+
) {
50+
$this->updateContentAssetLinks->execute(
51+
$identity,
52+
implode(PHP_EOL, $this->getEntityContents->execute($identity))
53+
);
54+
}
4855
}
4956
}
5057
}

app/code/Magento/MediaContentSynchronizationCatalog/etc/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,13 @@
3838
</argument>
3939
</arguments>
4040
</type>
41+
<type name="Magento\MediaContentSynchronizationApi\Model\SynchronizerIdentitiesPool">
42+
<arguments>
43+
<argument name="synchronizers" xsi:type="array">
44+
<item name="media_content_catalog_synchronizeIdentity"
45+
xsi:type="object">Magento\MediaContentSynchronizationCatalog\Model\Synchronizer\SynchronizeIdentitiesCatalog
46+
</item>
47+
</argument>
48+
</arguments>
49+
</type>
4150
</config>

app/code/Magento/MediaContentSynchronizationCms/etc/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
</argument>
1515
</arguments>
1616
</type>
17+
<type name="Magento\MediaContentSynchronizationApi\Model\SynchronizerIdentitiesPool">
18+
<arguments>
19+
<argument name="synchronizers" xsi:type="array">
20+
<item name="media_content_cms_synchronizeIdentity"
21+
xsi:type="object">Magento\MediaContentSynchronizationCms\Model\Synchronizer\SynchronizeIdentitiesCms
22+
</item>
23+
</argument>
24+
</arguments>
25+
</type>
1726
<type name="Magento\MediaContentSynchronizationApi\Model\GetEntitiesInterface">
1827
<arguments>
1928
<argument name="entities" xsi:type="array">

0 commit comments

Comments
 (0)