Completed
Push — master ( f713d7...12c083 )
by Tobias
11s
created

ProductBundleUpdater::addMissingSlotsToBundle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 3
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 string[]
56
     */
57
    private function getExistingSlotNames(ProductBundleInterface $productBundle): array
58
    {
59
        $existingSlots = [];
60
        foreach ($productBundle->getSlots() as $bundleSlot) {
61
            $existingSlots[] = $bundleSlot->getName();
62
        }
63
        return $existingSlots;
64
    }
65
66
    /**
67
     * @param string[] $existingSlots
68
     */
69
    private function slotAlreadyExists(string $slotName, array $existingSlots): bool
70
    {
71
        return in_array($slotName, $existingSlots);
72
    }
73
74
    /**
75
     * @param ProductBundleSlotOptionsFactoryInterface[] $slotOptions
76
     */
77
    private function getOptionsForSlot(array $slotOptions, string $slotName): ProductBundleSlotOptionsInterface
78
    {
79
        if (false === isset($slotOptions[$slotName])) {
80
            $optionsForSlot = $this->bundleSlotOptionsFactory->createNewWithValues(99, false);
81
        } else {
82
            $optionsForSlot = $slotOptions[$slotName];
83
        }
84
        return $optionsForSlot;
85
    }
86
}
87