ProductBundleUpdater::getOptionsForSlot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Created by solutionDrive GmbH
7
 *
8
 * @copyright 2018 solutionDrive GmbH
9
 */
10
11
namespace solutionDrive\SyliusProductBundlesPlugin\Service;
12
13
use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleInterface;
14
use solutionDrive\SyliusProductBundlesPlugin\Factory\ProductBundleSlotOptionsFactoryInterface;
15
use solutionDrive\SyliusProductBundlesPlugin\Service\Options\ProductBundleSlotOptionsInterface;
16
17
class ProductBundleUpdater implements ProductBundleUpdaterInterface
18
{
19
    /** @var ProductBundleSlotOptionsFactoryInterface */
20
    private $bundleSlotOptionsFactory;
21
22
    /** @var ProductBundleManipulatorInterface */
23
    private $productBundleManipulator;
24
25
    public function __construct(
26
        ProductBundleSlotOptionsFactoryInterface $bundleSlotOptionsFactory,
27
        ProductBundleManipulatorInterface $productBundleManipulator
28
    ) {
29
        $this->bundleSlotOptionsFactory = $bundleSlotOptionsFactory;
30
        $this->productBundleManipulator = $productBundleManipulator;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function addMissingSlotsToBundle(
37
        ProductBundleInterface $productBundle,
38
        array $allProductsPerSlot,
39
        array $slotOptions
40
    ): void {
41
        // @todo look into moving some services from morpheus to here. e.g. ProductBundleSlotOptionsCreator
42
        $this->productBundleManipulator->setProductBundle($productBundle);
43
        $existingSlots = $this->getExistingSlotNames($productBundle);
44
45
        foreach ($allProductsPerSlot as $slotName => $slotContent) {
46
            if (false === $this->slotAlreadyExists($slotName, $existingSlots)) {
47
                $optionsForSlot = $this->getOptionsForSlot($slotOptions, $slotName);
48
49
                $this->productBundleManipulator->addSlot($slotName, $optionsForSlot, $slotContent);
50
            }
51
        }
52
    }
53
54
    /**
55
     * @return mixed[]
56
     */
57
    private function getExistingSlotNames(ProductBundleInterface $productBundle): array
58
    {
59
        $existingSlots = [];
60
        foreach ($productBundle->getSlots() as $bundleSlot) {
61
            $existingSlots[] = $bundleSlot->getName();
62
        }
63
64
        return $existingSlots;
65
    }
66
67
    /**
68
     * @param string[] $existingSlots
69
     */
70
    private function slotAlreadyExists(string $slotName, array $existingSlots): bool
71
    {
72
        return in_array($slotName, $existingSlots);
73
    }
74
75
    /**
76
     * @param ProductBundleSlotOptionsInterface[] $slotOptions
77
     */
78
    private function getOptionsForSlot(array $slotOptions, string $slotName): ProductBundleSlotOptionsInterface
79
    {
80
        if (false === isset($slotOptions[$slotName])) {
81
            return $this->bundleSlotOptionsFactory->createNewWithValues(99, false);
82
        }
83
84
        return $slotOptions[$slotName];
85
    }
86
}
87