SourcesSelection.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\InventoryCatalogAdminUi\ViewModel;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\Framework\View\Element\Block\ArgumentInterface;
  10. use Magento\InventoryApi\Api\Data\SourceInterface;
  11. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  12. use Magento\InventoryApi\Api\SourceRepositoryInterface;
  13. use Magento\InventoryApi\Model\GetSourceCodesBySkusInterface;
  14. use Magento\InventoryCatalogAdminUi\Model\BulkSessionProductsStorage;
  15. class SourcesSelection implements ArgumentInterface
  16. {
  17. /**
  18. * @var SourceRepositoryInterface
  19. */
  20. private $sourceRepository;
  21. /**
  22. * @var BulkSessionProductsStorage
  23. */
  24. private $bulkSessionProductsStorage;
  25. /**
  26. * @var SearchCriteriaBuilder
  27. */
  28. private $searchCriteriaBuilder;
  29. /**
  30. * @var GetSourceCodesBySkusInterface
  31. */
  32. private $getSourceCodesBySkus;
  33. /**
  34. * @param SourceRepositoryInterface $sourceRepository
  35. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  36. * @param GetSourceCodesBySkusInterface $getSourceCodesBySkus
  37. * @param BulkSessionProductsStorage $bulkSessionProductsStorage
  38. * @SuppressWarnings(PHPMD.LongVariables)
  39. */
  40. public function __construct(
  41. SourceRepositoryInterface $sourceRepository,
  42. SearchCriteriaBuilder $searchCriteriaBuilder,
  43. GetSourceCodesBySkusInterface $getSourceCodesBySkus,
  44. BulkSessionProductsStorage $bulkSessionProductsStorage
  45. ) {
  46. $this->sourceRepository = $sourceRepository;
  47. $this->bulkSessionProductsStorage = $bulkSessionProductsStorage;
  48. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  49. $this->getSourceCodesBySkus = $getSourceCodesBySkus;
  50. }
  51. /**
  52. * Get a list of available sources
  53. * @return SourceInterface[]
  54. */
  55. public function getSources(): array
  56. {
  57. return $this->sourceRepository->getList()->getItems();
  58. }
  59. /**
  60. * Get a list of sources assigned to the products selection
  61. * @return SourceInterface[]
  62. */
  63. public function getAssignedSources(): array
  64. {
  65. $skus = $this->bulkSessionProductsStorage->getProductsSkus();
  66. $sourceCodes = $this->getSourceCodesBySkus->execute($skus);
  67. $searchCriteria = $this->searchCriteriaBuilder
  68. ->addFilter(
  69. SourceItemInterface::SOURCE_CODE,
  70. $sourceCodes,
  71. 'in'
  72. )
  73. ->create();
  74. return $this->sourceRepository->getList($searchCriteria)->getItems();
  75. }
  76. /**
  77. * @return int
  78. */
  79. public function getProductsCount(): int
  80. {
  81. return count($this->bulkSessionProductsStorage->getProductsSkus());
  82. }
  83. }