Completed
Push — master ( aec3f7...9dc84f )
by Łukasz
14s
created

ShowTaxonDetailsAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
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
        $code = $request->attributes->get('code');
43
        $locale = $request->query->get('locale');
44
45
        $taxon = $this->taxonRepository->findOneBy(['code' => $code]);
46
47
        if (null === $taxon) {
48
            throw new NotFoundHttpException(sprintf('Taxon with code %s has not been found.', $code));
49
        }
50
51
        return $this->viewHandler->handle(View::create($this->taxonViewFactory->create($taxon, $locale), Response::HTTP_OK));
52
    }
53
}
54