| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Odiseo\SyliusReportPlugin\Controller\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use FOS\RestBundle\View\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use FOS\RestBundle\View\ViewHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use FOS\RestBundle\View\ViewHandlerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Sylius\Component\Core\Model\ProductInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Sylius\Component\Core\Repository\ProductRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Sylius\Component\Locale\Context\LocaleContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | final class ProductSearchAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var ProductRepositoryInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $productRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var LocaleContextInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $localeContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var ViewHandlerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $viewHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         ProductRepositoryInterface $productRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         LocaleContextInterface $localeContext, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ViewHandler $viewHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->productRepository = $productRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->localeContext = $localeContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->viewHandler = $viewHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function __invoke(Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $locale = $this->localeContext->getLocaleCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $products = $this->getProducts($request->get('name', ''), $locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $view = View::create($products); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->viewHandler->setExclusionStrategyGroups(['Autocomplete']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $view->getContext()->enableMaxDepth(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return $this->viewHandler->handle($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     private function getProducts($query, $locale): array | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $products = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $searchProducts = $this->productRepository->findByNamePart($query, $locale); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         /** @var ProductInterface $product */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($searchProducts as $product) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $productLabel = ucfirst(strtolower($product->getName())); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $isNew = count(array_filter($products, function ($product) use ($productLabel) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 return $product['name'] === $productLabel; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             })) === 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             if ($isNew) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 $products[] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                     'name' => $productLabel, | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                     'id' => $product->getId(), | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         return $products; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |