ShowProductCatalogByTaxonCodeAction::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Controller\Product;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandlerInterface;
9
use Sylius\ShopApiPlugin\Model\PaginatorDetails;
10
use Sylius\ShopApiPlugin\ViewRepository\ProductCatalogViewRepositoryInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
15 View Code Duplication
final class ShowProductCatalogByTaxonCodeAction
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
    /** @var ViewHandlerInterface */
18
    private $viewHandler;
19
20
    /** @var ProductCatalogViewRepositoryInterface */
21
    private $productCatalogQuery;
22
23
    public function __construct(
24
        ViewHandlerInterface $viewHandler,
25
        ProductCatalogViewRepositoryInterface $productCatalogQuery
26
    ) {
27
        $this->viewHandler = $viewHandler;
28
        $this->productCatalogQuery = $productCatalogQuery;
29
    }
30
31
    public function __invoke(Request $request): Response
32
    {
33
        if (!$request->query->has('channel')) {
34
            throw new NotFoundHttpException('Cannot find product without channel provided');
35
        }
36
37
        try {
38
            return $this->viewHandler->handle(View::create($this->productCatalogQuery->findByTaxonCode(
39
                $request->attributes->get('code'),
40
                $request->query->get('channel'),
41
                new PaginatorDetails($request->attributes->get('_route'), $request->query->all()),
42
                $request->query->get('locale')
43
            ), Response::HTTP_OK));
44
        } catch (\InvalidArgumentException $exception) {
45
            throw new NotFoundHttpException($exception->getMessage());
46
        }
47
    }
48
}
49