GetUnassignedSalesChannelsForStock.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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;
  8. use Magento\InventoryApi\Api\Data\StockInterface;
  9. use Magento\InventorySalesApi\Model\GetAssignedSalesChannelsForStockInterface;
  10. /**
  11. * Service return sales channels witch assigned to stock in records in DB,
  12. * but stock itself might not to has them
  13. */
  14. class GetUnassignedSalesChannelsForStock
  15. {
  16. /**
  17. * @var GetAssignedSalesChannelsForStockInterface
  18. */
  19. private $getAssignedSalesChannelsForStock;
  20. /**
  21. * @param GetAssignedSalesChannelsForStockInterface $getAssignedSalesChannelsForStock
  22. */
  23. public function __construct(
  24. GetAssignedSalesChannelsForStockInterface $getAssignedSalesChannelsForStock
  25. ) {
  26. $this->getAssignedSalesChannelsForStock = $getAssignedSalesChannelsForStock;
  27. }
  28. /**
  29. * Return all sales channels witch will be unassigned from the saved stock
  30. *
  31. * @param StockInterface $stock
  32. * @return \Magento\InventorySales\Model\SalesChannel[]
  33. */
  34. public function execute(StockInterface $stock): array
  35. {
  36. $newWebsiteCodes = $result = [];
  37. $assignedSalesChannels = $this->getAssignedSalesChannelsForStock->execute((int)$stock->getStockId());
  38. $extensionAttributes = $stock->getExtensionAttributes();
  39. $newSalesChannels = $extensionAttributes->getSalesChannels() ?: [];
  40. foreach ($newSalesChannels as $salesChannel) {
  41. if ($salesChannel->getType() === SalesChannel::TYPE_WEBSITE) {
  42. $newWebsiteCodes[] = $salesChannel->getCode();
  43. }
  44. }
  45. foreach ($assignedSalesChannels as $salesChannel) {
  46. if ($salesChannel->getType() === SalesChannel::TYPE_WEBSITE
  47. && !in_array($salesChannel->getCode(), $newWebsiteCodes, true)
  48. ) {
  49. $result[] = $salesChannel;
  50. }
  51. }
  52. return $result;
  53. }
  54. }