1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Controller; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\View\View; |
6
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
7
|
|
|
use Sylius\Component\Core\Model\ImageInterface; |
8
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
9
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
10
|
|
|
use Sylius\ShopApiPlugin\Factory\ImageViewFactoryInterface; |
11
|
|
|
use Sylius\ShopApiPlugin\Factory\TaxonViewFactoryInterface; |
12
|
|
|
use Sylius\ShopApiPlugin\View\TaxonView; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
17
|
|
|
|
18
|
|
|
final class TaxonController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var TaxonRepositoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $taxonRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ViewHandlerInterface |
27
|
|
|
*/ |
28
|
|
|
private $viewHandler; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var TaxonViewFactoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $taxonViewFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param TaxonRepositoryInterface $taxonRepository |
37
|
|
|
* @param ViewHandlerInterface $viewHandler |
38
|
|
|
* @param TaxonViewFactoryInterface $taxonViewFactory |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
TaxonRepositoryInterface $taxonRepository, |
42
|
|
|
ViewHandlerInterface $viewHandler, |
43
|
|
|
TaxonViewFactoryInterface $taxonViewFactory |
44
|
|
|
) { |
45
|
|
|
$this->taxonRepository = $taxonRepository; |
46
|
|
|
$this->viewHandler = $viewHandler; |
47
|
|
|
$this->taxonViewFactory = $taxonViewFactory; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Request $request |
52
|
|
|
* |
53
|
|
|
* @return Response |
54
|
|
|
*/ |
55
|
|
|
public function showDetailsAction(Request $request) |
56
|
|
|
{ |
57
|
|
|
$taxonSlug = $request->attributes->get('slug'); |
58
|
|
|
$locale = $request->query->get('locale'); |
59
|
|
|
|
60
|
|
|
$taxon = $this->taxonRepository->findOneBySlug($taxonSlug, $locale); |
61
|
|
|
|
62
|
|
|
if (null === $taxon) { |
63
|
|
|
throw new NotFoundHttpException(sprintf('Taxon with slug %s has not been found in %s locale.', $taxonSlug, $locale)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->viewHandler->handle(View::create($this->buildTaxonView($taxon, $locale), Response::HTTP_OK)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
/** |
69
|
|
|
* @param Request $request |
70
|
|
|
* |
71
|
|
|
* @return Response |
72
|
|
|
*/ |
73
|
|
|
public function showTreeAction(Request $request) |
74
|
|
|
{ |
75
|
|
|
$locale = $request->query->get('locale'); |
76
|
|
|
|
77
|
|
|
$taxons = $this->taxonRepository->findRootNodes(); |
78
|
|
|
$taxonViews = []; |
79
|
|
|
|
80
|
|
|
/** @var TaxonInterface $taxon */ |
81
|
|
|
foreach ($taxons as $taxon) { |
82
|
|
|
$taxonViews[] = $this->buildTaxonView($taxon, $locale); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->viewHandler->handle(View::create($taxonViews, Response::HTTP_OK)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param TaxonInterface $taxon |
90
|
|
|
* @param string $locale |
91
|
|
|
* |
92
|
|
|
* @return TaxonView |
93
|
|
|
*/ |
94
|
|
|
private function buildTaxonView(TaxonInterface $taxon, $locale) |
95
|
|
|
{ |
96
|
|
|
$taxonView = $this->taxonViewFactory->create($taxon, $locale); |
97
|
|
|
|
98
|
|
|
foreach ($taxon->getChildren() as $childTaxon) { |
99
|
|
|
$taxonView->children[] = $this->buildTaxonView($childTaxon, $locale); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $taxonView; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.