ProductVariantsBasePriceExtension::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecocode\SyliusBasePricePlugin\Services\Helper;
6
7
use Ecocode\SyliusBasePricePlugin\Entity\Product\ProductVariantInterface;
8
use Ecocode\SyliusBasePricePlugin\Services\Calculator;
9
use Sylius\Component\Channel\Context\ChannelContextInterface;
10
use Sylius\Component\Core\Model\ProductInterface;
11
use Sylius\Component\Currency\Context\CurrencyContextInterface;
12
use Twig\Extension\AbstractExtension;
13
use Twig\TwigFunction;
14
15
/**
16
 * Class ProductVariantsBasePriceExtension
17
 * @package Ecocode\SyliusBasePricePlugin\Services\Helper
18
 */
19
final class ProductVariantsBasePriceExtension extends AbstractExtension
20
{
21
    /** @var Calculator */
22
    private $calculator;
23
24
    /** @var ChannelContextInterface */
25
    private $channelContext;
26
27
    /** @var CurrencyContextInterface */
28
    private $currencyContext;
29
30 3
    public function __construct(
31
        Calculator $calculator,
32
        ChannelContextInterface $channelContext,
33
        CurrencyContextInterface $currencyContext
34
    ) {
35 3
        $this->calculator      = $calculator;
36 3
        $this->channelContext  = $channelContext;
37 3
        $this->currencyContext = $currencyContext;
38 3
    }
39
40 1
    public function getFunctions(): array
41
    {
42
        return [
43 1
            new TwigFunction('ecocode_calculate_base_price', [$this, 'calculateBasePrice']),
44 1
            new TwigFunction('ecocode_calculate_base_prices', [$this, 'calculateBasePrices']),
45
        ];
46
    }
47
48 2
    public function calculateBasePrice(ProductVariantInterface $productVariant): ?string
49
    {
50
        /** @var \Sylius\Component\Core\Model\Channel $channel */
51 2
        $channel             = $this->channelContext->getChannel();
52 2
        $currentCurrencyCode = $this->currencyContext->getCurrencyCode();
53
54 2
        return $this->calculator->calculate(
55 2
            $productVariant,
56
            $channel,
57
            $currentCurrencyCode
58
        );
59
    }
60
61
    /**
62
     * @param ProductInterface $product
63
     *
64
     * @return array<int, string|null>
0 ignored issues
show
Documentation introduced by
The doc-type array<int, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
65
     */
66 1
    public function calculateBasePrices(ProductInterface $product): array
67
    {
68 1
        $basePrices = [];
69 1
        foreach ($product->getVariants() as $productVariant) {
70 1
            if ($productVariant instanceof ProductVariantInterface) {
71 1
                $basePrices[] = (string)$this->calculateBasePrice($productVariant);
72
            }
73
        }
74
75 1
        return $basePrices;
76
    }
77
}
78