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()); |
|
|
|
|
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), |
|
|
|
|
69
|
|
|
$this->productChannelPriceTransformer->transform($product), |
70
|
|
|
$this->productImageTransformer->transform($product) |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return JsonResponse::create($itemsResponse->toArray()); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.