Passed
Push — master ( 9d9530...9d60e1 )
by G
07:54
created

ProductVariantsDepositsProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 5

2 Methods

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