IsStockItemSalableConditionChain.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\InventorySales\Model\ResourceModel\IsStockItemSalableCondition;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Select;
  10. use Magento\Framework\Exception\LocalizedException;
  11. /**
  12. * Chain of stock item salable conditions.
  13. */
  14. class IsStockItemSalableConditionChain implements GetIsStockItemSalableConditionInterface
  15. {
  16. /**
  17. * @var GetIsStockItemSalableConditionInterface[]
  18. */
  19. private $conditions = [];
  20. /**
  21. * @var ResourceConnection
  22. */
  23. private $resourceConnection;
  24. /**
  25. * @param ResourceConnection $resourceConnection
  26. * @param array $conditions
  27. *
  28. * @throws LocalizedException
  29. */
  30. public function __construct(
  31. ResourceConnection $resourceConnection,
  32. array $conditions = []
  33. ) {
  34. foreach ($conditions as $getIsSalableCondition) {
  35. if (!$getIsSalableCondition instanceof GetIsStockItemSalableConditionInterface) {
  36. throw new LocalizedException(
  37. __('Condition must implement %1', GetIsStockItemSalableConditionInterface::class)
  38. );
  39. }
  40. }
  41. $this->resourceConnection = $resourceConnection;
  42. $this->conditions = $conditions;
  43. }
  44. /**
  45. * @inheritdoc
  46. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  47. */
  48. public function execute(Select $select): string
  49. {
  50. if (empty($this->conditions)) {
  51. return '1';
  52. }
  53. $conditionStrings = [];
  54. foreach ($this->conditions as $condition) {
  55. $conditionString = $condition->execute($select);
  56. if ('' !== trim($conditionString)) {
  57. $conditionStrings[] = $conditionString;
  58. }
  59. }
  60. $isSalableString = '(' . implode($conditionStrings, ') OR (') . ')';
  61. return (string)$this->resourceConnection->getConnection()->getCheckSql($isSalableString, 1, 0);
  62. }
  63. }