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
|
|
|
/** |
17
|
|
|
* @var TaxonRepositoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $taxonRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ViewHandlerInterface |
23
|
|
|
*/ |
24
|
|
|
private $viewHandler; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TaxonViewFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $taxonViewFactory; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
TaxonRepositoryInterface $taxonRepository, |
33
|
|
|
ViewHandlerInterface $viewHandler, |
34
|
|
|
TaxonViewFactoryInterface $taxonViewFactory |
35
|
|
|
) { |
36
|
|
|
$this->taxonRepository = $taxonRepository; |
37
|
|
|
$this->viewHandler = $viewHandler; |
38
|
|
|
$this->taxonViewFactory = $taxonViewFactory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Request $request |
43
|
|
|
* |
44
|
|
|
* @return Response |
45
|
|
|
*/ |
46
|
|
|
public function __invoke(Request $request): Response |
47
|
|
|
{ |
48
|
|
|
$locale = $request->query->get('locale'); |
49
|
|
|
|
50
|
|
|
$taxons = $this->taxonRepository->findRootNodes(); |
51
|
|
|
$taxonViews = []; |
52
|
|
|
|
53
|
|
|
/** @var TaxonInterface $taxon */ |
54
|
|
|
foreach ($taxons as $taxon) { |
55
|
|
|
$taxonViews[] = $this->buildTaxonView($taxon, $locale); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->viewHandler->handle(View::create($taxonViews, Response::HTTP_OK)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
private function buildTaxonView(TaxonInterface $taxon, $locale): TaxonView |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$taxonView = $this->taxonViewFactory->create($taxon, $locale); |
64
|
|
|
|
65
|
|
|
foreach ($taxon->getChildren() as $childTaxon) { |
66
|
|
|
$taxonView->children[] = $this->buildTaxonView($childTaxon, $locale); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $taxonView; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.