33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6- declare (strict_types=1 );
7-
86namespace Magento \Swatches \Model \Plugin ;
97
108use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
119use Magento \Framework \App \ObjectManager ;
1210use Magento \Framework \Exception \InputException ;
13- use Magento \Framework \Exception \LocalizedException ;
1411use Magento \Framework \Serialize \Serializer \Json ;
15- use Magento \Swatches \Helper \Data ;
1612use Magento \Swatches \Model \ResourceModel \Swatch as SwatchResource ;
17- use Magento \Swatches \Model \ResourceModel \Swatch \CollectionFactory ;
1813use Magento \Swatches \Model \Swatch ;
19- use Magento \Swatches \Model \SwatchFactory ;
2014
2115/**
2216 * Plugin model for Catalog Resource Attribute
@@ -27,6 +21,11 @@ class EavAttribute
2721{
2822 const DEFAULT_STORE_ID = 0 ;
2923
24+ /**
25+ * @var SwatchResource
26+ */
27+ private $ swatchResource ;
28+
3029 /**
3130 * Base option title used for string operations to detect is option already exists or new
3231 */
@@ -38,30 +37,20 @@ class EavAttribute
3837 private const API_OPTION_PREFIX = 'id_ ' ;
3938
4039 /**
41- * @var CollectionFactory
40+ * @var \Magento\Swatches\Model\ResourceModel\Swatch\ CollectionFactory
4241 */
4342 protected $ swatchCollectionFactory ;
4443
4544 /**
46- * @var SwatchFactory
45+ * @var \Magento\Swatches\Model\ SwatchFactory
4746 */
4847 protected $ swatchFactory ;
4948
5049 /**
51- * @var Data
50+ * @var \Magento\Swatches\Helper\ Data
5251 */
5352 protected $ swatchHelper ;
5453
55- /**
56- * @var Json
57- */
58- private $ serializer ;
59-
60- /**
61- * @var SwatchResource
62- */
63- private $ swatchResource ;
64-
6554 /**
6655 * Array which contains links for new created attributes for swatches
6756 *
@@ -77,16 +66,23 @@ class EavAttribute
7766 protected $ isSwatchExists ;
7867
7968 /**
80- * @param CollectionFactory $collectionFactory
81- * @param SwatchFactory $swatchFactory
82- * @param Data $swatchHelper
69+ * Serializer from arrays to string.
70+ *
71+ * @var Json
72+ */
73+ private $ serializer ;
74+
75+ /**
76+ * @param \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory
77+ * @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
78+ * @param \Magento\Swatches\Helper\Data $swatchHelper
8379 * @param Json|null $serializer
8480 * @param SwatchResource|null $swatchResource
8581 */
8682 public function __construct (
87- CollectionFactory $ collectionFactory ,
88- SwatchFactory $ swatchFactory ,
89- Data $ swatchHelper ,
83+ \ Magento \ Swatches \ Model \ ResourceModel \ Swatch \ CollectionFactory $ collectionFactory ,
84+ \ Magento \ Swatches \ Model \ SwatchFactory $ swatchFactory ,
85+ \ Magento \ Swatches \ Helper \ Data $ swatchHelper ,
9086 Json $ serializer = null ,
9187 SwatchResource $ swatchResource = null
9288 ) {
@@ -101,7 +97,6 @@ public function __construct(
10197 * Set base data to Attribute
10298 *
10399 * @param Attribute $attribute
104- *
105100 * @return void
106101 */
107102 public function beforeBeforeSave (Attribute $ attribute )
@@ -118,9 +113,8 @@ public function beforeBeforeSave(Attribute $attribute)
118113 * Swatch save operations
119114 *
120115 * @param Attribute $attribute
121- *
116+ * @throws \Magento\Framework\Exception\LocalizedException
122117 * @return void
123- * @throws LocalizedException
124118 */
125119 public function afterAfterSave (Attribute $ attribute )
126120 {
@@ -135,7 +129,6 @@ public function afterAfterSave(Attribute $attribute)
135129 * Substitute suitable options and swatches arrays
136130 *
137131 * @param Attribute $attribute
138- *
139132 * @return void
140133 */
141134 protected function setProperOptionsArray (Attribute $ attribute )
@@ -171,9 +164,8 @@ protected function setProperOptionsArray(Attribute $attribute)
171164 * Prepare attribute for conversion from any swatch type to dropdown
172165 *
173166 * @param Attribute $attribute
174- *
167+ * @throws \Magento\Framework\Exception\LocalizedException
175168 * @return void
176- * @throws LocalizedException
177169 */
178170 protected function convertSwatchToDropdown (Attribute $ attribute )
179171 {
@@ -195,9 +187,8 @@ protected function convertSwatchToDropdown(Attribute $attribute)
195187 * Creates array which link new option ids
196188 *
197189 * @param Attribute $attribute
198- *
199190 * @return Attribute
200- * @throws LocalizedException
191+ * @throws \Magento\Framework\Exception\ LocalizedException
201192 */
202193 protected function processSwatchOptions (Attribute $ attribute )
203194 {
@@ -218,21 +209,19 @@ protected function processSwatchOptions(Attribute $attribute)
218209 * Get options array without deleted items
219210 *
220211 * @param array $optionsArray
221- *
222212 * @return array
223213 */
224214 protected function prepareOptionIds (array $ optionsArray )
225215 {
226- if (! is_array ($ optionsArray ['value ' ] ?? null )) {
227- return $ optionsArray;
228- }
229-
230- foreach ( array_keys ( $ optionsArray [ ' value ' ]) as $ optionId ) {
231- if ( isset ( $ optionsArray [ ' delete ' ][ $ optionId ]) && ( int ) $ optionsArray ['delete ' ][$ optionId ] === 1 ) {
232- unset( $ optionsArray [ ' value ' ][ $ optionId ]);
216+ if (isset ($ optionsArray ['value ' ]) && is_array ( $ optionsArray [ ' value ' ] )) {
217+ foreach ( array_keys ( $ optionsArray[ ' value ' ]) as $ optionId ) {
218+ if ( isset ( $ optionsArray [ ' delete ' ]) && isset ( $ optionsArray [ ' delete ' ][ $ optionId ])
219+ && $ optionsArray [ ' delete ' ][ $ optionId ] == 1
220+ ) {
221+ unset( $ optionsArray ['value ' ][$ optionId ]);
222+ }
233223 }
234224 }
235-
236225 return $ optionsArray ;
237226 }
238227
@@ -241,7 +230,6 @@ protected function prepareOptionIds(array $optionsArray)
241230 *
242231 * @param array $optionsArray
243232 * @param array $attributeSavedOptions
244- *
245233 * @return void
246234 */
247235 protected function prepareOptionLinks (array $ optionsArray , array $ attributeSavedOptions )
@@ -261,7 +249,6 @@ protected function prepareOptionLinks(array $optionsArray, array $attributeSaved
261249 * Save all Swatches data
262250 *
263251 * @param Attribute $attribute
264- *
265252 * @return void
266253 */
267254 protected function saveSwatchParams (Attribute $ attribute )
@@ -279,7 +266,6 @@ protected function saveSwatchParams(Attribute $attribute)
279266 * Save Visual Swatch data
280267 *
281268 * @param Attribute $attribute
282- *
283269 * @return void
284270 */
285271 protected function processVisualSwatch (Attribute $ attribute )
@@ -308,7 +294,7 @@ protected function processVisualSwatch(Attribute $attribute)
308294 *
309295 * @param array $attributeOptions
310296 * @param int|null $swatchType
311- * @throws LocalizedException
297+ * @throws \Magento\Framework\Exception\ LocalizedException
312298 */
313299 private function cleanEavAttributeOptionSwatchValues (array $ attributeOptions , int $ swatchType = null )
314300 {
@@ -323,7 +309,7 @@ private function cleanEavAttributeOptionSwatchValues(array $attributeOptions, in
323309 * Cleaning the text type of swatch option values after switching.
324310 *
325311 * @param array $attributeOptions
326- * @throws LocalizedException
312+ * @throws \Magento\Framework\Exception\ LocalizedException
327313 */
328314 private function cleanTextSwatchValuesAfterSwitch (array $ attributeOptions )
329315 {
@@ -334,7 +320,6 @@ private function cleanTextSwatchValuesAfterSwitch(array $attributeOptions)
334320 * Get the visual swatch type based on its value
335321 *
336322 * @param string $value
337- *
338323 * @return int
339324 */
340325 private function determineSwatchType ($ value )
@@ -345,15 +330,13 @@ private function determineSwatchType($value)
345330 } elseif (!empty ($ value ) && $ value [0 ] == '/ ' ) {
346331 $ swatchType = Swatch::SWATCH_TYPE_VISUAL_IMAGE ;
347332 }
348-
349333 return $ swatchType ;
350334 }
351335
352336 /**
353337 * Save Textual Swatch data
354338 *
355339 * @param Attribute $attribute
356- *
357340 * @return void
358341 */
359342 protected function processTextualSwatch (Attribute $ attribute )
@@ -378,7 +361,7 @@ protected function processTextualSwatch(Attribute $attribute)
378361 $ swatch ,
379362 $ optionId ,
380363 $ storeId ,
381- Swatch::SWATCH_TYPE_TEXTUAL ,
364+ \ Magento \ Swatches \ Model \ Swatch::SWATCH_TYPE_TEXTUAL ,
382365 $ value
383366 );
384367 $ this ->isSwatchExists = null ;
@@ -391,7 +374,6 @@ protected function processTextualSwatch(Attribute $attribute)
391374 * Get option id. If it not exist get it from dependency link array
392375 *
393376 * @param integer $optionId
394- *
395377 * @return int
396378 */
397379 protected function getAttributeOptionId ($ optionId )
@@ -400,7 +382,6 @@ protected function getAttributeOptionId($optionId)
400382 strpos ((string )$ optionId , self ::API_OPTION_PREFIX ) === 0 ) {
401383 $ optionId = $ this ->dependencyArray [$ optionId ] ?? null ;
402384 }
403-
404385 return $ optionId ;
405386 }
406387
@@ -409,13 +390,11 @@ protected function getAttributeOptionId($optionId)
409390 *
410391 * @param Attribute $attribute
411392 * @param integer $optionId
412- *
413393 * @return bool
414394 */
415395 protected function isOptionForDelete (Attribute $ attribute , $ optionId )
416396 {
417397 $ isOptionForDelete = $ attribute ->getData ('option/delete/ ' . $ optionId );
418-
419398 return isset ($ isOptionForDelete ) && $ isOptionForDelete ;
420399 }
421400
@@ -424,7 +403,6 @@ protected function isOptionForDelete(Attribute $attribute, $optionId)
424403 *
425404 * @param int $optionId
426405 * @param int $storeId
427- *
428406 * @return Swatch
429407 */
430408 protected function loadSwatchIfExists ($ optionId , $ storeId )
@@ -438,7 +416,6 @@ protected function loadSwatchIfExists($optionId, $storeId)
438416 if ($ loadedSwatch ->getId ()) {
439417 $ this ->isSwatchExists = true ;
440418 }
441-
442419 return $ loadedSwatch ;
443420 }
444421
@@ -450,7 +427,6 @@ protected function loadSwatchIfExists($optionId, $storeId)
450427 * @param integer $storeId
451428 * @param integer $type
452429 * @param string $value
453- *
454430 * @return void
455431 */
456432 protected function saveSwatchData ($ swatch , $ optionId , $ storeId , $ type , $ value )
@@ -471,7 +447,6 @@ protected function saveSwatchData($swatch, $optionId, $storeId, $type, $value)
471447 * Save default swatch value using Swatch model instead of Eav model
472448 *
473449 * @param Attribute $attribute
474- *
475450 * @return void
476451 */
477452 protected function saveDefaultSwatchOptionValue (Attribute $ attribute )
@@ -481,7 +456,7 @@ protected function saveDefaultSwatchOptionValue(Attribute $attribute)
481456 }
482457 $ defaultValue = $ attribute ->getData ('default/0 ' );
483458 if (!empty ($ defaultValue )) {
484- /** @var Swatch $swatch */
459+ /** @var \Magento\Swatches\Model\ Swatch $swatch */
485460 $ swatch = $ this ->swatchFactory ->create ();
486461 $ swatch ->getResource ()->saveDefaultSwatchOption (
487462 $ attribute ->getId (),
@@ -494,7 +469,6 @@ protected function saveDefaultSwatchOptionValue(Attribute $attribute)
494469 * Validate that attribute options exist
495470 *
496471 * @param Attribute $attribute
497- *
498472 * @return bool
499473 * @throws InputException
500474 */
@@ -509,7 +483,6 @@ protected function validateOptions(Attribute $attribute)
509483 if ($ options && !$ this ->isOptionsValid ($ options , $ attribute )) {
510484 throw new InputException (__ ('Admin is a required field in each row ' ));
511485 }
512-
513486 return true ;
514487 }
515488
@@ -518,26 +491,22 @@ protected function validateOptions(Attribute $attribute)
518491 *
519492 * @param array $options
520493 * @param Attribute $attribute
521- *
522494 * @return bool
523495 */
524496 protected function isOptionsValid (array $ options , Attribute $ attribute )
525497 {
526498 if (!isset ($ options ['value ' ])) {
527499 return false ;
528500 }
529-
530501 foreach ($ options ['value ' ] as $ optionId => $ option ) {
531502 // do not validate options marked as deleted
532503 if ($ this ->isOptionForDelete ($ attribute , $ optionId )) {
533504 continue ;
534505 }
535-
536506 if (!isset ($ option [0 ]) || $ option [0 ] === '' ) {
537507 return false ;
538508 }
539509 }
540-
541510 return true ;
542511 }
543512
@@ -548,15 +517,13 @@ protected function isOptionsValid(array $options, Attribute $attribute)
548517 *
549518 * @param Attribute $attribute
550519 * @param bool $result
551- *
552520 * @return bool
553521 */
554522 public function afterUsesSource (Attribute $ attribute , $ result )
555523 {
556524 if ($ this ->swatchHelper ->isSwatchAttribute ($ attribute )) {
557525 return true ;
558526 }
559-
560527 return $ result ;
561528 }
562529}
0 commit comments