StockStatusSelectBuilder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model\ResourceModel\Indexer;
  7. use Magento\Framework\DB\Select;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
  10. /**
  11. * Class StockStatusSelectBuilder
  12. * Is used to create Select object that is used for Bundle product stock status indexation
  13. *
  14. * @see \Magento\Bundle\Model\ResourceModel\Indexer\Stock::_getStockStatusSelect
  15. */
  16. class StockStatusSelectBuilder
  17. {
  18. /**
  19. * @param \Magento\Framework\App\ResourceConnection $resourceConnection
  20. * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
  21. * @param \Magento\Eav\Model\Config $eavConfig
  22. */
  23. public function __construct(
  24. \Magento\Framework\App\ResourceConnection $resourceConnection,
  25. \Magento\Framework\EntityManager\MetadataPool $metadataPool,
  26. \Magento\Eav\Model\Config $eavConfig
  27. ) {
  28. $this->resourceConnection = $resourceConnection;
  29. $this->metadataPool = $metadataPool;
  30. $this->eavConfig = $eavConfig;
  31. }
  32. /**
  33. * @param Select $select
  34. * @return Select
  35. * @throws \Exception
  36. */
  37. public function buildSelect(Select $select)
  38. {
  39. $select = clone $select;
  40. $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
  41. $linkField = $metadata->getLinkField();
  42. $select->reset(
  43. Select::COLUMNS
  44. )->columns(
  45. ['e.entity_id', 'cis.website_id', 'cis.stock_id']
  46. )->joinLeft(
  47. ['o' => $this->resourceConnection->getTableName('catalog_product_bundle_stock_index')],
  48. 'o.entity_id = e.entity_id AND o.website_id = cis.website_id AND o.stock_id = cis.stock_id',
  49. []
  50. )->joinInner(
  51. ['cpr' => $this->resourceConnection->getTableName('catalog_product_relation')],
  52. 'e.' . $linkField . ' = cpr.parent_id',
  53. []
  54. )->columns(
  55. ['qty' => new \Zend_Db_Expr('0')]
  56. );
  57. if ($metadata->getIdentifierField() === $metadata->getLinkField()) {
  58. $select->joinInner(
  59. ['cpei' => $this->resourceConnection->getTableName('catalog_product_entity_int')],
  60. 'cpr.child_id = cpei.' . $linkField
  61. . ' AND cpei.attribute_id = ' . $this->getAttribute('status')->getId()
  62. . ' AND cpei.value = ' . ProductStatus::STATUS_ENABLED,
  63. []
  64. );
  65. } else {
  66. $select->joinInner(
  67. ['cpel' => $this->resourceConnection->getTableName('catalog_product_entity')],
  68. 'cpel.entity_id = cpr.child_id',
  69. []
  70. )->joinInner(
  71. ['cpei' => $this->resourceConnection->getTableName('catalog_product_entity_int')],
  72. 'cpel.'. $linkField . ' = cpei.' . $linkField
  73. . ' AND cpei.attribute_id = ' . $this->getAttribute('status')->getId()
  74. . ' AND cpei.value = ' . ProductStatus::STATUS_ENABLED,
  75. []
  76. );
  77. }
  78. return $select;
  79. }
  80. /**
  81. * Retrieve catalog_product attribute instance by attribute code
  82. *
  83. * @param string $attributeCode
  84. * @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute
  85. */
  86. private function getAttribute($attributeCode)
  87. {
  88. return $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
  89. }
  90. }