Failed Conditions
Pull Request — master (#237)
by
unknown
02:14
created

ShowLatestProductAction::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
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
1 ignored issue
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...
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
                (int) $request->query->get('limit', 4)
40
            ), Response::HTTP_OK));
41
        } catch (\InvalidArgumentException $exception) {
42
            throw new NotFoundHttpException($exception->getMessage());
43
        }
44
    }
45
}