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

constructOptionsMap()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
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