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

ChannelPricingTransformer::transform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 19
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\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());
0 ignored issues
show
Bug introduced by
It seems like $productVariantPricing->getPrice() can also be of type null; however, parameter $amount of Sylius\Bundle\MoneyBundl...tterInterface::format() does only seem to accept integer, 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

61
        return $this->moneyFormatter->format(/** @scrutinizer ignore-type */ $productVariantPricing->getPrice(), $channelBaseCurrency->getCode());
Loading history...
Bug introduced by
It seems like $channelBaseCurrency->getCode() can also be of type null; however, parameter $currencyCode of Sylius\Bundle\MoneyBundl...tterInterface::format() 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

61
        return $this->moneyFormatter->format($productVariantPricing->getPrice(), /** @scrutinizer ignore-type */ $channelBaseCurrency->getCode());
Loading history...
62
    }
63
}
64