ChannelPricingTransformer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 49
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 23 4
A __construct() 0 10 1
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());
0 ignored issues
show
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

69
        return $this->moneyFormatter->format($productVariantPricing->getPrice(), /** @scrutinizer ignore-type */ $channelBaseCurrency->getCode(), $this->localeContext->getLocaleCode());
Loading history...
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

69
        return $this->moneyFormatter->format(/** @scrutinizer ignore-type */ $productVariantPricing->getPrice(), $channelBaseCurrency->getCode(), $this->localeContext->getLocaleCode());
Loading history...
70
    }
71
}
72