| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file has been created by developers from BitBag. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Feel free to contact us once you face any issues or want to start | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * another great project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * You can find more information about us on https://bitbag.shop and write us | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * an email on [email protected]. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Elastica\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use FOS\ElasticaBundle\Event\TransformEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sylius\Component\Core\Model\ProductInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sylius\Component\Locale\Context\LocaleContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | final class AttributeBuilder extends AbstractBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var ConcatedNameResolverInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $attributeNameResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var StringFormatterInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $stringFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var LocaleContextInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $localeContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ConcatedNameResolverInterface $attributeNameResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         StringFormatterInterface $stringFormatter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         LocaleContextInterface $localeContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->attributeNameResolver = $attributeNameResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->stringFormatter = $stringFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->localeContext = $localeContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function consumeEvent(TransformEvent $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->buildProperty($event, ProductInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             function (ProductInterface $product, Document $document): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $this->resolveProductAttributes($product, $document); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     private function resolveProductAttributes(ProductInterface $product, Document $document): void | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         foreach ($product->getAttributes() as $attributeValue) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $attribute = $attributeValue->getAttribute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             if (!$attribute) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $attributeCode = $attribute->getCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $index = $this->attributeNameResolver->resolvePropertyName($attributeCode); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $value = $attributeValue->getValue(); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             if ($attribute->getType() === 'select') { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 $choices = $attribute->getConfiguration()['choices']; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 if (is_array($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     foreach ($value as $i => $item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                         $value[$i] = $choices[$item][$this->localeContext->getLocaleCode()] ?? $item; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                     $value = $choices[$value][$this->localeContext->getLocaleCode()] ?? $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $attributes = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             if (is_array($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 foreach ($value as $singleElement) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                     $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 $value = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 $attributes[] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             $document->set($index, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  |