1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Controller\Taxon; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\View\View; |
6
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
7
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
8
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
9
|
|
|
use Sylius\ShopApiPlugin\Factory\TaxonViewFactoryInterface; |
10
|
|
|
use Sylius\ShopApiPlugin\View\TaxonView; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
|
14
|
|
|
final class ShowTaxonTreeAction |
15
|
|
|
{ |
16
|
|
|
/** @var TaxonRepositoryInterface */ |
17
|
|
|
private $taxonRepository; |
18
|
|
|
|
19
|
|
|
/** @var ViewHandlerInterface */ |
20
|
|
|
private $viewHandler; |
21
|
|
|
|
22
|
|
|
/** @var TaxonViewFactoryInterface */ |
23
|
|
|
private $taxonViewFactory; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $fallbackLocale; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
TaxonRepositoryInterface $taxonRepository, |
30
|
|
|
ViewHandlerInterface $viewHandler, |
31
|
|
|
TaxonViewFactoryInterface $taxonViewFactory, |
32
|
|
|
string $fallbackLocale |
33
|
|
|
) { |
34
|
|
|
$this->taxonRepository = $taxonRepository; |
35
|
|
|
$this->viewHandler = $viewHandler; |
36
|
|
|
$this->taxonViewFactory = $taxonViewFactory; |
37
|
|
|
$this->fallbackLocale = $fallbackLocale; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __invoke(Request $request): Response |
41
|
|
|
{ |
42
|
|
|
$locale = $request->query->get('locale', $this->fallbackLocale); |
43
|
|
|
|
44
|
|
|
$taxons = $this->taxonRepository->findRootNodes(); |
45
|
|
|
$taxonViews = []; |
46
|
|
|
|
47
|
|
|
/** @var TaxonInterface $taxon */ |
48
|
|
|
foreach ($taxons as $taxon) { |
49
|
|
|
$taxonViews[] = $this->buildTaxonView($taxon, $locale); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->viewHandler->handle(View::create($taxonViews, Response::HTTP_OK)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
private function buildTaxonView(TaxonInterface $taxon, $locale): TaxonView |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$taxonView = $this->taxonViewFactory->create($taxon, $locale); |
58
|
|
|
|
59
|
|
|
foreach ($taxon->getChildren() as $childTaxon) { |
60
|
|
|
$taxonView->children[] = $this->buildTaxonView($childTaxon, $locale); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $taxonView; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.