@@ -118,104 +118,125 @@ public function process(MapperGeneratorMetadataInterface $metadata): void
118
118
}, $ product ->getChannels ()->toArray ());
119
119
});
120
120
121
- $ metadata ->forMember ('attributes ' , function (ProductInterface $ product ): array {
122
- $ attributes = [];
123
- $ currentLocale = $ product ->getTranslation ()->getLocale ();
124
- if (null === $ currentLocale ) {
125
- return $ attributes ;
126
- }
127
- $ productAttributeDTOClass = $ this ->configuration ->getTargetClass ('product_attribute ' );
128
- foreach ($ product ->getAttributesByLocale ($ currentLocale , $ currentLocale ) as $ attributeValue ) {
129
- if (null === $ attributeValue ->getName () || null === $ attributeValue ->getValue ()) {
130
- continue ;
131
- }
132
- $ attribute = $ attributeValue ->getAttribute ();
133
- if (!$ attribute instanceof SearchableInterface || (!$ attribute ->isSearchable () && !$ attribute ->isFilterable ())) {
134
- continue ;
135
- }
136
- $ attributes [$ attributeValue ->getCode ()] = $ this ->autoMapper ->map ($ attributeValue , $ productAttributeDTOClass );
137
- }
121
+ $ metadata ->forMember ('attributes ' , [$ this , 'getAttributes ' ]);
138
122
139
- return $ attributes ;
140
- });
123
+ $ metadata ->forMember ('options ' , [$ this , 'getOptions ' ]);
141
124
142
- $ metadata ->forMember ('options ' , function (ProductInterface $ product ): array {
143
- $ options = [];
144
- $ currentLocale = $ product ->getTranslation ()->getLocale ();
145
- foreach ($ product ->getVariants () as $ variant ) {
146
- foreach ($ variant ->getOptionValues () as $ optionValue ) {
147
- if (null === $ optionValue ->getOption ()) {
148
- continue ;
149
- }
150
- if (!isset ($ options [$ optionValue ->getOptionCode ()])) {
151
- $ options [$ optionValue ->getOptionCode ()] = [
152
- 'name ' => $ optionValue ->getOption ()->getTranslation ($ currentLocale )->getName (),
153
- 'values ' => [],
154
- ];
155
- }
156
- $ isEnabled = ($ options [$ optionValue ->getOptionCode ()]['values ' ][$ optionValue ->getCode ()]['enabled ' ] ?? false )
157
- || $ variant ->isEnabled ();
158
- // A variant option is considered to be in stock if the current option is enabled and is in stock
159
- $ isInStock = ($ options [$ optionValue ->getOptionCode ()]['values ' ][$ optionValue ->getCode ()]['is_in_stock ' ] ?? false )
160
- || ($ variant ->isEnabled () && $ this ->isProductVariantInStock ($ variant ));
161
- $ options [$ optionValue ->getOptionCode ()]['values ' ][$ optionValue ->getCode ()] = [
162
- 'value ' => $ optionValue ->getTranslation ($ currentLocale )->getValue (),
163
- 'enabled ' => $ isEnabled ,
164
- 'is_in_stock ' => $ isInStock ,
165
- ];
166
- }
167
- }
125
+ $ metadata ->forMember ('variants ' , [$ this , 'getVariants ' ]);
168
126
169
- foreach ($ options as $ optionCode => $ optionValues ) {
170
- $ options [$ optionCode ]['values ' ] = array_values ($ optionValues ['values ' ]);
171
- }
127
+ $ metadata ->forMember ('prices ' , [$ this , 'getPrices ' ]);
128
+ }
172
129
173
- return $ options ;
174
- });
130
+ public function getSource (): string
131
+ {
132
+ return $ this ->configuration ->getSourceClass ('product ' );
133
+ }
175
134
176
- $ metadata ->forMember ('variants ' , function (ProductInterface $ product ): array {
177
- $ variants = [];
178
- $ productVariantDTOClass = $ this ->configuration ->getTargetClass ('product_variant ' );
179
- foreach ($ product ->getEnabledVariants () as $ variant ) {
180
- $ variants [] = $ this ->autoMapper ->map ($ variant , $ productVariantDTOClass );
181
- }
135
+ public function getTarget (): string
136
+ {
137
+ return $ this ->configuration ->getTargetClass ('product ' );
138
+ }
182
139
183
- return $ variants ;
184
- });
140
+ /**
141
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
142
+ */
143
+ public function getAttributes (ProductInterface $ product ): array
144
+ {
145
+ $ attributes = [];
146
+ $ currentLocale = $ product ->getTranslation ()->getLocale ();
147
+ if (null === $ currentLocale ) {
148
+ return $ attributes ;
149
+ }
150
+ $ productAttributeDTOClass = $ this ->configuration ->getTargetClass ('product_attribute ' );
151
+ foreach ($ product ->getAttributesByLocale ($ currentLocale , $ currentLocale ) as $ attributeValue ) {
152
+ if (null === $ attributeValue ->getName () || null === $ attributeValue ->getValue ()) {
153
+ continue ;
154
+ }
155
+ $ attribute = $ attributeValue ->getAttribute ();
156
+ if (!$ attribute instanceof SearchableInterface || (!$ attribute ->isSearchable () && !$ attribute ->isFilterable ())) {
157
+ continue ;
158
+ }
159
+ $ attributes [$ attributeValue ->getCode ()] = $ this ->autoMapper ->map ($ attributeValue , $ productAttributeDTOClass );
160
+ }
185
161
186
- $ metadata ->forMember ('prices ' , function (ProductInterface $ product ): array {
187
- $ prices = [];
188
- foreach ($ product ->getChannels () as $ channel ) {
189
- /** @var ChannelInterface $channel */
190
- $ this ->channelSimulationContext ->setChannel ($ channel );
191
- if (
192
- null === ($ variant = $ this ->productVariantResolver ->getVariant ($ product ))
193
- || !$ variant instanceof ModelProductVariantInterface
194
- || null === ($ channelPricing = $ variant ->getChannelPricingForChannel ($ channel ))
195
- ) {
196
- $ this ->channelSimulationContext ->setChannel (null );
162
+ return $ attributes ;
163
+ }
197
164
165
+ /**
166
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
167
+ */
168
+ public function getOptions (ProductInterface $ product ): array
169
+ {
170
+ $ options = [];
171
+ $ currentLocale = $ product ->getTranslation ()->getLocale ();
172
+ foreach ($ product ->getVariants () as $ variant ) {
173
+ foreach ($ variant ->getOptionValues () as $ optionValue ) {
174
+ if (null === $ optionValue ->getOption ()) {
198
175
continue ;
199
176
}
200
- $ this ->channelSimulationContext ->setChannel (null );
201
- $ prices [] = $ this ->autoMapper ->map (
202
- $ channelPricing ,
203
- $ this ->configuration ->getTargetClass ('pricing ' )
204
- );
177
+ if (!isset ($ options [$ optionValue ->getOptionCode ()])) {
178
+ $ options [$ optionValue ->getOptionCode ()] = [
179
+ 'name ' => $ optionValue ->getOption ()->getTranslation ($ currentLocale )->getName (),
180
+ 'values ' => [],
181
+ ];
182
+ }
183
+ $ isEnabled = ($ options [$ optionValue ->getOptionCode ()]['values ' ][$ optionValue ->getCode ()]['enabled ' ] ?? false )
184
+ || $ variant ->isEnabled ();
185
+ // A variant option is considered to be in stock if the current option is enabled and is in stock
186
+ $ isInStock = ($ options [$ optionValue ->getOptionCode ()]['values ' ][$ optionValue ->getCode ()]['is_in_stock ' ] ?? false )
187
+ || ($ variant ->isEnabled () && $ this ->isProductVariantInStock ($ variant ));
188
+ $ options [$ optionValue ->getOptionCode ()]['values ' ][$ optionValue ->getCode ()] = [
189
+ 'value ' => $ optionValue ->getTranslation ($ currentLocale )->getValue (),
190
+ 'enabled ' => $ isEnabled ,
191
+ 'is_in_stock ' => $ isInStock ,
192
+ ];
205
193
}
194
+ }
206
195
207
- return $ prices ;
208
- });
196
+ foreach ($ options as $ optionCode => $ optionValues ) {
197
+ $ options [$ optionCode ]['values ' ] = array_values ($ optionValues ['values ' ]);
198
+ }
199
+
200
+ return $ options ;
209
201
}
210
202
211
- public function getSource ( ): string
203
+ public function getVariants ( ProductInterface $ product ): array
212
204
{
213
- return $ this ->configuration ->getSourceClass ('product ' );
205
+ $ variants = [];
206
+ $ productVariantDTOClass = $ this ->configuration ->getTargetClass ('product_variant ' );
207
+ foreach ($ product ->getEnabledVariants () as $ variant ) {
208
+ $ variants [] = $ this ->autoMapper ->map ($ variant , $ productVariantDTOClass );
209
+ }
210
+
211
+ return $ variants ;
214
212
}
215
213
216
- public function getTarget (): string
214
+ /**
215
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
216
+ */
217
+ public function getPrices (ProductInterface $ product ): array
217
218
{
218
- return $ this ->configuration ->getTargetClass ('product ' );
219
+ $ prices = [];
220
+ foreach ($ product ->getChannels () as $ channel ) {
221
+ /** @var ChannelInterface $channel */
222
+ $ this ->channelSimulationContext ->setChannel ($ channel );
223
+ if (
224
+ null === ($ variant = $ this ->productVariantResolver ->getVariant ($ product ))
225
+ || !$ variant instanceof ModelProductVariantInterface
226
+ || null === ($ channelPricing = $ variant ->getChannelPricingForChannel ($ channel ))
227
+ ) {
228
+ $ this ->channelSimulationContext ->setChannel (null );
229
+
230
+ continue ;
231
+ }
232
+ $ this ->channelSimulationContext ->setChannel (null );
233
+ $ prices [] = $ this ->autoMapper ->map (
234
+ $ channelPricing ,
235
+ $ this ->configuration ->getTargetClass ('pricing ' )
236
+ );
237
+ }
238
+
239
+ return $ prices ;
219
240
}
220
241
221
242
private function isProductVariantInStock (ProductVariantInterface $ productVariant ): bool
0 commit comments