ListProductsByPartialNameAction::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 27
rs 9.7666
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusElasticsearchPlugin\Controller\Action\Api;
12
13
use BitBag\SyliusElasticsearchPlugin\Controller\Response\DTO\Item;
14
use BitBag\SyliusElasticsearchPlugin\Controller\Response\ItemsResponse;
15
use BitBag\SyliusElasticsearchPlugin\Finder\NamedProductsFinderInterface;
16
use BitBag\SyliusElasticsearchPlugin\Transformer\Product\TransformerInterface;
17
use Sylius\Component\Core\Model\ProductInterface;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
22
final class ListProductsByPartialNameAction
23
{
24
    /** @var NamedProductsFinderInterface */
25
    private $namedProductsFinder;
26
27
    /** @var TransformerInterface */
28
    private $productSlugTransformer;
29
30
    /** @var TransformerInterface */
31
    private $productChannelPriceTransformer;
32
33
    /** @var TransformerInterface */
34
    private $productImageTransformer;
35
36
    public function __construct(
37
        NamedProductsFinderInterface $namedProductsFinder,
38
        TransformerInterface $productSlugResolver,
39
        TransformerInterface $productChannelPriceResolver,
40
        TransformerInterface $productImageResolver
41
    ) {
42
        $this->namedProductsFinder = $namedProductsFinder;
43
        $this->productSlugTransformer = $productSlugResolver;
44
        $this->productChannelPriceTransformer = $productChannelPriceResolver;
45
        $this->productImageTransformer = $productImageResolver;
46
    }
47
48
    public function __invoke(Request $request): Response
49
    {
50
        $itemsResponse = ItemsResponse::createEmpty();
51
52
        if (null === $request->query->get('query')) {
53
            return JsonResponse::create($itemsResponse->toArray());
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpFo...\JsonResponse::create() has been deprecated: since Symfony 5.1, use __construct() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

53
            return /** @scrutinizer ignore-deprecated */ JsonResponse::create($itemsResponse->toArray());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
54
        }
55
56
        $products = $this->namedProductsFinder->findByNamePart($request->query->get('query'));
57
58
        /** @var ProductInterface $product */
59
        foreach ($products as $product) {
60
            if (null === $productMainTaxon = $product->getMainTaxon()) {
61
                continue;
62
            }
63
64
            $itemsResponse->addItem(new Item(
65
                $productMainTaxon->getName(),
66
                $product->getName(),
67
                $product->getShortDescription(),
68
                $this->productSlugTransformer->transform($product),
0 ignored issues
show
Bug introduced by
It seems like $this->productSlugTransf...er->transform($product) can also be of type null; however, parameter $slug of BitBag\SyliusElasticsear...DTO\Item::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
                /** @scrutinizer ignore-type */ $this->productSlugTransformer->transform($product),
Loading history...
69
                $this->productChannelPriceTransformer->transform($product),
70
                $this->productImageTransformer->transform($product)
71
            ));
72
        }
73
74
        return JsonResponse::create($itemsResponse->toArray());
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpFo...\JsonResponse::create() has been deprecated: since Symfony 5.1, use __construct() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

74
        return /** @scrutinizer ignore-deprecated */ JsonResponse::create($itemsResponse->toArray());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
75
    }
76
}
77