Skip to content

Commit b6ac3a3

Browse files
committed
MDEE-40:[Commerce Export] Implement stock_item_status feed
- use sku for changelog table
1 parent c1fba8b commit b6ac3a3

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\InventoryDataExporter\Plugin\Mview;
10+
11+
use Magento\Framework\DB\Adapter\ConnectionException;
12+
use Magento\Framework\Mview\View\Changelog;
13+
use Magento\Framework\Mview\View\ChangelogTableNotExistsException;
14+
use Magento\Framework\Phrase;
15+
16+
/**
17+
* Override default behavior for Mview:
18+
* - allow to collect String instead Integer for "inventory_data_exporter_stock_status" changelog table
19+
*/
20+
class StockStatusChangelog
21+
{
22+
/**
23+
* @var \Magento\Framework\App\ResourceConnection
24+
*/
25+
private $resource;
26+
27+
private const SKU_FIELD_SIZE = 64;
28+
private const STOCK_STATUS_CHANGELOG_NAME = 'inventory_data_exporter_stock_status_' . Changelog::NAME_SUFFIX;
29+
30+
/**
31+
* @param \Magento\Framework\App\ResourceConnection $resource
32+
* @throws ConnectionException
33+
*/
34+
public function __construct(\Magento\Framework\App\ResourceConnection $resource)
35+
{
36+
$this->connection = $resource->getConnection();
37+
$this->resource = $resource;
38+
}
39+
40+
public function aroundCreate(
41+
\Magento\Framework\Mview\View\Changelog $subject,
42+
callable $proceed
43+
): void {
44+
if ($this->isStockStatusChangelog($subject)) {
45+
$this->createChangelogTable($subject);
46+
} else {
47+
$proceed();
48+
}
49+
}
50+
51+
/**
52+
* @param Changelog $subject
53+
*/
54+
private function createChangelogTable(Changelog $subject): void
55+
{
56+
$changelogTableName = $this->resource->getTableName($subject->getName());
57+
if (!$this->connection->isTableExists($changelogTableName)) {
58+
$table = $this->connection->newTable(
59+
$changelogTableName
60+
)->addColumn(
61+
'version_id',
62+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
63+
null,
64+
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
65+
'Version ID'
66+
)->addColumn(
67+
$subject->getColumnName(),
68+
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
69+
self::SKU_FIELD_SIZE,
70+
['nullable' => false],
71+
'Entity SKU'
72+
);
73+
$this->connection->createTable($table);
74+
}
75+
}
76+
77+
/**
78+
* @param Changelog $changelog
79+
* @return bool
80+
*/
81+
private function isStockStatusChangelog(Changelog $changelog): bool
82+
{
83+
return $changelog->getName() === self::STOCK_STATUS_CHANGELOG_NAME;
84+
}
85+
86+
/**
87+
* Override original method: return list of SKUs instead of
88+
* Retrieve entity ids by range [$fromVersionId..$toVersionId]
89+
*
90+
* @param Changelog $subject
91+
* @param callable $proceed
92+
* @param int $fromVersionId
93+
* @param int $toVersionId
94+
* @return string[]
95+
* @throws ChangelogTableNotExistsException
96+
*/
97+
public function aroundGetList(
98+
\Magento\Framework\Mview\View\Changelog $subject,
99+
callable $proceed,
100+
$fromVersionId,
101+
$toVersionId
102+
) {
103+
if (!$this->isStockStatusChangelog($subject)) {
104+
return $proceed($fromVersionId, $toVersionId);
105+
}
106+
$changelogTableName = $this->resource->getTableName($subject->getName());
107+
if (!$this->connection->isTableExists($changelogTableName)) {
108+
throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
109+
}
110+
111+
$select = $this->connection->select()->distinct(
112+
true
113+
)->from(
114+
$changelogTableName,
115+
[$subject->getColumnName()]
116+
)->where(
117+
'version_id > ?',
118+
(int)$fromVersionId
119+
)->where(
120+
'version_id <= ?',
121+
(int)$toVersionId
122+
);
123+
124+
return $this->connection->fetchCol($select);
125+
}
126+
}

Diff for: InventoryDataExporter/etc/di.xml

+5
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@
6565
<argument name="feedIndexMetadata" xsi:type="object">Magento\InventoryDataExporter\Model\Indexer\StockStatusFeedIndexMetadata</argument>
6666
</arguments>
6767
</virtualType>
68+
69+
<type name="\Magento\Framework\Mview\View\Changelog">
70+
<plugin name="create_stock_item_changelog_table" type="\Magento\InventoryDataExporter\Plugin\Mview\StockStatusChangelog"/>
71+
</type>
72+
6873
</config>

Diff for: InventoryDataExporter/etc/mview.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
99
<view id="inventory_data_exporter_stock_status" class="Magento\InventoryDataExporter\Model\Indexer\StockStatusFeedIndexer" group="indexer">
1010
<subscriptions>
11-
<table name="inventory_source_item" entity_column="source_item_id" />
11+
<table name="inventory_source_item" entity_column="sku" />
1212
</subscriptions>
1313
</view>
1414
</config>

0 commit comments

Comments
 (0)