Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockDataProvider.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\Framework\Api\Search\SearchCriteriaBuilder as SearchSearchCriteriaBuilder;
25 
31 {
35  private $stockRepository;
36 
40  private $searchResultFactory;
41 
45  private $getStockSourceLinks;
46 
50  private $sourceRepository;
51 
55  private $apiSearchCriteriaBuilder;
56 
60  private $sortOrderBuilder;
64  private $getSourcesAssignedToStockOrderedByPriority;
65 
85  public function __construct(
86  $name,
90  SearchSearchCriteriaBuilder $searchCriteriaBuilder,
93  StockRepositoryInterface $stockRepository,
94  SearchResultFactory $searchResultFactory,
95  GetStockSourceLinksInterface $getStockSourceLinks,
96  SourceRepositoryInterface $sourceRepository,
97  SearchCriteriaBuilder $apiSearchCriteriaBuilder,
98  SortOrderBuilder $sortOrderBuilder,
99  GetSourcesAssignedToStockOrderedByPriorityInterface $getSourcesAssignedToStockOrderedByPriority,
100  array $meta = [],
101  array $data = []
102  ) {
103  parent::__construct(
104  $name,
107  $reporting,
109  $request,
111  $meta,
112  $data
113  );
114  $this->stockRepository = $stockRepository;
115  $this->searchResultFactory = $searchResultFactory;
116  $this->getStockSourceLinks = $getStockSourceLinks;
117  $this->sourceRepository = $sourceRepository;
118  $this->apiSearchCriteriaBuilder = $apiSearchCriteriaBuilder;
119  $this->sortOrderBuilder = $sortOrderBuilder;
120  $this->getSourcesAssignedToStockOrderedByPriority = $getSourcesAssignedToStockOrderedByPriority;
121  }
122 
126  public function getData()
127  {
128  $data = parent::getData();
129  if ('inventory_stock_form_data_source' === $this->name) {
130  // It is need for support of several fieldsets.
131  // For details see \Magento\Ui\Component\Form::getDataSourceData
132  if ($data['totalRecords'] > 0) {
133  $stockId = (int)$data['items'][0][StockInterface::STOCK_ID];
134  $stockGeneralData = $data['items'][0];
135  $dataForSingle[$stockId] = [
136  'general' => $stockGeneralData,
137  'sources' => [
138  'assigned_sources' => $this->getAssignedSourcesData($stockId),
139  ],
140  ];
141  $data = $dataForSingle;
142  } else {
143  $data = [];
144  }
145  } elseif ('inventory_stock_listing_data_stock' === $this->name) {
146  if ($data['totalRecords'] > 0) {
147  foreach ($data['items'] as $index => $stock) {
148  $data['items'][$index]['assigned_sources'] = $this->getAssignedSourcesById($stock['stock_id']);
149  }
150  }
151  }
152 
153  return $data;
154  }
155 
159  public function getSearchResult()
160  {
162  $result = $this->stockRepository->getList($searchCriteria);
163 
164  $searchResult = $this->searchResultFactory->create(
165  $result->getItems(),
166  $result->getTotalCount(),
169  );
170  return $searchResult;
171  }
172 
177  private function getAssignedSourcesData(int $stockId): array
178  {
179  $sortOrder = $this->sortOrderBuilder
181  ->setAscendingDirection()
182  ->create();
183  $searchCriteria = $this->apiSearchCriteriaBuilder
184  ->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId)
185  ->addSortOrder($sortOrder)
186  ->create();
187 
188  $searchResult = $this->getStockSourceLinks->execute($searchCriteria);
189 
190  if ($searchResult->getTotalCount() === 0) {
191  return [];
192  }
193 
194  $assignedSourcesData = [];
195  foreach ($searchResult->getItems() as $link) {
196  $source = $this->sourceRepository->get($link->getSourceCode());
197 
198  $assignedSourcesData[] = [
199  SourceInterface::NAME => $source->getName(),
200  StockSourceLinkInterface::SOURCE_CODE => $link->getSourceCode(),
201  StockSourceLinkInterface::STOCK_ID => $link->getStockId(),
202  StockSourceLinkInterface::PRIORITY => $link->getPriority(),
203  ];
204  }
205  return $assignedSourcesData;
206  }
207 
214  private function getAssignedSourcesById(int $stockId): array
215  {
216  $sources = $this->getSourcesAssignedToStockOrderedByPriority->execute($stockId);
217  $sourcesData = [];
218  foreach ($sources as $source) {
219  $sourcesData[] = [
220  'sourceCode' => $source->getSourceCode(),
221  'name' => $source->getName()
222  ];
223  }
224 
225  return $sourcesData;
226  }
227 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
__construct( $name, $primaryFieldName, $requestFieldName, ReportingInterface $reporting, SearchSearchCriteriaBuilder $searchCriteriaBuilder, RequestInterface $request, FilterBuilder $filterBuilder, StockRepositoryInterface $stockRepository, SearchResultFactory $searchResultFactory, GetStockSourceLinksInterface $getStockSourceLinks, SourceRepositoryInterface $sourceRepository, SearchCriteriaBuilder $apiSearchCriteriaBuilder, SortOrderBuilder $sortOrderBuilder, GetSourcesAssignedToStockOrderedByPriorityInterface $getSourcesAssignedToStockOrderedByPriority, array $meta=[], array $data=[])
$stockRepository
Definition: stock.php:19
$sourcesData
Definition: sources.php:21
$stock
$sourceRepository
Definition: source.php:20
$index
Definition: list.phtml:44