-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathStockStatusChangelog.php
176 lines (163 loc) · 5.44 KB
/
StockStatusChangelog.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*/
declare(strict_types=1);
namespace Magento\InventoryDataExporter\Plugin\Mview;
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Mview\View\Changelog;
use Magento\Framework\Mview\View\ChangelogTableNotExistsException;
use Magento\Framework\Phrase;
/**
* Override default behavior for Mview:
* - allow to collect String instead Integer for "inventory_data_exporter_stock_status_feed_cl" changelog table
*/
class StockStatusChangelog
{
private const SKU_FIELD_SIZE = 64;
private const STOCK_STATUS_CHANGELOG_NAME = 'inventory_data_exporter_stock_status_feed_' . Changelog::NAME_SUFFIX;
private ResourceConnection $resource;
private AdapterInterface $connection;
private CommerceDataExportLoggerInterface $logger;
/**
* Constructor for class StockStatusChangelog
*
* @param ResourceConnection $resource
* @param CommerceDataExportLoggerInterface $logger
*/
public function __construct(
ResourceConnection $resource,
CommerceDataExportLoggerInterface $logger
) {
$this->connection = $resource->getConnection();
$this->resource = $resource;
$this->logger = $logger;
}
/**
* Around plugin for create method
*
* @param Changelog $subject
* @param callable $proceed
* @return void
* @throws \Zend_Db_Exception
*/
public function aroundCreate(
Changelog $subject,
callable $proceed
): void {
try {
if ($this->isStockStatusChangelog($subject)) {
$this->createChangelogTable($subject);
} else {
$proceed();
}
} catch (\Throwable $e) {
$this->logger->error(
'Data Exporter exception has occurred: ' . $e->getMessage(),
['exception' => $e]
);
$proceed();
}
}
/**
* Create changelog table
*
* @param Changelog $subject
* @throws \Zend_Db_Exception
*/
private function createChangelogTable(Changelog $subject): void
{
$changelogTableName = $this->resource->getTableName($subject->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
$table = $this->connection->newTable(
$changelogTableName
)->addColumn(
'version_id',
Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Version ID'
)->addColumn(
$subject->getColumnName(),
Table::TYPE_TEXT,
self::SKU_FIELD_SIZE,
['nullable' => false],
'Entity SKU'
);
$this->connection->createTable($table);
}
}
/**
* Is stock status has changelog
*
* @param Changelog $changelog
* @return bool
*/
private function isStockStatusChangelog(Changelog $changelog): bool
{
return $changelog->getName() === self::STOCK_STATUS_CHANGELOG_NAME;
}
/**
* Override original method
*
* Return list of SKUs instead of
* retrieve entity ids by range [$fromVersionId..$toVersionId]
*
* @param Changelog $subject
* @param callable $proceed
* @param int $fromVersionId
* @param int $toVersionId
* @return string[]
* @throws ChangelogTableNotExistsException
*/
public function aroundGetList(
Changelog $subject,
callable $proceed,
$fromVersionId,
$toVersionId
) {
try {
if (!$this->isStockStatusChangelog($subject)) {
return $proceed($fromVersionId, $toVersionId);
}
$changelogTableName = $this->resource->getTableName($subject->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new ChangelogTableNotExistsException(
new Phrase("Table %1 does not exist", [$changelogTableName])
);
}
$select = $this->connection->select()->distinct(
true
)->from(
$changelogTableName,
[$subject->getColumnName()]
)->where(
'version_id > ?',
(int)$fromVersionId
)->where(
'version_id <= ?',
(int)$toVersionId
);
return $this->connection->fetchCol($select);
} catch (\Throwable $e) {
$this->logger->error(
'Data Exporter exception has occurred: ' . $e->getMessage(),
['exception' => $e]
);
return $proceed($fromVersionId, $toVersionId);
}
}
}