Failed Conditions
Push — master ( 09f66d...6b086b )
by Łukasz
13s
created

ShowLatestProductAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Sylius\ShopApiPlugin\Controller\Product;
5
6
use FOS\RestBundle\View\View;
7
use FOS\RestBundle\View\ViewHandlerInterface;
8
use Sylius\ShopApiPlugin\ViewRepository\ProductLatestViewRepositoryInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13 View Code Duplication
final class ShowLatestProductAction
14
{
15
    /** @var ViewHandlerInterface */
16
    private $viewHandler;
17
18
    /** @var ProductLatestViewRepositoryInterface */
19
    private $productLatestQuery;
20
21
    public function __construct(
22
        ViewHandlerInterface $viewHandler,
23
        ProductLatestViewRepositoryInterface $productLatestQuery
24
    ) {
25
        $this->viewHandler = $viewHandler;
26
        $this->productLatestQuery = $productLatestQuery;
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->productLatestQuery->getLatestProducts(
37
                $request->query->get('channel'),
38
                $request->query->get('locale'),
39
                $request->query->getInt('limit', 4)
40
            ), Response::HTTP_OK));
41
        } catch (\InvalidArgumentException $exception) {
42
            throw new NotFoundHttpException($exception->getMessage());
43
        }
44
    }
45
}
46