IndexStructure.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryIndexer\Indexer;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Ddl\Table;
  10. use Magento\Framework\Exception\StateException;
  11. use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexName;
  12. use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexNameResolverInterface;
  13. use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexStructureInterface;
  14. /**
  15. * @inheritdoc
  16. */
  17. class IndexStructure implements IndexStructureInterface
  18. {
  19. /**
  20. * Constants for represent fields in index table
  21. */
  22. const SKU = 'sku';
  23. const QUANTITY = 'quantity';
  24. const IS_SALABLE = 'is_salable';
  25. /**#@-*/
  26. /**
  27. * @var ResourceConnection
  28. */
  29. private $resourceConnection;
  30. /**
  31. * @var IndexNameResolverInterface
  32. */
  33. private $indexNameResolver;
  34. /**
  35. * @param ResourceConnection $resourceConnection
  36. * @param IndexNameResolverInterface $indexNameResolver
  37. */
  38. public function __construct(
  39. ResourceConnection $resourceConnection,
  40. IndexNameResolverInterface $indexNameResolver
  41. ) {
  42. $this->resourceConnection = $resourceConnection;
  43. $this->indexNameResolver = $indexNameResolver;
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function isExist(IndexName $indexName, string $connectionName): bool
  49. {
  50. $connection = $this->resourceConnection->getConnection($connectionName);
  51. $tableName = $this->indexNameResolver->resolveName($indexName);
  52. return $connection->isTableExists($this->resourceConnection->getTableName($tableName));
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function create(IndexName $indexName, string $connectionName): void
  58. {
  59. $connection = $this->resourceConnection->getConnection($connectionName);
  60. $tableName = $this->indexNameResolver->resolveName($indexName);
  61. if ($connection->isTableExists($tableName)) {
  62. throw new StateException(__('Table %table already exits', ['table' => $tableName]));
  63. }
  64. $this->createTable($connection, $tableName);
  65. }
  66. /**
  67. * Create the index table
  68. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  69. * @param string $tableName
  70. * @return void
  71. */
  72. private function createTable(\Magento\Framework\DB\Adapter\AdapterInterface $connection, string $tableName)
  73. {
  74. $table = $connection->newTable(
  75. $this->resourceConnection->getTableName($tableName)
  76. )->setComment(
  77. 'Inventory Stock item Table'
  78. )->addColumn(
  79. self::SKU,
  80. Table::TYPE_TEXT,
  81. 64,
  82. [
  83. Table::OPTION_PRIMARY => true,
  84. Table::OPTION_NULLABLE => false,
  85. ],
  86. 'Sku'
  87. )->addColumn(
  88. self::QUANTITY,
  89. Table::TYPE_DECIMAL,
  90. null,
  91. [
  92. Table::OPTION_UNSIGNED => false,
  93. Table::OPTION_NULLABLE => false,
  94. Table::OPTION_DEFAULT => 0,
  95. Table::OPTION_PRECISION => 10,
  96. Table::OPTION_SCALE => 4,
  97. ],
  98. 'Quantity'
  99. )->addColumn(
  100. self::IS_SALABLE,
  101. Table::TYPE_BOOLEAN,
  102. null,
  103. [
  104. Table::OPTION_NULLABLE => false,
  105. ],
  106. 'Is Salable'
  107. );
  108. $connection->createTable($table);
  109. }
  110. /**
  111. * @inheritdoc
  112. */
  113. public function delete(IndexName $indexName, string $connectionName): void
  114. {
  115. $connection = $this->resourceConnection->getConnection($connectionName);
  116. $tableName = $this->indexNameResolver->resolveName($indexName);
  117. $connection->dropTable($this->resourceConnection->getTableName($tableName));
  118. }
  119. }