Completed
Pull Request — master (#54)
by Konrad
04:41
created

ListProductsByPartialNameAction::resolveChannelProductPrice()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusElasticsearchPlugin\Controller\Action\Api;
14
15
use BitBag\SyliusElasticsearchPlugin\Controller\Response\DTO\Item;
16
use BitBag\SyliusElasticsearchPlugin\Controller\Response\ItemsResponse;
17
use BitBag\SyliusElasticsearchPlugin\Finder\NamedProductsFinderInterface;
18
use BitBag\SyliusElasticsearchPlugin\Transformer\Product\TransformerInterface;
19
use Sylius\Component\Core\Model\ProductInterface;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
24
final class ListProductsByPartialNameAction
25
{
26
    /** @var NamedProductsFinderInterface */
27
    private $namedProductsFinder;
28
29
    /** @var TransformerInterface */
30
    private $productSlugTransformer;
31
32
    /** @var TransformerInterface */
33
    private $productChannelPriceTransformer;
34
35
    /** @var TransformerInterface */
36
    private $productImageTransformer;
37
38
    public function __construct(
39
        NamedProductsFinderInterface $namedProductsFinder,
40
        TransformerInterface $productSlugResolver,
41
        TransformerInterface $productChannelPriceResolver,
42
        TransformerInterface $productImageResolver
43
    ) {
44
        $this->namedProductsFinder = $namedProductsFinder;
45
        $this->productSlugTransformer = $productSlugResolver;
46
        $this->productChannelPriceTransformer = $productChannelPriceResolver;
47
        $this->productImageTransformer = $productImageResolver;
48
    }
49
50
    public function __invoke(Request $request): Response
51
    {
52
        $itemsResponse = ItemsResponse::createEmpty();
53
54
        if (null === $request->query->get('query')) {
55
            return JsonResponse::create($itemsResponse->toArray());
56
        }
57
58
        $products = $this->namedProductsFinder->findByNamePart($request->query->get('query'));
59
60
        /** @var ProductInterface $product */
61
        foreach ($products as $product) {
62
            if (null === $productMainTaxon = $product->getMainTaxon()) {
63
                continue;
64
            }
65
66
            $itemsResponse->addItem(new Item(
67
                $productMainTaxon->getName(),
68
                $product->getName(),
69
                $product->getShortDescription(),
70
                $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

70
                /** @scrutinizer ignore-type */ $this->productSlugTransformer->transform($product),
Loading history...
71
                $this->productChannelPriceTransformer->transform($product),
0 ignored issues
show
Bug introduced by
It seems like $this->productChannelPri...er->transform($product) can also be of type null; however, parameter $price 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

71
                /** @scrutinizer ignore-type */ $this->productChannelPriceTransformer->transform($product),
Loading history...
72
                $this->productImageTransformer->transform($product)
73
            ));
74
        }
75
76
        return JsonResponse::create($itemsResponse->toArray());
77
    }
78
}
79