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

ShowLatestProductAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 1
dl 33
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A __invoke() 16 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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