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\Transformer\Product; |
14
|
|
|
|
15
|
|
|
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface; |
16
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
17
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
18
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
19
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
20
|
|
|
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface; |
21
|
|
|
|
22
|
|
|
final class ChannelPricingTransformer implements TransformerInterface |
23
|
|
|
{ |
24
|
|
|
/** @var ChannelContextInterface */ |
25
|
|
|
private $channelContext; |
26
|
|
|
|
27
|
|
|
/** @var ProductVariantResolverInterface */ |
28
|
|
|
private $productVariantResolver; |
29
|
|
|
|
30
|
|
|
/** @var MoneyFormatterInterface */ |
31
|
|
|
private $moneyFormatter; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
ChannelContextInterface $channelContext, |
35
|
|
|
ProductVariantResolverInterface $productVariantResolver, |
36
|
|
|
MoneyFormatterInterface $moneyFormatter |
37
|
|
|
) { |
38
|
|
|
$this->channelContext = $channelContext; |
39
|
|
|
$this->productVariantResolver = $productVariantResolver; |
40
|
|
|
$this->moneyFormatter = $moneyFormatter; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function transform(ProductInterface $product): ?string |
44
|
|
|
{ |
45
|
|
|
/** @var ChannelInterface|null $channel */ |
46
|
|
|
$channel = $this->channelContext->getChannel(); |
47
|
|
|
|
48
|
|
|
if (null === $channelBaseCurrency = $channel->getBaseCurrency()) { |
49
|
|
|
throw new \RuntimeException('No channel currency configured'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @var ProductVariantInterface $productVariant */ |
53
|
|
|
$productVariant = $this->productVariantResolver->getVariant($product); |
54
|
|
|
|
55
|
|
|
$productVariantPricing = $productVariant->getChannelPricingForChannel($channel); |
56
|
|
|
|
57
|
|
|
if (null === $productVariantPricing) { |
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->moneyFormatter->format($productVariantPricing->getPrice(), $channelBaseCurrency->getCode()); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|