Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CostStorage.php
Go to the documentation of this file.
1 <?php
8 
13 {
19  private $attributeCode = 'cost';
20 
24  private $pricePersistence;
25 
29  private $costInterfaceFactory;
30 
34  private $productIdLocator;
35 
39  private $validationResult;
40 
44  private $invalidSkuProcessor;
45 
51  private $allowedProductTypes = [];
52 
56  private $pricePersistenceFactory;
57 
61  private $storeRepository;
62 
74  public function __construct(
75  PricePersistenceFactory $pricePersistenceFactory,
76  \Magento\Catalog\Api\Data\CostInterfaceFactory $costInterfaceFactory,
77  \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator,
78  \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
79  \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult,
80  \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor,
81  array $allowedProductTypes = []
82  ) {
83  $this->pricePersistenceFactory = $pricePersistenceFactory;
84  $this->costInterfaceFactory = $costInterfaceFactory;
85  $this->productIdLocator = $productIdLocator;
86  $this->storeRepository = $storeRepository;
87  $this->validationResult = $validationResult;
88  $this->invalidSkuProcessor = $invalidSkuProcessor;
89  $this->allowedProductTypes = $allowedProductTypes;
90  }
91 
95  public function get(array $skus)
96  {
97  $skus = $this->invalidSkuProcessor->filterSkuList($skus, $this->allowedProductTypes);
98  $rawPrices = $this->getPricePersistence()->get($skus);
99  $prices = [];
100  foreach ($rawPrices as $rawPrice) {
101  $price = $this->costInterfaceFactory->create();
102  $sku = $this->getPricePersistence()
103  ->retrieveSkuById($rawPrice[$this->getPricePersistence()->getEntityLinkField()], $skus);
104  $price->setSku($sku);
105  $price->setCost($rawPrice['value']);
106  $price->setStoreId($rawPrice['store_id']);
107  $prices[] = $price;
108  }
109 
110  return $prices;
111  }
112 
116  public function update(array $prices)
117  {
118  $prices = $this->retrieveValidPrices($prices);
119  $formattedPrices = [];
120 
121  foreach ($prices as $price) {
122  $productIdsBySkus = $this->productIdLocator->retrieveProductIdsBySkus([$price->getSku()]);
123  $productIds = array_keys($productIdsBySkus[$price->getSku()]);
124  foreach ($productIds as $id) {
125  $formattedPrices[] = [
126  'store_id' => $price->getStoreId(),
127  $this->getPricePersistence()->getEntityLinkField() => $id,
128  'value' => $price->getCost(),
129  ];
130  }
131  }
132 
133  $this->getPricePersistence()->update($formattedPrices);
134 
135  return $this->validationResult->getFailedItems();
136  }
137 
141  public function delete(array $skus)
142  {
143  $skus = $this->invalidSkuProcessor->filterSkuList($skus, $this->allowedProductTypes);
144  $this->getPricePersistence()->delete($skus);
145 
146  return true;
147  }
148 
154  private function getPricePersistence()
155  {
156  if (!$this->pricePersistence) {
157  $this->pricePersistence = $this->pricePersistenceFactory->create(['attributeCode' => $this->attributeCode]);
158  }
159 
160  return $this->pricePersistence;
161  }
162 
169  private function retrieveValidPrices(array $prices)
170  {
171  $skus = array_unique(
172  array_map(function ($price) {
173  return $price->getSku();
174  }, $prices)
175  );
176  $invalidSkus = $this->invalidSkuProcessor->retrieveInvalidSkuList($skus, $this->allowedProductTypes);
177 
178  foreach ($prices as $id => $price) {
179  if (!$price->getSku() || in_array($price->getSku(), $invalidSkus)) {
180  $this->validationResult->addFailedItem(
181  $id,
182  __(
183  'Invalid attribute %fieldName = %fieldValue.',
184  ['fieldName' => '%fieldName', 'fieldValue' => '%fieldValue']
185  ),
186  ['fieldName' => 'SKU', 'fieldValue' => $price->getSku()]
187  );
188  }
189  if (null === $price->getCost() || $price->getCost() < 0) {
190  $this->validationResult->addFailedItem(
191  $id,
192  __(
193  'Invalid attribute Cost = %cost. Row ID: SKU = %SKU, Store ID: %storeId.',
194  ['cost' => $price->getCost(), 'SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
195  ),
196  ['cost' => $price->getCost(), 'SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
197  );
198  }
199  try {
200  $this->storeRepository->getById($price->getStoreId());
201  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
202  $this->validationResult->addFailedItem(
203  $id,
204  __(
205  'Requested store is not found. Row ID: SKU = %SKU, Store ID: %storeId.',
206  ['SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
207  ),
208  ['SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
209  );
210  }
211  }
212 
213  foreach ($this->validationResult->getFailedRowIds() as $id) {
214  unset($prices[$id]);
215  }
216 
217  return $prices;
218  }
219 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$price
foreach($websiteCodes as $websiteCode) $skus
__construct(PricePersistenceFactory $pricePersistenceFactory, \Magento\Catalog\Api\Data\CostInterfaceFactory $costInterfaceFactory, \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator, \Magento\Store\Api\StoreRepositoryInterface $storeRepository, \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult, \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor, array $allowedProductTypes=[])
Definition: CostStorage.php:74