Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LoadSalesChannelsOnGetListPlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 use Psr\Log\LoggerInterface;
15 
20 {
24  private $getAssignedSalesChannelsForStock;
25 
29  private $logger;
30 
35  public function __construct(
36  GetAssignedSalesChannelsForStockInterface $getAssignedSalesChannelsForStock,
37  LoggerInterface $logger
38  ) {
39  $this->getAssignedSalesChannelsForStock = $getAssignedSalesChannelsForStock;
40  $this->logger = $logger;
41  }
42 
52  public function afterGetList(
53  StockRepositoryInterface $subject,
54  StockSearchResultsInterface $stockSearchResults
56  try {
57  return $this->doAfterGetList($stockSearchResults);
58  } catch (\Exception $e) {
59  $this->logger->error($e->getMessage());
60  throw new StateException(__('Could not load Sales Channels for Stock'), $e);
61  }
62  }
63 
68  private function doAfterGetList(StockSearchResultsInterface $stockSearchResults): StockSearchResultsInterface
69  {
70  $stocks = [];
71  foreach ($stockSearchResults->getItems() as $stock) {
72  $salesChannels = $this->getAssignedSalesChannelsForStock->execute((int)$stock->getStockId());
73 
74  $extensionAttributes = $stock->getExtensionAttributes();
75  $extensionAttributes->setSalesChannels($salesChannels);
76  $stock->setExtensionAttributes($extensionAttributes);
77  $stocks[] = $stock;
78  }
79  $stockSearchResults->setItems($stocks);
80  return $stockSearchResults;
81  }
82 }
__()
Definition: __.php:13
$logger
__construct(GetAssignedSalesChannelsForStockInterface $getAssignedSalesChannelsForStock, LoggerInterface $logger)
afterGetList(StockRepositoryInterface $subject, StockSearchResultsInterface $stockSearchResults)
$extensionAttributes
Definition: payment.php:22
$stock