Completed
Pull Request — master (#290)
by
unknown
41:51
created

ShowTaxonTreeAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Controller\Taxon;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandlerInterface;
9
use Sylius\Component\Core\Model\TaxonInterface;
10
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
11
use Sylius\ShopApiPlugin\Factory\TaxonViewFactoryInterface;
12
use Sylius\ShopApiPlugin\View\TaxonView;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
final class ShowTaxonTreeAction
17
{
18
    /** @var TaxonRepositoryInterface */
19
    private $taxonRepository;
20
21
    /** @var ViewHandlerInterface */
22
    private $viewHandler;
23
24
    /** @var TaxonViewFactoryInterface */
25
    private $taxonViewFactory;
26
27
    /** @var string */
28
    private $fallbackLocale;
29
30
    public function __construct(
31
        TaxonRepositoryInterface $taxonRepository,
32
        ViewHandlerInterface $viewHandler,
33
        TaxonViewFactoryInterface $taxonViewFactory,
34
        string $fallbackLocale
35
    ) {
36
        $this->taxonRepository = $taxonRepository;
37
        $this->viewHandler = $viewHandler;
38
        $this->taxonViewFactory = $taxonViewFactory;
39
        $this->fallbackLocale = $fallbackLocale;
40
    }
41
42
    public function __invoke(Request $request): Response
43
    {
44
        $locale = $request->query->get('locale', $this->fallbackLocale);
45
46
        $taxons = $this->taxonRepository->findRootNodes();
47
        $taxonViews = [];
48
49
        /** @var TaxonInterface $taxon */
50
        foreach ($taxons as $taxon) {
51
            $taxonViews[] = $this->buildTaxonView($taxon, $locale);
52
        }
53
54
        return $this->viewHandler->handle(View::create($taxonViews, Response::HTTP_OK));
55
    }
56
57 View Code Duplication
    private function buildTaxonView(TaxonInterface $taxon, $locale): TaxonView
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $taxonView = $this->taxonViewFactory->create($taxon, $locale);
60
61
        foreach ($taxon->getChildren() as $childTaxon) {
62
            $taxonView->children[] = $this->buildTaxonView($childTaxon, $locale);
63
        }
64
65
        return $taxonView;
66
    }
67
}
68