Completed
Pull Request — master (#124)
by
unknown
01:38
created

ProductSearchAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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\SyliusCmsPlugin\Controller\Action\Admin;
14
15
use BitBag\SyliusCmsPlugin\Repository\ProductRepositoryInterface;
16
use FOS\RestBundle\View\View;
17
use FOS\RestBundle\View\ViewHandler;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
21
final class ProductSearchAction
22
{
23
    /**
24
     * @var ProductRepositoryInterface
25
     */
26
    private $productRepository;
27
28
    /**
29
     * @var ViewHandler
30
     */
31
    private $viewHandler;
32
33
    /**
34
     * @param ProductRepositoryInterface $productRepository
35
     * @param ViewHandler $viewHandler
36
     */
37
    public function __construct(ProductRepositoryInterface $productRepository, ViewHandler $viewHandler)
38
    {
39
        $this->productRepository = $productRepository;
40
        $this->viewHandler = $viewHandler;
41
    }
42
43
    /**
44
     * @param Request $request
45
     *
46
     * @return Response
47
     */
48
    public function __invoke(Request $request): Response
49
    {
50
        $resource = $this->productRepository->findByNamePart($request->get('phrase', ''));
51
52
        $view = View::create($resource);
53
54
        $this->viewHandler->setExclusionStrategyGroups(['Autocomplete']);
55
56
        $view->getContext()->enableMaxDepth();
57
58
        return $this->viewHandler->handle($view);
59
    }
60
}
61