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