SourcesSelection.php 2.77 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\InventoryCatalogAdminUi\ViewModel;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryApi\Model\GetSourceCodesBySkusInterface;
use Magento\InventoryCatalogAdminUi\Model\BulkSessionProductsStorage;

class SourcesSelection implements ArgumentInterface
{
    /**
     * @var SourceRepositoryInterface
     */
    private $sourceRepository;

    /**
     * @var BulkSessionProductsStorage
     */
    private $bulkSessionProductsStorage;

    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var GetSourceCodesBySkusInterface
     */
    private $getSourceCodesBySkus;

    /**
     * @param SourceRepositoryInterface $sourceRepository
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     * @param GetSourceCodesBySkusInterface $getSourceCodesBySkus
     * @param BulkSessionProductsStorage $bulkSessionProductsStorage
     * @SuppressWarnings(PHPMD.LongVariables)
     */
    public function __construct(
        SourceRepositoryInterface $sourceRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        GetSourceCodesBySkusInterface $getSourceCodesBySkus,
        BulkSessionProductsStorage $bulkSessionProductsStorage
    ) {
        $this->sourceRepository = $sourceRepository;
        $this->bulkSessionProductsStorage = $bulkSessionProductsStorage;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->getSourceCodesBySkus = $getSourceCodesBySkus;
    }

    /**
     * Get a list of available sources
     * @return SourceInterface[]
     */
    public function getSources(): array
    {
        return $this->sourceRepository->getList()->getItems();
    }

    /**
     * Get a list of sources assigned to the products selection
     * @return SourceInterface[]
     */
    public function getAssignedSources(): array
    {
        $skus = $this->bulkSessionProductsStorage->getProductsSkus();
        $sourceCodes = $this->getSourceCodesBySkus->execute($skus);

        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter(
                SourceItemInterface::SOURCE_CODE,
                $sourceCodes,
                'in'
            )
            ->create();

        return $this->sourceRepository->getList($searchCriteria)->getItems();
    }

    /**
     * @return int
     */
    public function getProductsCount(): int
    {
        return count($this->bulkSessionProductsStorage->getProductsSkus());
    }
}