66namespace Magento \Swatches \Helper ;
77
88use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface ;
9- use Magento \Catalog \Api \Data \ProductInterface as Product ;
9+ use Magento \Catalog \Api \Data \ProductInterface ;
1010use Magento \Catalog \Api \ProductRepositoryInterface ;
11- use Magento \Catalog \Model \Product as ModelProduct ;
11+ use Magento \Catalog \Model \Product ;
1212use Magento \Catalog \Model \Product \Image \UrlBuilder ;
1313use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
1414use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
2323use Magento \Swatches \Model \SwatchAttributeType ;
2424
2525/**
26- * Class Helper Data
27- *
2826 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2927 */
3028class Data
@@ -83,8 +81,11 @@ class Data
8381 ];
8482
8583 /**
86- * Serializer to/from JSON.
87- *
84+ * @var array
85+ */
86+ private $ swatchesCache = [];
87+
88+ /**
8889 * @var Json
8990 */
9091 private $ serializer ;
@@ -106,7 +107,7 @@ class Data
106107 * @param SwatchCollectionFactory $swatchCollectionFactory
107108 * @param UrlBuilder $urlBuilder
108109 * @param Json|null $serializer
109- * @param SwatchAttributesProvider $swatchAttributesProvider
110+ * @param SwatchAttributesProvider|null $swatchAttributesProvider
110111 * @param SwatchAttributeType|null $swatchTypeChecker
111112 */
112113 public function __construct (
@@ -123,12 +124,12 @@ public function __construct(
123124 $ this ->productRepository = $ productRepository ;
124125 $ this ->storeManager = $ storeManager ;
125126 $ this ->swatchCollectionFactory = $ swatchCollectionFactory ;
127+ $ this ->imageUrlBuilder = $ urlBuilder ;
126128 $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->create (Json::class);
127129 $ this ->swatchAttributesProvider = $ swatchAttributesProvider
128130 ?: ObjectManager::getInstance ()->get (SwatchAttributesProvider::class);
129131 $ this ->swatchTypeChecker = $ swatchTypeChecker
130132 ?: ObjectManager::getInstance ()->create (SwatchAttributeType::class);
131- $ this ->imageUrlBuilder = $ urlBuilder ;
132133 }
133134
134135 /**
@@ -163,11 +164,11 @@ public function assembleAdditionalDataEavAttribute(Attribute $attribute)
163164 /**
164165 * Check is media attribute available
165166 *
166- * @param ModelProduct $product
167+ * @param Product $product
167168 * @param string $attributeCode
168169 * @return bool
169170 */
170- private function isMediaAvailable (ModelProduct $ product , string $ attributeCode ): bool
171+ private function isMediaAvailable (Product $ product , string $ attributeCode ): bool
171172 {
172173 $ isAvailable = false ;
173174
@@ -186,11 +187,11 @@ private function isMediaAvailable(ModelProduct $product, string $attributeCode):
186187 * Load first variation
187188 *
188189 * @param string $attributeCode swatch_image|image
189- * @param ModelProduct $configurableProduct
190+ * @param Product $configurableProduct
190191 * @param array $requiredAttributes
191- * @return bool|Product
192+ * @return bool|ProductInterface
192193 */
193- private function loadFirstVariation ($ attributeCode , ModelProduct $ configurableProduct , array $ requiredAttributes )
194+ private function loadFirstVariation ($ attributeCode , Product $ configurableProduct , array $ requiredAttributes )
194195 {
195196 if ($ this ->isProductHasSwatch ($ configurableProduct )) {
196197 $ usedProducts = $ configurableProduct ->getTypeInstance ()->getUsedProducts ($ configurableProduct );
@@ -210,35 +211,35 @@ private function loadFirstVariation($attributeCode, ModelProduct $configurablePr
210211 /**
211212 * Load first variation with swatch image
212213 *
213- * @param Product $configurableProduct
214+ * @param ProductInterface| Product $configurableProduct
214215 * @param array $requiredAttributes
215- * @return bool|Product
216+ * @return bool|ProductInterface
216217 */
217- public function loadFirstVariationWithSwatchImage (Product $ configurableProduct , array $ requiredAttributes )
218+ public function loadFirstVariationWithSwatchImage (ProductInterface $ configurableProduct , array $ requiredAttributes )
218219 {
219220 return $ this ->loadFirstVariation ('swatch_image ' , $ configurableProduct , $ requiredAttributes );
220221 }
221222
222223 /**
223224 * Load first variation with image
224225 *
225- * @param Product $configurableProduct
226+ * @param ProductInterface| Product $configurableProduct
226227 * @param array $requiredAttributes
227- * @return bool|Product
228+ * @return bool|ProductInterface
228229 */
229- public function loadFirstVariationWithImage (Product $ configurableProduct , array $ requiredAttributes )
230+ public function loadFirstVariationWithImage (ProductInterface $ configurableProduct , array $ requiredAttributes )
230231 {
231232 return $ this ->loadFirstVariation ('image ' , $ configurableProduct , $ requiredAttributes );
232233 }
233234
234235 /**
235236 * Load Variation Product using fallback
236237 *
237- * @param Product $parentProduct
238+ * @param ProductInterface $parentProduct
238239 * @param array $attributes
239- * @return bool|Product
240+ * @return bool|ProductInterface
240241 */
241- public function loadVariationByFallback (Product $ parentProduct , array $ attributes )
242+ public function loadVariationByFallback (ProductInterface $ parentProduct , array $ attributes )
242243 {
243244 if (!$ this ->isProductHasSwatch ($ parentProduct )) {
244245 return false ;
@@ -318,12 +319,12 @@ private function addFilterByParent(ProductCollection $productCollection, $parent
318319 * ]
319320 * ]
320321 *
321- * @param ModelProduct $product
322+ * @param Product $product
322323 *
323324 * @return array
324325 * @throws \Magento\Framework\Exception\LocalizedException
325326 */
326- public function getProductMediaGallery (ModelProduct $ product ): array
327+ public function getProductMediaGallery (Product $ product ): array
327328 {
328329 $ baseImage = null ;
329330 $ gallery = [];
@@ -394,22 +395,21 @@ private function getAllSizeImages($imageFile)
394395 /**
395396 * Retrieve collection of Swatch attributes
396397 *
397- * @param Product $product
398+ * @param ProductInterface| Product $product
398399 * @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute[]
399400 */
400- private function getSwatchAttributes (Product $ product )
401+ private function getSwatchAttributes (ProductInterface $ product )
401402 {
402- $ swatchAttributes = $ this ->swatchAttributesProvider ->provide ($ product );
403- return $ swatchAttributes ;
403+ return $ this ->swatchAttributesProvider ->provide ($ product );
404404 }
405405
406406 /**
407407 * Retrieve collection of Eav Attributes from Configurable product
408408 *
409- * @param Product $product
409+ * @param ProductInterface| Product $product
410410 * @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute[]
411411 */
412- public function getAttributesFromConfigurable (Product $ product )
412+ public function getAttributesFromConfigurable (ProductInterface $ product )
413413 {
414414 $ result = [];
415415 $ typeInstance = $ product ->getTypeInstance ();
@@ -428,10 +428,10 @@ public function getAttributesFromConfigurable(Product $product)
428428 /**
429429 * Retrieve all visible Swatch attributes for current product.
430430 *
431- * @param Product $product
431+ * @param ProductInterface $product
432432 * @return array
433433 */
434- public function getSwatchAttributesAsArray (Product $ product )
434+ public function getSwatchAttributesAsArray (ProductInterface $ product )
435435 {
436436 $ result = [];
437437 $ swatchAttributes = $ this ->getSwatchAttributes ($ product );
@@ -447,11 +447,6 @@ public function getSwatchAttributesAsArray(Product $product)
447447 return $ result ;
448448 }
449449
450- /**
451- * @var array
452- */
453- private $ swatchesCache = [];
454-
455450 /**
456451 * Get swatch options by option id's according to fallback logic
457452 *
@@ -511,7 +506,7 @@ private function getCachedSwatches(array $optionIds)
511506 private function setCachedSwatches (array $ optionIds , array $ swatches )
512507 {
513508 foreach ($ optionIds as $ optionId ) {
514- $ this ->swatchesCache [$ optionId ] = isset ( $ swatches [$ optionId ]) ? $ swatches [ $ optionId ] : null ;
509+ $ this ->swatchesCache [$ optionId ] = $ swatches [$ optionId ] ?? null ;
515510 }
516511 }
517512
@@ -543,10 +538,10 @@ private function addFallbackOptions(array $fallbackValues, array $swatches)
543538 /**
544539 * Check if the Product has Swatch attributes
545540 *
546- * @param Product $product
541+ * @param ProductInterface $product
547542 * @return bool
548543 */
549- public function isProductHasSwatch (Product $ product )
544+ public function isProductHasSwatch (ProductInterface $ product )
550545 {
551546 return !empty ($ this ->getSwatchAttributes ($ product ));
552547 }
0 commit comments