6
6
namespace Magento \Swatches \Helper ;
7
7
8
8
use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface ;
9
- use Magento \Catalog \Api \Data \ProductInterface as Product ;
9
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
- use Magento \Catalog \Model \Product as ModelProduct ;
11
+ use Magento \Catalog \Model \Product ;
12
12
use Magento \Catalog \Model \Product \Image \UrlBuilder ;
13
13
use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
14
14
use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
23
23
use Magento \Swatches \Model \SwatchAttributeType ;
24
24
25
25
/**
26
- * Class Helper Data
27
- *
28
26
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29
27
*/
30
28
class Data
31
29
{
32
30
/**
33
31
* When we init media gallery empty image types contain this value.
34
32
*/
35
- const EMPTY_IMAGE_VALUE = 'no_selection ' ;
33
+ public const EMPTY_IMAGE_VALUE = 'no_selection ' ;
36
34
37
35
/**
38
36
* The int value of the Default store ID
39
37
*/
40
- const DEFAULT_STORE_ID = 0 ;
38
+ public const DEFAULT_STORE_ID = 0 ;
41
39
42
40
/**
43
41
* @var CollectionFactory
@@ -83,8 +81,11 @@ class Data
83
81
];
84
82
85
83
/**
86
- * Serializer to/from JSON.
87
- *
84
+ * @var array
85
+ */
86
+ private $ swatchesCache = [];
87
+
88
+ /**
88
89
* @var Json
89
90
*/
90
91
private $ serializer ;
@@ -106,7 +107,7 @@ class Data
106
107
* @param SwatchCollectionFactory $swatchCollectionFactory
107
108
* @param UrlBuilder $urlBuilder
108
109
* @param Json|null $serializer
109
- * @param SwatchAttributesProvider $swatchAttributesProvider
110
+ * @param SwatchAttributesProvider|null $swatchAttributesProvider
110
111
* @param SwatchAttributeType|null $swatchTypeChecker
111
112
*/
112
113
public function __construct (
@@ -123,12 +124,12 @@ public function __construct(
123
124
$ this ->productRepository = $ productRepository ;
124
125
$ this ->storeManager = $ storeManager ;
125
126
$ this ->swatchCollectionFactory = $ swatchCollectionFactory ;
127
+ $ this ->imageUrlBuilder = $ urlBuilder ;
126
128
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->create (Json::class);
127
129
$ this ->swatchAttributesProvider = $ swatchAttributesProvider
128
130
?: ObjectManager::getInstance ()->get (SwatchAttributesProvider::class);
129
131
$ this ->swatchTypeChecker = $ swatchTypeChecker
130
132
?: ObjectManager::getInstance ()->create (SwatchAttributeType::class);
131
- $ this ->imageUrlBuilder = $ urlBuilder ;
132
133
}
133
134
134
135
/**
@@ -163,11 +164,11 @@ public function assembleAdditionalDataEavAttribute(Attribute $attribute)
163
164
/**
164
165
* Check is media attribute available
165
166
*
166
- * @param ModelProduct $product
167
+ * @param Product $product
167
168
* @param string $attributeCode
168
169
* @return bool
169
170
*/
170
- private function isMediaAvailable (ModelProduct $ product , string $ attributeCode ): bool
171
+ private function isMediaAvailable (Product $ product , string $ attributeCode ): bool
171
172
{
172
173
$ isAvailable = false ;
173
174
@@ -186,11 +187,11 @@ private function isMediaAvailable(ModelProduct $product, string $attributeCode):
186
187
* Load first variation
187
188
*
188
189
* @param string $attributeCode swatch_image|image
189
- * @param ModelProduct $configurableProduct
190
+ * @param Product $configurableProduct
190
191
* @param array $requiredAttributes
191
- * @return bool|Product
192
+ * @return bool|ProductInterface
192
193
*/
193
- private function loadFirstVariation ($ attributeCode , ModelProduct $ configurableProduct , array $ requiredAttributes )
194
+ private function loadFirstVariation ($ attributeCode , Product $ configurableProduct , array $ requiredAttributes )
194
195
{
195
196
if ($ this ->isProductHasSwatch ($ configurableProduct )) {
196
197
$ usedProducts = $ configurableProduct ->getTypeInstance ()->getUsedProducts ($ configurableProduct );
@@ -210,35 +211,35 @@ private function loadFirstVariation($attributeCode, ModelProduct $configurablePr
210
211
/**
211
212
* Load first variation with swatch image
212
213
*
213
- * @param Product $configurableProduct
214
+ * @param ProductInterface| Product $configurableProduct
214
215
* @param array $requiredAttributes
215
- * @return bool|Product
216
+ * @return bool|ProductInterface
216
217
*/
217
- public function loadFirstVariationWithSwatchImage (Product $ configurableProduct , array $ requiredAttributes )
218
+ public function loadFirstVariationWithSwatchImage (ProductInterface $ configurableProduct , array $ requiredAttributes )
218
219
{
219
220
return $ this ->loadFirstVariation ('swatch_image ' , $ configurableProduct , $ requiredAttributes );
220
221
}
221
222
222
223
/**
223
224
* Load first variation with image
224
225
*
225
- * @param Product $configurableProduct
226
+ * @param ProductInterface| Product $configurableProduct
226
227
* @param array $requiredAttributes
227
- * @return bool|Product
228
+ * @return bool|ProductInterface
228
229
*/
229
- public function loadFirstVariationWithImage (Product $ configurableProduct , array $ requiredAttributes )
230
+ public function loadFirstVariationWithImage (ProductInterface $ configurableProduct , array $ requiredAttributes )
230
231
{
231
232
return $ this ->loadFirstVariation ('image ' , $ configurableProduct , $ requiredAttributes );
232
233
}
233
234
234
235
/**
235
236
* Load Variation Product using fallback
236
237
*
237
- * @param Product $parentProduct
238
+ * @param ProductInterface $parentProduct
238
239
* @param array $attributes
239
- * @return bool|Product
240
+ * @return bool|ProductInterface
240
241
*/
241
- public function loadVariationByFallback (Product $ parentProduct , array $ attributes )
242
+ public function loadVariationByFallback (ProductInterface $ parentProduct , array $ attributes )
242
243
{
243
244
if (!$ this ->isProductHasSwatch ($ parentProduct )) {
244
245
return false ;
@@ -318,12 +319,12 @@ private function addFilterByParent(ProductCollection $productCollection, $parent
318
319
* ]
319
320
* ]
320
321
*
321
- * @param ModelProduct $product
322
+ * @param Product $product
322
323
*
323
324
* @return array
324
325
* @throws \Magento\Framework\Exception\LocalizedException
325
326
*/
326
- public function getProductMediaGallery (ModelProduct $ product ): array
327
+ public function getProductMediaGallery (Product $ product ): array
327
328
{
328
329
$ baseImage = null ;
329
330
$ gallery = [];
@@ -394,22 +395,21 @@ private function getAllSizeImages($imageFile)
394
395
/**
395
396
* Retrieve collection of Swatch attributes
396
397
*
397
- * @param Product $product
398
+ * @param ProductInterface| Product $product
398
399
* @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute[]
399
400
*/
400
- private function getSwatchAttributes (Product $ product )
401
+ private function getSwatchAttributes (ProductInterface $ product )
401
402
{
402
- $ swatchAttributes = $ this ->swatchAttributesProvider ->provide ($ product );
403
- return $ swatchAttributes ;
403
+ return $ this ->swatchAttributesProvider ->provide ($ product );
404
404
}
405
405
406
406
/**
407
407
* Retrieve collection of Eav Attributes from Configurable product
408
408
*
409
- * @param Product $product
409
+ * @param ProductInterface| Product $product
410
410
* @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute[]
411
411
*/
412
- public function getAttributesFromConfigurable (Product $ product )
412
+ public function getAttributesFromConfigurable (ProductInterface $ product )
413
413
{
414
414
$ result = [];
415
415
$ typeInstance = $ product ->getTypeInstance ();
@@ -428,10 +428,10 @@ public function getAttributesFromConfigurable(Product $product)
428
428
/**
429
429
* Retrieve all visible Swatch attributes for current product.
430
430
*
431
- * @param Product $product
431
+ * @param ProductInterface $product
432
432
* @return array
433
433
*/
434
- public function getSwatchAttributesAsArray (Product $ product )
434
+ public function getSwatchAttributesAsArray (ProductInterface $ product )
435
435
{
436
436
$ result = [];
437
437
$ swatchAttributes = $ this ->getSwatchAttributes ($ product );
@@ -447,11 +447,6 @@ public function getSwatchAttributesAsArray(Product $product)
447
447
return $ result ;
448
448
}
449
449
450
- /**
451
- * @var array
452
- */
453
- private $ swatchesCache = [];
454
-
455
450
/**
456
451
* Get swatch options by option id's according to fallback logic
457
452
*
@@ -511,7 +506,7 @@ private function getCachedSwatches(array $optionIds)
511
506
private function setCachedSwatches (array $ optionIds , array $ swatches )
512
507
{
513
508
foreach ($ optionIds as $ optionId ) {
514
- $ this ->swatchesCache [$ optionId ] = isset ( $ swatches [$ optionId ]) ? $ swatches [ $ optionId ] : null ;
509
+ $ this ->swatchesCache [$ optionId ] = $ swatches [$ optionId ] ?? null ;
515
510
}
516
511
}
517
512
@@ -543,10 +538,10 @@ private function addFallbackOptions(array $fallbackValues, array $swatches)
543
538
/**
544
539
* Check if the Product has Swatch attributes
545
540
*
546
- * @param Product $product
541
+ * @param ProductInterface $product
547
542
* @return bool
548
543
*/
549
- public function isProductHasSwatch (Product $ product )
544
+ public function isProductHasSwatch (ProductInterface $ product )
550
545
{
551
546
return !empty ($ this ->getSwatchAttributes ($ product ));
552
547
}
0 commit comments