| 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\Controller\Action\Api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BitBag\SyliusElasticsearchPlugin\Controller\Response\DTO\Item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use BitBag\SyliusElasticsearchPlugin\Controller\Response\ItemsResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use BitBag\SyliusElasticsearchPlugin\Finder\NamedProductsFinderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use BitBag\SyliusElasticsearchPlugin\Transformer\Product\TransformerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sylius\Component\Core\Model\ProductInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\HttpFoundation\JsonResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class ListProductsByPartialNameAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var NamedProductsFinderInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $namedProductsFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var TransformerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $productSlugTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var TransformerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $productChannelPriceTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** @var TransformerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $productImageTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         NamedProductsFinderInterface $namedProductsFinder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         TransformerInterface $productSlugResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         TransformerInterface $productChannelPriceResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         TransformerInterface $productImageResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->namedProductsFinder = $namedProductsFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->productSlugTransformer = $productSlugResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->productChannelPriceTransformer = $productChannelPriceResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->productImageTransformer = $productImageResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function __invoke(Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $itemsResponse = ItemsResponse::createEmpty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if (null === $request->query->get('query')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return JsonResponse::create($itemsResponse->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $products = $this->namedProductsFinder->findByNamePart($request->query->get('query')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         /** @var ProductInterface $product */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         foreach ($products as $product) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             if (null === $productMainTaxon = $product->getMainTaxon()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $itemsResponse->addItem(new Item( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $productMainTaxon->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $product->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $product->getShortDescription(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $this->productSlugTransformer->transform($product), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $this->productChannelPriceTransformer->transform($product), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $this->productImageTransformer->transform($product) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return JsonResponse::create($itemsResponse->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  |