12
12
use Setono \SyliusCatalogPromotionPlugin \Checker \PreQualification \PreQualificationCheckerInterface ;
13
13
use Setono \SyliusCatalogPromotionPlugin \DataProvider \ProductDataProviderInterface ;
14
14
use Setono \SyliusCatalogPromotionPlugin \Message \Command \UpdateProducts ;
15
+ use Setono \SyliusCatalogPromotionPlugin \Model \CatalogPromotionInterface ;
15
16
use Setono \SyliusCatalogPromotionPlugin \Model \CatalogPromotionUpdateInterface ;
16
17
use Setono \SyliusCatalogPromotionPlugin \Repository \CatalogPromotionRepositoryInterface ;
17
18
use Setono \SyliusCatalogPromotionPlugin \Workflow \CatalogPromotionUpdateWorkflow ;
@@ -23,9 +24,9 @@ final class UpdateProductsHandler
23
24
use ORMTrait;
24
25
25
26
/**
26
- * A cache of catalog promotions telling if the code (the key of the array) exists (the value)
27
+ * A cache of catalog promotions where the index is the code
27
28
*
28
- * @var array<string, bool >
29
+ * @var array<string, CatalogPromotionInterface|null >
29
30
*/
30
31
private array $ catalogPromotions = [];
31
32
@@ -52,19 +53,33 @@ public function __invoke(UpdateProducts $message): void
52
53
$ error = null ;
53
54
54
55
try {
55
- $ catalogPromotions = $ this ->catalogPromotionRepository ->findForProcessing ($ message ->catalogPromotions );
56
+ $ initialProcessableCatalogPromotions = $ this ->catalogPromotionRepository ->findForProcessing ($ message ->catalogPromotions );
56
57
57
58
foreach ($ this ->productDataProvider ->getProducts ($ message ->productIds ) as $ product ) {
58
- // Remove the catalog promotions
59
- // - we are processing and
60
- // - the ones that doesn't exist anymore
61
- // from the pre-qualified catalog promotions before we start the actual processing
62
- $ preQualifiedCatalogPromotions = array_filter (
63
- $ product ->getPreQualifiedCatalogPromotions (),
64
- fn (string $ code ) => !in_array ($ code , $ message ->catalogPromotions , true ) && $ this ->catalogPromotionExists ($ code ),
65
- );
66
-
67
- foreach ($ catalogPromotions as $ catalogPromotion ) {
59
+ $ processableCatalogPromotions = $ initialProcessableCatalogPromotions ;
60
+
61
+ /**
62
+ * This part of the code ensures that, while updating a specified set of catalog promotions,
63
+ * we also update the catalog promotions that are already applied to the product.
64
+ * While this behavior isn't strictly required as per the given task, it is implemented to achieve a
65
+ * higher level of data consistency.
66
+ */
67
+ if ([] !== $ message ->catalogPromotions ) {
68
+ $ filteredPreQualifiedCatalogPromotions = array_filter (
69
+ $ product ->getPreQualifiedCatalogPromotions (),
70
+ static fn (string $ code ): bool => !in_array ($ code , $ message ->catalogPromotions , true ),
71
+ );
72
+
73
+ foreach ($ filteredPreQualifiedCatalogPromotions as $ preQualifiedCatalogPromotion ) {
74
+ $ catalogPromotion = $ this ->getCatalogPromotion ($ preQualifiedCatalogPromotion );
75
+ if (null !== $ catalogPromotion ) {
76
+ $ processableCatalogPromotions [] = $ catalogPromotion ;
77
+ }
78
+ }
79
+ }
80
+
81
+ $ preQualifiedCatalogPromotions = [];
82
+ foreach ($ processableCatalogPromotions as $ catalogPromotion ) {
68
83
if ($ this ->preQualificationChecker ->isPreQualified ($ product , $ catalogPromotion )) {
69
84
$ preQualifiedCatalogPromotions [] = (string ) $ catalogPromotion ->getCode ();
70
85
}
@@ -114,6 +129,15 @@ public function __invoke(UpdateProducts $message): void
114
129
}
115
130
}
116
131
132
+ private function getCatalogPromotion (string $ code ): ?CatalogPromotionInterface
133
+ {
134
+ if (!array_key_exists ($ code , $ this ->catalogPromotions )) {
135
+ $ this ->catalogPromotions [$ code ] = $ this ->catalogPromotionRepository ->findOneForProcessing ($ code );
136
+ }
137
+
138
+ return $ this ->catalogPromotions [$ code ];
139
+ }
140
+
117
141
private function getCatalogPromotionUpdate (int $ id ): CatalogPromotionUpdateInterface
118
142
{
119
143
$ catalogPromotionUpdate = $ this ->getManager ($ this ->catalogPromotionUpdateClass )->find ($ this ->catalogPromotionUpdateClass , $ id );
@@ -122,17 +146,9 @@ private function getCatalogPromotionUpdate(int $id): CatalogPromotionUpdateInter
122
146
throw new UnrecoverableMessageHandlingException (sprintf ('Catalog promotion update with id %s not found ' , $ id ));
123
147
}
124
148
149
+ // Because the unit of work may have been cleared, we refresh the entity
125
150
$ this ->getManager ($ this ->catalogPromotionUpdateClass )->refresh ($ catalogPromotionUpdate );
126
151
127
152
return $ catalogPromotionUpdate ;
128
153
}
129
-
130
- private function catalogPromotionExists (string $ code ): bool
131
- {
132
- if (!array_key_exists ($ code , $ this ->catalogPromotions )) {
133
- $ this ->catalogPromotions [$ code ] = null !== $ this ->catalogPromotionRepository ->findOneBy (['code ' => $ code ]);
134
- }
135
-
136
- return $ this ->catalogPromotions [$ code ];
137
- }
138
154
}
0 commit comments