Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/Ecombricks/Inventory/Model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/Ecombricks/Inventory/Model/SourceManagement.php
<?php
/**
 * Copyright © eComBricks. All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace Ecombricks\Inventory\Model;

/**
 * Source management
 */
class SourceManagement
{
    
    /**
     * Source repository
     * 
     * @var \Magento\InventoryApi\Api\SourceRepositoryInterface
     */
    protected $sourceRepository;
    
    /**
     * Store manager
     * 
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;
    
    /**
     * Stock resolver
     * 
     * @var \Magento\InventorySalesApi\Api\StockResolverInterface
     */
    protected $stockResolver;
    
    /**
     * Default source provider
     * 
     * @var \Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface
     */
    protected $defaultSourceProvider;
    
    /**
     * Search criteria builder
     * 
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;
    
    /**
     * Sort order builder
     * 
     * @var \Magento\Framework\Api\SortOrderBuilder
     */
    protected $sortOrderBuilder;
    
    /**
     * Get stock source links
     * 
     * @var \Magento\InventoryApi\Api\GetStockSourceLinksInterface
     */
    protected $getStockSourceLinks;
    
    /**
     * Sources
     * 
     * @var \Magento\InventoryApi\Api\Data\SourceInterface[]
     */
    protected $sources = [];
    
    /**
     * Source stock IDs
     * 
     * @var array
     */
    protected $sourceStockIds = [];
    
    /**
     * Stock sources
     * 
     * @var \Magento\InventoryApi\Api\Data\SourceInterface[][]
     */
    protected $stockSources = [];
    
    /**
     * Constructor
     * 
     * @param \Magento\InventoryApi\Api\SourceRepositoryInterface $sourceRepository
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\InventorySalesApi\Api\StockResolverInterface $stockResolver
     * @param \Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface $defaultSourceProvider
     * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
     * @param \Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder
     * @param \Magento\InventoryApi\Api\GetStockSourceLinksInterface $getStockSourceLinks
     * @return void
     */
    public function __construct(
        \Magento\InventoryApi\Api\SourceRepositoryInterface $sourceRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\InventorySalesApi\Api\StockResolverInterface $stockResolver,
        \Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface $defaultSourceProvider,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder,
        \Magento\InventoryApi\Api\GetStockSourceLinksInterface $getStockSourceLinks
    )
    {
        $this->sourceRepository = $sourceRepository;
        $this->storeManager = $storeManager;
        $this->stockResolver = $stockResolver;
        $this->defaultSourceProvider = $defaultSourceProvider;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->sortOrderBuilder = $sortOrderBuilder;
        $this->getStockSourceLinks = $getStockSourceLinks;
    }
    
    /**
     * Filter not loaded source codes
     * 
     * @param array $sourceCodes
     * @return array
     */
    protected function filterNotLoadedSourceCodes($sourceCodes)
    {
        $notLoadedSourceCodes = [];
        foreach ($sourceCodes as $sourceCode) {
            if (array_key_exists($sourceCode, $this->sources)) {
                continue;
            }
            $notLoadedSourceCodes[] = $sourceCode;
        }
        return $notLoadedSourceCodes;
    }
    
    /**
     * Load sources
     * 
     * @param array|null $sourceCodes
     * @return $this
     */
    public function loadSources($sourceCodes = null)
    {
        if ($sourceCodes !== null) {
            $notLoadedSourceCodes = $this->filterNotLoadedSourceCodes($sourceCodes);
            if (empty($notLoadedSourceCodes)) {
                return $this;
            }
            $sortOrder = $this->sortOrderBuilder
                ->setField(\Magento\InventoryApi\Api\Data\SourceInterface::NAME)
                ->setAscendingDirection()
                ->create();
            $searchCriteria = $this->searchCriteriaBuilder
                ->addFilter(\Magento\InventoryApi\Api\Data\SourceInterface::SOURCE_CODE, $notLoadedSourceCodes, 'in')
                ->addSortOrder($sortOrder)
                ->create();
        } else {
            $searchCriteria = null;
        }
        $sources = $this->sourceRepository->getList($searchCriteria)->getItems();
        foreach ($sources as $source) {
            $this->sources[$source->getSourceCode()] = $source;
        }
        return $this;
    }
    
    /**
     * Get all sources
     * 
     * @return \Magento\InventoryApi\Api\Data\SourceInterface[]
     */
    public function getAllSources()
    {
        $this->loadSources();
        return $this->sources;
    }
    
    /**
     * Get sources
     * 
     * @param array $sourceCodes
     * @return \Magento\InventoryApi\Api\Data\SourceInterface[]
     */
    public function getSources($sourceCodes)
    {
        if (empty($sourceCodes)) {
            return [];
        }
        $this->loadSources($sourceCodes);
        $sources = [];
        foreach ($sourceCodes as $sourceCode) {
            if (empty($this->sources[$sourceCode])) {
                $sources = [];
                break;
            }
            $this->sources[$sourceCode] = $this->sources[$sourceCode];
        }
        return $sources;
    }
    
    /**
     * Get source
     * 
     * @param string $sourceCode
     * @return \Magento\InventoryApi\Api\Data\SourceInterface
     */
    public function getSource($sourceCode)
    {
        if (empty($sourceCode)) {
            return null;
        }
        if (!empty($this->sources[$sourceCode])) {
            return $this->sources[$sourceCode];
        }
        $this->loadSources([$sourceCode]);
        return (!empty($this->sources[$sourceCode])) ? $this->sources[$sourceCode] : null;
    }
    
    /**
     * Get source name
     * 
     * @param string $sourceCode
     * @return string
     */
    public function getSourceName($sourceCode)
    {
        $source = $this->getSource($sourceCode);
        return $source ? $source->getName() : '';
    }
    
    /**
     * Validate source code
     * 
     * @param string $sourceCode
     * @return bool
     */
    public function validateSourceCode($sourceCode)
    {
        $source = $this->getSource($sourceCode);
        return $source !== null;
    }
    
    /**
     * Get default source code
     * 
     * @return string
     */
    public function getDefaultSourceCode()
    {
        return $this->defaultSourceProvider->getCode();
    }
    
    /**
     * Get source stock ID
     * 
     * @param string $sourceCode
     * @return int
     */
    public function getSourceStockId($sourceCode)
    {
        if (array_key_exists($sourceCode, $this->sourceStockIds)) {
            return $this->sourceStockIds[$sourceCode];
        }
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter(\Magento\InventoryApi\Api\Data\StockSourceLinkInterface::SOURCE_CODE, $sourceCode)
            ->create();
        $stockSourceLinks = $this->getStockSourceLinks->execute($searchCriteria)->getItems();
        $stockId = null;
        if (!empty($stockSourceLinks)) {
            $stockSourceLink = current($stockSourceLinks);
            $stockId = $stockSourceLink->getStockId();
        } else {
            $stockId = null;
        }
        $this->sourceStockIds[$sourceCode] = $stockId;
        return $this->sourceStockIds[$sourceCode];
    }
    
    /**
     * Get current stock source links
     * 
     * @param int $stockId
     * @return \Magento\InventoryApi\Api\Data\StockSourceLinkInterface[]
     */
    protected function getStockSourceLinks($stockId)
    {
        $sortOrder = $this->sortOrderBuilder
            ->setField(\Magento\InventoryApi\Api\Data\StockSourceLinkInterface::PRIORITY)
            ->setAscendingDirection()
            ->create();
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter(\Magento\InventoryApi\Api\Data\StockSourceLinkInterface::STOCK_ID, $stockId)
            ->addSortOrder($sortOrder)
            ->create();
        return $this->getStockSourceLinks->execute($searchCriteria)->getItems();
    }
    
    /**
     * Get stock source link source codes
     * 
     * @param int $stockId
     * @return array
     */
    protected function getStockSourceLinkSourceCodes($stockId)
    {
        $sourceCodes = [];
        foreach ($this->getStockSourceLinks($stockId) as $stockSourceLink) {
            $sourceCodes[] = $stockSourceLink->getSourceCode();
        }
        return $sourceCodes;
    }
    
    /**
     * Get stock sources
     * 
     * @param int $stockId
     * @return \Magento\InventoryApi\Api\Data\SourceInterface[]
     */
    public function getStockSources($stockId)
    {
        if (array_key_exists($stockId, $this->stockSources)) {
            return $this->stockSources[$stockId];
        }
        $this->stockSources[$stockId] = [];
        $sourceCodes = $this->getStockSourceLinkSourceCodes($stockId);
        $this->loadSources($sourceCodes);
        foreach ($sourceCodes as $sourceCode) {
            $source = $this->getSource($sourceCode);
            if (empty($source) || !$source->isEnabled()) {
                continue;
            }
            $this->stockSources[$stockId][$sourceCode] = $source;
        }
        return $this->stockSources[$stockId];
    }
    
    /**
     * Get stock source codes
     * 
     * @param int $stockId
     * @return array
     */
    public function getStockSourceCodes($stockId)
    {
        return array_keys($this->getStockSources($stockId));
    }
    
    /**
     * Get current stock
     * 
     * @param \Magento\Store\Api\Data\StoreInterface|null $store
     * @return \Magento\InventoryApi\Api\Data\StockInterface
     */
    public function getCurrentStock($store = null)
    {
        if ($store === null) {
            $store = $this->storeManager->getStore();
        }
        return $this->stockResolver->execute(
            \Magento\InventorySalesApi\Api\Data\SalesChannelInterface::TYPE_WEBSITE,
            $store->getWebsite()->getCode()
        );
    }
    
    /**
     * Get current sources
     * 
     * @param \Magento\Store\Api\Data\StoreInterface|null $store
     * @return \Magento\InventoryApi\Api\Data\SourceInterface[]
     */
    public function getCurrentSources($store = null)
    {
        return $this->getStockSources($this->getCurrentStock($store)->getStockId());
    }
    
    /**
     * Get current source codes
     * 
     * @param \Magento\Store\Api\Data\StoreInterface|null $store
     * @return array
     */
    public function getCurrentSourceCodes($store = null)
    {
        return array_keys($this->getCurrentSources($store));
    }
    
    /**
     * Clear cache
     * 
     * @return $this
     */
    public function clearCache()
    {
        if (!empty($this->sources)) {
            $this->sources = [];
        }
        if (!empty($this->sourceStockIds)) {
            $this->sourceStockIds = [];
        }
        if (!empty($this->stockSources)) {
            $this->stockSources = [];
        }
        return $this;
    }
    
}

Spamworldpro Mini