Completed
Pull Request — master (#179)
by Łukasz
02:48
created

ShowProductCatalogByCodeAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 7
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __invoke() 0 17 3
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Controller\Product;
4
5
use FOS\RestBundle\View\View;
6
use FOS\RestBundle\View\ViewHandlerInterface;
7
use Sylius\ShopApiPlugin\Model\PaginatorDetails;
8
use Sylius\ShopApiPlugin\Query\ProductCatalogQuery;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13
final class ShowProductCatalogByCodeAction
14
{
15
    /** @var ViewHandlerInterface */
16
    private $viewHandler;
17
18
    /** @var ProductCatalogQuery */
19
    private $productCatalogQuery;
20
21
    public function __construct(
22
        ViewHandlerInterface $viewHandler,
23
        ProductCatalogQuery $productCatalogQuery
24
    ) {
25
        $this->viewHandler = $viewHandler;
26
        $this->productCatalogQuery = $productCatalogQuery;
27
    }
28
29
    public function __invoke(Request $request): Response
30
    {
31
        if (!$request->query->has('channel')) {
32
            throw new NotFoundHttpException('Cannot find product without channel provided');
33
        }
34
35
        try {
36
            return $this->viewHandler->handle(View::create($this->productCatalogQuery->findByTaxonCode(
37
                $request->attributes->get('code'),
38
                $request->query->get('locale'),
39
                $request->query->get('channel'),
40
                new PaginatorDetails($request->attributes->get('_route'), $request->query->all())
41
            ), Response::HTTP_OK));
42
        } catch (\InvalidArgumentException $exception) {
43
            throw new NotFoundHttpException($exception->getMessage());
44
        }
45
    }
46
}
47