ShowTaxonDetailsAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __invoke() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Controller\Taxon;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandlerInterface;
9
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
10
use Sylius\ShopApiPlugin\Factory\TaxonDetailsViewFactoryInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
15
final class ShowTaxonDetailsAction
16
{
17
    /**
18
     * @var TaxonRepositoryInterface
19
     */
20
    private $taxonRepository;
21
22
    /**
23
     * @var ViewHandlerInterface
24
     */
25
    private $viewHandler;
26
27
    /**
28
     * @var TaxonDetailsViewFactoryInterface
29
     */
30
    private $taxonViewFactory;
31
32
    public function __construct(
33
        TaxonRepositoryInterface $taxonRepository,
34
        ViewHandlerInterface $viewHandler,
35
        TaxonDetailsViewFactoryInterface $taxonViewFactory
36
    ) {
37
        $this->taxonRepository = $taxonRepository;
38
        $this->viewHandler = $viewHandler;
39
        $this->taxonViewFactory = $taxonViewFactory;
40
    }
41
42
    public function __invoke(Request $request): Response
43
    {
44
        $code = $request->attributes->get('code');
45
        $locale = $request->query->get('locale');
46
47
        $taxon = $this->taxonRepository->findOneBy(['code' => $code]);
48
49
        if (null === $taxon) {
50
            throw new NotFoundHttpException(sprintf('Taxon with code %s has not been found.', $code));
51
        }
52
53
        return $this->viewHandler->handle(View::create($this->taxonViewFactory->create($taxon, $locale), Response::HTTP_OK));
54
    }
55
}
56