ProductSearchAction   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 30
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 9 1
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Controller\Action\Admin;
12
13
use FOS\RestBundle\View\View;
14
use FOS\RestBundle\View\ViewHandler;
15
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
16
use Sylius\Component\Locale\Context\LocaleContextInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
final class ProductSearchAction
21
{
22
    /** @var ProductRepositoryInterface */
23
    private $productRepository;
24
25
    /** @var LocaleContextInterface */
26
    private $localeContext;
27
28
    /** @var ViewHandler */
29
    private $viewHandler;
30
31
    public function __construct(
32
        ProductRepositoryInterface $productRepository,
33
        LocaleContextInterface $localeContext,
34
        ViewHandler $viewHandler
35
    ) {
36
        $this->productRepository = $productRepository;
37
        $this->localeContext = $localeContext;
38
        $this->viewHandler = $viewHandler;
39
    }
40
41
    public function __invoke(Request $request): Response
42
    {
43
        $product = $this->productRepository->findByNamePart($request->get('phrase', ''), $this->localeContext->getLocaleCode());
44
        $view = View::create($product);
45
46
        $this->viewHandler->setExclusionStrategyGroups(['Autocomplete']);
47
        $view->getContext()->enableMaxDepth();
48
49
        return $this->viewHandler->handle($view);
50
    }
51
}
52