Completed
Push — master ( 35f668...d57f4e )
by G
03:55
created

provideVariantsDeposits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gweb\SyliusProductDepositPlugin\Provider;
6
7
use Gweb\SyliusProductDepositPlugin\Entity\ProductVariantInterface;
8
use Sylius\Component\Core\Model\ChannelInterface;
9
use Sylius\Component\Core\Model\ProductInterface;
10
use Sylius\Component\Product\Model\ProductOptionValueInterface;
11
12
final class ProductVariantsDepositsProvider implements ProductVariantsDepositsProviderInterface
13
{
14
    public function provideVariantsDeposits(ProductInterface $product, ChannelInterface $channel): array
15
    {
16
        $variantsDeposits = [];
17
18
        /** @var ProductVariantInterface $variant */
19
        foreach ($product->getVariants() as $variant) {
20
            $variantsDeposits[] = $this->constructOptionsMap($variant, $channel);
21
        }
22
23
        return $variantsDeposits;
24
    }
25
26
    private function constructOptionsMap(ProductVariantInterface $variant, ChannelInterface $channel): array
27
    {
28
        $optionMap = [];
29
30
        /** @var ProductOptionValueInterface $option */
31
        foreach ($variant->getOptionValues() as $option) {
32
            $optionMap[$option->getOptionCode()] = $option->getCode();
33
        }
34
35
        $channelDeposit = $variant->getChannelDepositForChannel($channel);
36
        if ($channelDeposit) {
37
            $optionMap['value'] = $channelDeposit->getPrice();
38
        }
39
40
        return $optionMap;
41
    }
42
}
43