66
77namespace Magento \Catalog \Model \Product \Price ;
88
9+ use Magento \Catalog \Api \Data \SpecialPriceInterface ;
10+ use Magento \Catalog \Api \Data \SpecialPriceInterfaceFactory ;
11+ use Magento \Catalog \Api \SpecialPriceStorageInterface ;
12+ use Magento \Catalog \Model \Product \Price \Validation \InvalidSkuProcessor ;
13+ use Magento \Catalog \Model \Product \Price \Validation \Result ;
14+ use Magento \Catalog \Model \ProductIdLocatorInterface ;
15+ use Magento \Framework \App \ObjectManager ;
916use Magento \Framework \Exception \NoSuchEntityException ;
17+ use Magento \Catalog \Helper \Data ;
18+ use Magento \Store \Api \StoreRepositoryInterface ;
1019
1120/**
1221 * Special price storage presents efficient price API and is used to retrieve, update or delete special prices.
22+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1323 */
14- class SpecialPriceStorage implements \ Magento \ Catalog \ Api \ SpecialPriceStorageInterface
24+ class SpecialPriceStorage implements SpecialPriceStorageInterface
1525{
1626 /**
1727 * @var \Magento\Catalog\Api\SpecialPriceInterface
1828 */
1929 private $ specialPriceResource ;
2030
2131 /**
22- * @var \Magento\Catalog\Api\Data\ SpecialPriceInterfaceFactory
32+ * @var SpecialPriceInterfaceFactory
2333 */
2434 private $ specialPriceFactory ;
2535
2636 /**
27- * @var \Magento\Catalog\Model\ ProductIdLocatorInterface
37+ * @var ProductIdLocatorInterface
2838 */
2939 private $ productIdLocator ;
3040
3141 /**
32- * @var \Magento\Store\Api\ StoreRepositoryInterface
42+ * @var StoreRepositoryInterface
3343 */
3444 private $ storeRepository ;
3545
3646 /**
37- * @var \Magento\Catalog\Model\Product\Price\Validation\ Result
47+ * @var Result
3848 */
3949 private $ validationResult ;
4050
4151 /**
42- * @var \Magento\Catalog\Model\Product\Price\Validation\ InvalidSkuProcessor
52+ * @var InvalidSkuProcessor
4353 */
4454 private $ invalidSkuProcessor ;
4555
4656 /**
4757 * @var array
4858 */
49- private $ allowedProductTypes = [];
59+ private $ allowedProductTypes ;
60+
61+ /**
62+ * @var Data
63+ */
64+ private $ catalogData ;
5065
5166 /**
5267 * @param \Magento\Catalog\Api\SpecialPriceInterface $specialPriceResource
53- * @param \Magento\Catalog\Api\Data\SpecialPriceInterfaceFactory $specialPriceFactory
54- * @param \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator
55- * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
56- * @param \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult
57- * @param \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor
58- * @param array $allowedProductTypes [optional]
68+ * @param SpecialPriceInterfaceFactory $specialPriceFactory
69+ * @param ProductIdLocatorInterface $productIdLocator
70+ * @param StoreRepositoryInterface $storeRepository
71+ * @param Result $validationResult
72+ * @param InvalidSkuProcessor $invalidSkuProcessor
73+ * @param array $allowedProductTypes
74+ * @param Data|null $catalogData
5975 */
6076 public function __construct (
6177 \Magento \Catalog \Api \SpecialPriceInterface $ specialPriceResource ,
62- \Magento \Catalog \Api \Data \SpecialPriceInterfaceFactory $ specialPriceFactory ,
63- \Magento \Catalog \Model \ProductIdLocatorInterface $ productIdLocator ,
64- \Magento \Store \Api \StoreRepositoryInterface $ storeRepository ,
65- \Magento \Catalog \Model \Product \Price \Validation \Result $ validationResult ,
66- \Magento \Catalog \Model \Product \Price \Validation \InvalidSkuProcessor $ invalidSkuProcessor ,
67- array $ allowedProductTypes = []
78+ SpecialPriceInterfaceFactory $ specialPriceFactory ,
79+ ProductIdLocatorInterface $ productIdLocator ,
80+ StoreRepositoryInterface $ storeRepository ,
81+ Result $ validationResult ,
82+ InvalidSkuProcessor $ invalidSkuProcessor ,
83+ array $ allowedProductTypes = [],
84+ ?Data $ catalogData = null
6885 ) {
6986 $ this ->specialPriceResource = $ specialPriceResource ;
7087 $ this ->specialPriceFactory = $ specialPriceFactory ;
@@ -73,10 +90,11 @@ public function __construct(
7390 $ this ->validationResult = $ validationResult ;
7491 $ this ->invalidSkuProcessor = $ invalidSkuProcessor ;
7592 $ this ->allowedProductTypes = $ allowedProductTypes ;
93+ $ this ->catalogData = $ catalogData ?: ObjectManager::getInstance ()->get (Data::class);
7694 }
7795
7896 /**
79- * { @inheritdoc}
97+ * @inheritdoc
8098 */
8199 public function get (array $ skus )
82100 {
@@ -85,7 +103,7 @@ public function get(array $skus)
85103
86104 $ prices = [];
87105 foreach ($ rawPrices as $ rawPrice ) {
88- /** @var \Magento\Catalog\Api\Data\ SpecialPriceInterface $price */
106+ /** @var SpecialPriceInterface $price */
89107 $ price = $ this ->specialPriceFactory ->create ();
90108 $ sku = isset ($ rawPrice ['sku ' ])
91109 ? $ rawPrice ['sku ' ]
@@ -102,7 +120,7 @@ public function get(array $skus)
102120 }
103121
104122 /**
105- * { @inheritdoc}
123+ * @inheritdoc
106124 */
107125 public function update (array $ prices )
108126 {
@@ -113,7 +131,7 @@ public function update(array $prices)
113131 }
114132
115133 /**
116- * { @inheritdoc}
134+ * @inheritdoc
117135 */
118136 public function delete (array $ prices )
119137 {
@@ -140,52 +158,14 @@ private function retrieveValidPrices(array $prices)
140158
141159 foreach ($ prices as $ key => $ price ) {
142160 if (!$ price ->getSku () || in_array ($ price ->getSku (), $ failedSkus )) {
143- $ this ->validationResult ->addFailedItem (
144- $ key ,
145- __ (
146- 'The product that was requested doesn \'t exist. Verify the product and try again. '
147- . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ,
148- [
149- 'SKU ' => $ price ->getSku (),
150- 'storeId ' => $ price ->getStoreId (),
151- 'priceFrom ' => $ price ->getPriceFrom (),
152- 'priceTo ' => $ price ->getPriceTo ()
153- ]
154- ),
155- [
156- 'SKU ' => $ price ->getSku (),
157- 'storeId ' => $ price ->getStoreId (),
158- 'priceFrom ' => $ price ->getPriceFrom (),
159- 'priceTo ' => $ price ->getPriceTo ()
160- ]
161- );
161+ $ errorMessage = 'The product that was requested doesn \'t exist. Verify the product and try again. '
162+ . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ;
163+ $ this ->addFailedItemPrice ($ price , $ key , $ errorMessage , []);
162164 }
165+ $ this ->checkStore ($ price , $ key );
163166 $ this ->checkPrice ($ price , $ key );
164167 $ this ->checkDate ($ price , $ price ->getPriceFrom (), 'Price From ' , $ key );
165168 $ this ->checkDate ($ price , $ price ->getPriceTo (), 'Price To ' , $ key );
166- try {
167- $ this ->storeRepository ->getById ($ price ->getStoreId ());
168- } catch (NoSuchEntityException $ e ) {
169- $ this ->validationResult ->addFailedItem (
170- $ key ,
171- __ (
172- 'Requested store is not found. '
173- . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ,
174- [
175- 'SKU ' => $ price ->getSku (),
176- 'storeId ' => $ price ->getStoreId (),
177- 'priceFrom ' => $ price ->getPriceFrom (),
178- 'priceTo ' => $ price ->getPriceTo ()
179- ]
180- ),
181- [
182- 'SKU ' => $ price ->getSku (),
183- 'storeId ' => $ price ->getStoreId (),
184- 'priceFrom ' => $ price ->getPriceFrom (),
185- 'priceTo ' => $ price ->getPriceTo ()
186- ]
187- );
188- }
189169 }
190170
191171 foreach ($ this ->validationResult ->getFailedRowIds () as $ id ) {
@@ -195,77 +175,95 @@ private function retrieveValidPrices(array $prices)
195175 return $ prices ;
196176 }
197177
178+ /**
179+ * Check that store exists and is global when price scope is global and otherwise add error to aggregator.
180+ *
181+ * @param SpecialPriceInterface $price
182+ * @param int $key
183+ * @return void
184+ */
185+ private function checkStore (SpecialPriceInterface $ price , int $ key ): void
186+ {
187+ if ($ this ->catalogData ->isPriceGlobal () && $ price ->getStoreId () !== 0 ) {
188+ $ errorMessage = 'Could not change non global Price when price scope is global. '
189+ . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ;
190+ $ this ->addFailedItemPrice ($ price , $ key , $ errorMessage , []);
191+ }
192+
193+ try {
194+ $ this ->storeRepository ->getById ($ price ->getStoreId ());
195+ } catch (NoSuchEntityException $ e ) {
196+ $ errorMessage = 'Requested store is not found. '
197+ . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ;
198+ $ this ->addFailedItemPrice ($ price , $ key , $ errorMessage , []);
199+ }
200+ }
201+
198202 /**
199203 * Check that date value is correct and add error to aggregator if it contains incorrect data.
200204 *
201- * @param \Magento\Catalog\Api\Data\ SpecialPriceInterface $price
205+ * @param SpecialPriceInterface $price
202206 * @param string $value
203207 * @param string $label
204208 * @param int $key
205209 * @return void
206210 */
207- private function checkDate (\ Magento \ Catalog \ Api \ Data \ SpecialPriceInterface $ price , $ value , $ label , $ key )
211+ private function checkDate (SpecialPriceInterface $ price , $ value , $ label , $ key )
208212 {
209213 if ($ value && !$ this ->isCorrectDateValue ($ value )) {
210- $ this ->validationResult ->addFailedItem (
211- $ key ,
212- __ (
213- 'Invalid attribute %label = %priceTo. '
214- . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ,
215- [
216- 'label ' => $ label ,
217- 'SKU ' => $ price ->getSku (),
218- 'storeId ' => $ price ->getStoreId (),
219- 'priceFrom ' => $ price ->getPriceFrom (),
220- 'priceTo ' => $ price ->getPriceTo ()
221- ]
222- ),
223- [
224- 'label ' => $ label ,
225- 'SKU ' => $ price ->getSku (),
226- 'storeId ' => $ price ->getStoreId (),
227- 'priceFrom ' => $ price ->getPriceFrom (),
228- 'priceTo ' => $ price ->getPriceTo ()
229- ]
230- );
214+ $ errorMessage = 'Invalid attribute %label = %priceTo. '
215+ . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ;
216+ $ this ->addFailedItemPrice ($ price , $ key , $ errorMessage , ['label ' => $ label ]);
231217 }
232218 }
233219
234220 /**
235- * Check that provided price value is not empty and not lower then zero and add error to aggregator if price
221+ * Check price.
222+ *
223+ * Verify that provided price value is not empty and not lower then zero and add error to aggregator if price
236224 * contains not valid data.
237225 *
238- * @param \Magento\Catalog\Api\Data\ SpecialPriceInterface $price
226+ * @param SpecialPriceInterface $price
239227 * @param int $key
240228 * @return void
241229 */
242- private function checkPrice (\ Magento \ Catalog \ Api \ Data \ SpecialPriceInterface $ price , $ key )
230+ private function checkPrice (SpecialPriceInterface $ price , int $ key ): void
243231 {
244232 if (null === $ price ->getPrice () || $ price ->getPrice () < 0 ) {
245- $ this ->validationResult ->addFailedItem (
246- $ key ,
247- __ (
248- 'Invalid attribute Price = %price. '
249- . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ,
250- [
251- 'price ' => $ price ->getPrice (),
252- 'SKU ' => $ price ->getSku (),
253- 'storeId ' => $ price ->getStoreId (),
254- 'priceFrom ' => $ price ->getPriceFrom (),
255- 'priceTo ' => $ price ->getPriceTo ()
256- ]
257- ),
258- [
259- 'price ' => $ price ->getPrice (),
260- 'SKU ' => $ price ->getSku (),
261- 'storeId ' => $ price ->getStoreId (),
262- 'priceFrom ' => $ price ->getPriceFrom (),
263- 'priceTo ' => $ price ->getPriceTo ()
264- ]
265- );
233+ $ errorMessage = 'Invalid attribute Price = %price. '
234+ . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo. ' ;
235+ $ this ->addFailedItemPrice ($ price , $ key , $ errorMessage , ['price ' => $ price ->getPrice ()]);
266236 }
267237 }
268238
239+ /**
240+ * Adds failed item price to validation result
241+ *
242+ * @param SpecialPriceInterface $price
243+ * @param int $key
244+ * @param string $message
245+ * @param array $firstParam
246+ * @return void
247+ */
248+ private function addFailedItemPrice (
249+ SpecialPriceInterface $ price ,
250+ int $ key ,
251+ string $ message ,
252+ array $ firstParam
253+ ): void {
254+ $ additionalInfo = [];
255+ if ($ firstParam ) {
256+ $ additionalInfo = array_merge ($ additionalInfo , $ firstParam );
257+ }
258+
259+ $ additionalInfo ['SKU ' ] = $ price ->getSku ();
260+ $ additionalInfo ['storeId ' ] = $ price ->getStoreId ();
261+ $ additionalInfo ['priceFrom ' ] = $ price ->getPriceFrom ();
262+ $ additionalInfo ['priceTo ' ] = $ price ->getPriceTo ();
263+
264+ $ this ->validationResult ->addFailedItem ($ key , __ ($ message , $ additionalInfo ), $ additionalInfo );
265+ }
266+
269267 /**
270268 * Retrieve SKU by product ID.
271269 *
0 commit comments