3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
- declare (strict_types=1 );
7
-
8
6
namespace Magento \Swatches \Model \Plugin ;
9
7
10
8
use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
11
9
use Magento \Framework \App \ObjectManager ;
12
10
use Magento \Framework \Exception \InputException ;
13
- use Magento \Framework \Exception \LocalizedException ;
14
11
use Magento \Framework \Serialize \Serializer \Json ;
15
- use Magento \Swatches \Helper \Data ;
16
12
use Magento \Swatches \Model \ResourceModel \Swatch as SwatchResource ;
17
- use Magento \Swatches \Model \ResourceModel \Swatch \CollectionFactory ;
18
13
use Magento \Swatches \Model \Swatch ;
19
- use Magento \Swatches \Model \SwatchFactory ;
20
14
21
15
/**
22
16
* Plugin model for Catalog Resource Attribute
@@ -27,6 +21,11 @@ class EavAttribute
27
21
{
28
22
const DEFAULT_STORE_ID = 0 ;
29
23
24
+ /**
25
+ * @var SwatchResource
26
+ */
27
+ private $ swatchResource ;
28
+
30
29
/**
31
30
* Base option title used for string operations to detect is option already exists or new
32
31
*/
@@ -38,30 +37,20 @@ class EavAttribute
38
37
private const API_OPTION_PREFIX = 'id_ ' ;
39
38
40
39
/**
41
- * @var CollectionFactory
40
+ * @var \Magento\Swatches\Model\ResourceModel\Swatch\ CollectionFactory
42
41
*/
43
42
protected $ swatchCollectionFactory ;
44
43
45
44
/**
46
- * @var SwatchFactory
45
+ * @var \Magento\Swatches\Model\ SwatchFactory
47
46
*/
48
47
protected $ swatchFactory ;
49
48
50
49
/**
51
- * @var Data
50
+ * @var \Magento\Swatches\Helper\ Data
52
51
*/
53
52
protected $ swatchHelper ;
54
53
55
- /**
56
- * @var Json
57
- */
58
- private $ serializer ;
59
-
60
- /**
61
- * @var SwatchResource
62
- */
63
- private $ swatchResource ;
64
-
65
54
/**
66
55
* Array which contains links for new created attributes for swatches
67
56
*
@@ -77,16 +66,23 @@ class EavAttribute
77
66
protected $ isSwatchExists ;
78
67
79
68
/**
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
83
79
* @param Json|null $serializer
84
80
* @param SwatchResource|null $swatchResource
85
81
*/
86
82
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 ,
90
86
Json $ serializer = null ,
91
87
SwatchResource $ swatchResource = null
92
88
) {
@@ -101,7 +97,6 @@ public function __construct(
101
97
* Set base data to Attribute
102
98
*
103
99
* @param Attribute $attribute
104
- *
105
100
* @return void
106
101
*/
107
102
public function beforeBeforeSave (Attribute $ attribute )
@@ -118,9 +113,8 @@ public function beforeBeforeSave(Attribute $attribute)
118
113
* Swatch save operations
119
114
*
120
115
* @param Attribute $attribute
121
- *
116
+ * @throws \Magento\Framework\Exception\LocalizedException
122
117
* @return void
123
- * @throws LocalizedException
124
118
*/
125
119
public function afterAfterSave (Attribute $ attribute )
126
120
{
@@ -135,7 +129,6 @@ public function afterAfterSave(Attribute $attribute)
135
129
* Substitute suitable options and swatches arrays
136
130
*
137
131
* @param Attribute $attribute
138
- *
139
132
* @return void
140
133
*/
141
134
protected function setProperOptionsArray (Attribute $ attribute )
@@ -171,9 +164,8 @@ protected function setProperOptionsArray(Attribute $attribute)
171
164
* Prepare attribute for conversion from any swatch type to dropdown
172
165
*
173
166
* @param Attribute $attribute
174
- *
167
+ * @throws \Magento\Framework\Exception\LocalizedException
175
168
* @return void
176
- * @throws LocalizedException
177
169
*/
178
170
protected function convertSwatchToDropdown (Attribute $ attribute )
179
171
{
@@ -195,9 +187,8 @@ protected function convertSwatchToDropdown(Attribute $attribute)
195
187
* Creates array which link new option ids
196
188
*
197
189
* @param Attribute $attribute
198
- *
199
190
* @return Attribute
200
- * @throws LocalizedException
191
+ * @throws \Magento\Framework\Exception\ LocalizedException
201
192
*/
202
193
protected function processSwatchOptions (Attribute $ attribute )
203
194
{
@@ -218,21 +209,19 @@ protected function processSwatchOptions(Attribute $attribute)
218
209
* Get options array without deleted items
219
210
*
220
211
* @param array $optionsArray
221
- *
222
212
* @return array
223
213
*/
224
214
protected function prepareOptionIds (array $ optionsArray )
225
215
{
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
+ }
233
223
}
234
224
}
235
-
236
225
return $ optionsArray ;
237
226
}
238
227
@@ -241,7 +230,6 @@ protected function prepareOptionIds(array $optionsArray)
241
230
*
242
231
* @param array $optionsArray
243
232
* @param array $attributeSavedOptions
244
- *
245
233
* @return void
246
234
*/
247
235
protected function prepareOptionLinks (array $ optionsArray , array $ attributeSavedOptions )
@@ -261,7 +249,6 @@ protected function prepareOptionLinks(array $optionsArray, array $attributeSaved
261
249
* Save all Swatches data
262
250
*
263
251
* @param Attribute $attribute
264
- *
265
252
* @return void
266
253
*/
267
254
protected function saveSwatchParams (Attribute $ attribute )
@@ -279,7 +266,6 @@ protected function saveSwatchParams(Attribute $attribute)
279
266
* Save Visual Swatch data
280
267
*
281
268
* @param Attribute $attribute
282
- *
283
269
* @return void
284
270
*/
285
271
protected function processVisualSwatch (Attribute $ attribute )
@@ -308,7 +294,7 @@ protected function processVisualSwatch(Attribute $attribute)
308
294
*
309
295
* @param array $attributeOptions
310
296
* @param int|null $swatchType
311
- * @throws LocalizedException
297
+ * @throws \Magento\Framework\Exception\ LocalizedException
312
298
*/
313
299
private function cleanEavAttributeOptionSwatchValues (array $ attributeOptions , int $ swatchType = null )
314
300
{
@@ -323,7 +309,7 @@ private function cleanEavAttributeOptionSwatchValues(array $attributeOptions, in
323
309
* Cleaning the text type of swatch option values after switching.
324
310
*
325
311
* @param array $attributeOptions
326
- * @throws LocalizedException
312
+ * @throws \Magento\Framework\Exception\ LocalizedException
327
313
*/
328
314
private function cleanTextSwatchValuesAfterSwitch (array $ attributeOptions )
329
315
{
@@ -334,7 +320,6 @@ private function cleanTextSwatchValuesAfterSwitch(array $attributeOptions)
334
320
* Get the visual swatch type based on its value
335
321
*
336
322
* @param string $value
337
- *
338
323
* @return int
339
324
*/
340
325
private function determineSwatchType ($ value )
@@ -345,15 +330,13 @@ private function determineSwatchType($value)
345
330
} elseif (!empty ($ value ) && $ value [0 ] == '/ ' ) {
346
331
$ swatchType = Swatch::SWATCH_TYPE_VISUAL_IMAGE ;
347
332
}
348
-
349
333
return $ swatchType ;
350
334
}
351
335
352
336
/**
353
337
* Save Textual Swatch data
354
338
*
355
339
* @param Attribute $attribute
356
- *
357
340
* @return void
358
341
*/
359
342
protected function processTextualSwatch (Attribute $ attribute )
@@ -378,7 +361,7 @@ protected function processTextualSwatch(Attribute $attribute)
378
361
$ swatch ,
379
362
$ optionId ,
380
363
$ storeId ,
381
- Swatch::SWATCH_TYPE_TEXTUAL ,
364
+ \ Magento \ Swatches \ Model \ Swatch::SWATCH_TYPE_TEXTUAL ,
382
365
$ value
383
366
);
384
367
$ this ->isSwatchExists = null ;
@@ -391,7 +374,6 @@ protected function processTextualSwatch(Attribute $attribute)
391
374
* Get option id. If it not exist get it from dependency link array
392
375
*
393
376
* @param integer $optionId
394
- *
395
377
* @return int
396
378
*/
397
379
protected function getAttributeOptionId ($ optionId )
@@ -400,7 +382,6 @@ protected function getAttributeOptionId($optionId)
400
382
strpos ((string )$ optionId , self ::API_OPTION_PREFIX ) === 0 ) {
401
383
$ optionId = $ this ->dependencyArray [$ optionId ] ?? null ;
402
384
}
403
-
404
385
return $ optionId ;
405
386
}
406
387
@@ -409,13 +390,11 @@ protected function getAttributeOptionId($optionId)
409
390
*
410
391
* @param Attribute $attribute
411
392
* @param integer $optionId
412
- *
413
393
* @return bool
414
394
*/
415
395
protected function isOptionForDelete (Attribute $ attribute , $ optionId )
416
396
{
417
397
$ isOptionForDelete = $ attribute ->getData ('option/delete/ ' . $ optionId );
418
-
419
398
return isset ($ isOptionForDelete ) && $ isOptionForDelete ;
420
399
}
421
400
@@ -424,7 +403,6 @@ protected function isOptionForDelete(Attribute $attribute, $optionId)
424
403
*
425
404
* @param int $optionId
426
405
* @param int $storeId
427
- *
428
406
* @return Swatch
429
407
*/
430
408
protected function loadSwatchIfExists ($ optionId , $ storeId )
@@ -438,7 +416,6 @@ protected function loadSwatchIfExists($optionId, $storeId)
438
416
if ($ loadedSwatch ->getId ()) {
439
417
$ this ->isSwatchExists = true ;
440
418
}
441
-
442
419
return $ loadedSwatch ;
443
420
}
444
421
@@ -450,7 +427,6 @@ protected function loadSwatchIfExists($optionId, $storeId)
450
427
* @param integer $storeId
451
428
* @param integer $type
452
429
* @param string $value
453
- *
454
430
* @return void
455
431
*/
456
432
protected function saveSwatchData ($ swatch , $ optionId , $ storeId , $ type , $ value )
@@ -471,7 +447,6 @@ protected function saveSwatchData($swatch, $optionId, $storeId, $type, $value)
471
447
* Save default swatch value using Swatch model instead of Eav model
472
448
*
473
449
* @param Attribute $attribute
474
- *
475
450
* @return void
476
451
*/
477
452
protected function saveDefaultSwatchOptionValue (Attribute $ attribute )
@@ -481,7 +456,7 @@ protected function saveDefaultSwatchOptionValue(Attribute $attribute)
481
456
}
482
457
$ defaultValue = $ attribute ->getData ('default/0 ' );
483
458
if (!empty ($ defaultValue )) {
484
- /** @var Swatch $swatch */
459
+ /** @var \Magento\Swatches\Model\ Swatch $swatch */
485
460
$ swatch = $ this ->swatchFactory ->create ();
486
461
$ swatch ->getResource ()->saveDefaultSwatchOption (
487
462
$ attribute ->getId (),
@@ -494,7 +469,6 @@ protected function saveDefaultSwatchOptionValue(Attribute $attribute)
494
469
* Validate that attribute options exist
495
470
*
496
471
* @param Attribute $attribute
497
- *
498
472
* @return bool
499
473
* @throws InputException
500
474
*/
@@ -509,7 +483,6 @@ protected function validateOptions(Attribute $attribute)
509
483
if ($ options && !$ this ->isOptionsValid ($ options , $ attribute )) {
510
484
throw new InputException (__ ('Admin is a required field in each row ' ));
511
485
}
512
-
513
486
return true ;
514
487
}
515
488
@@ -518,26 +491,22 @@ protected function validateOptions(Attribute $attribute)
518
491
*
519
492
* @param array $options
520
493
* @param Attribute $attribute
521
- *
522
494
* @return bool
523
495
*/
524
496
protected function isOptionsValid (array $ options , Attribute $ attribute )
525
497
{
526
498
if (!isset ($ options ['value ' ])) {
527
499
return false ;
528
500
}
529
-
530
501
foreach ($ options ['value ' ] as $ optionId => $ option ) {
531
502
// do not validate options marked as deleted
532
503
if ($ this ->isOptionForDelete ($ attribute , $ optionId )) {
533
504
continue ;
534
505
}
535
-
536
506
if (!isset ($ option [0 ]) || $ option [0 ] === '' ) {
537
507
return false ;
538
508
}
539
509
}
540
-
541
510
return true ;
542
511
}
543
512
@@ -548,15 +517,13 @@ protected function isOptionsValid(array $options, Attribute $attribute)
548
517
*
549
518
* @param Attribute $attribute
550
519
* @param bool $result
551
- *
552
520
* @return bool
553
521
*/
554
522
public function afterUsesSource (Attribute $ attribute , $ result )
555
523
{
556
524
if ($ this ->swatchHelper ->isSwatchAttribute ($ attribute )) {
557
525
return true ;
558
526
}
559
-
560
527
return $ result ;
561
528
}
562
529
}
0 commit comments