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\Taxonomy\Repository\TaxonRepositoryInterface; |
8
|
|
|
use Sylius\ShopApiPlugin\Factory\TaxonDetailsViewFactoryInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
12
|
|
|
|
13
|
|
|
final class ShowTaxonDetailsAction |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var TaxonRepositoryInterface |
17
|
|
|
*/ |
18
|
|
|
private $taxonRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ViewHandlerInterface |
22
|
|
|
*/ |
23
|
|
|
private $viewHandler; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TaxonDetailsViewFactoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $taxonViewFactory; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
TaxonRepositoryInterface $taxonRepository, |
32
|
|
|
ViewHandlerInterface $viewHandler, |
33
|
|
|
TaxonDetailsViewFactoryInterface $taxonViewFactory |
34
|
|
|
) { |
35
|
|
|
$this->taxonRepository = $taxonRepository; |
36
|
|
|
$this->viewHandler = $viewHandler; |
37
|
|
|
$this->taxonViewFactory = $taxonViewFactory; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __invoke(Request $request): Response |
41
|
|
|
{ |
42
|
|
|
$taxonSlug = $request->attributes->get('slug'); |
43
|
|
|
$locale = $request->query->get('locale'); |
44
|
|
|
|
45
|
|
|
$taxon = $this->taxonRepository->findOneBySlug($taxonSlug, $locale); |
46
|
|
|
|
47
|
|
|
if (null === $taxon) { |
48
|
|
|
throw new NotFoundHttpException(sprintf('Taxon with slug %s has not been found in %s locale.', $taxonSlug, $locale)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->viewHandler->handle(View::create($this->taxonViewFactory->create($taxon, $locale), Response::HTTP_OK)); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
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.