|
| 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 | +} |
0 commit comments