Completed
Push — master ( 1e5c1d...1e0ecd )
by Mikołaj
01:31 queued 11s
created

TaxonSearchAction::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 FOS\RestBundle\View\View;
16
use FOS\RestBundle\View\ViewHandler;
17
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Sylius\Component\Locale\Context\LocaleContextInterface;
21
22
final class TaxonSearchAction
23
{
24
    /** @var TaxonRepositoryInterface */
25
    private $taxonRepository;
26
27
    /** @var LocaleContextInterface */
28
    private $localeContext;
29
30
    /** @var ViewHandler */
31
    private $viewHandler;
32
33
    public function __construct(
34
        TaxonRepositoryInterface $taxonRepository,
35
        LocaleContextInterface $localeContext,
36
        ViewHandler $viewHandler
37
    )
38
    {
39
        $this->taxonRepository = $taxonRepository;
40
        $this->localeContext = $localeContext;
41
        $this->viewHandler = $viewHandler;
42
    }
43
44
    public function __invoke(Request $request): Response
45
    {
46
        $taxon = $this->taxonRepository->findByNamePart($request->get('phrase', ''), $this->localeContext->getLocaleCode());
47
        $view = View::create($taxon);
48
49
        $this->viewHandler->setExclusionStrategyGroups(['Autocomplete']);
50
        $view->getContext()->enableMaxDepth();
51
52
        return $this->viewHandler->handle($view);
53
    }
54
}
55