| 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | final class AttributeBuilder extends AbstractBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var ConcatedNameResolverInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $attributeNameResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var StringFormatterInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $stringFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ConcatedNameResolverInterface $attributeNameResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         StringFormatterInterface $stringFormatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->attributeNameResolver = $attributeNameResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->stringFormatter = $stringFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function consumeEvent(TransformEvent $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->buildProperty($event, ProductInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             function (ProductInterface $product, Document $document): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 $this->resolveProductAttributes($product, $document); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     private function resolveProductAttributes(ProductInterface $product, Document $document): void | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         foreach ($product->getAttributes() as $attributeValue) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $attribute = $attributeValue->getAttribute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             if (!$attribute) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $attributeCode = $attribute->getCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             $index = $this->attributeNameResolver->resolvePropertyName($attributeCode); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $value = $attributeValue->getValue(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             if ($attribute->getType() === 'select') { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 $document->set($index, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $attributes = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             if (is_array($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 foreach ($value as $singleElement) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                     $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                 $value = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 $attributes[] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             $document->set($index, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |