Completed
Pull Request — master (#44)
by Matthias
01:41
created

ProductBundleUpdater::getExistingSlotNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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
16
class ProductBundleUpdater implements ProductBundleUpdaterInterface
17
{
18
    /** @var ProductBundleSlotOptionsFactoryInterface */
19
    private $bundleSlotOptionsFactory;
20
21
    /** @var ProductBundleManipulatorInterface */
22
    private $productBundleManipulator;
23
24
    public function __construct(
25
        ProductBundleSlotOptionsFactoryInterface $bundleSlotOptionsFactory,
26
        ProductBundleManipulatorInterface $productBundleManipulator
27
    ) {
28
        $this->bundleSlotOptionsFactory = $bundleSlotOptionsFactory;
29
        $this->productBundleManipulator = $productBundleManipulator;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function addMissingSlotsToBundle(
36
        ProductBundleInterface $productBundle,
37
        array $allProductsPerSlot,
38
        array $slotOptions
39
    ): void {
40
        /** @todo look into moving some services from morpheus to here. e.g. ProductBundleSlotOptionsCreator */
41
        $this->productBundleManipulator->setProductBundle($productBundle);
42
        $existingSlots = $this->getExistingSlotNames($productBundle);
43
        foreach ($allProductsPerSlot as $slotName => $slotContent) {
44
            if ($this->slotAlreadyExists($slotName, $existingSlots)) {
45
                continue;
46
            }
47
            //@todo find a way to set a configured sort-value, maybe it is better to inject bundleSlotOptions from the outside
48
            if (false === isset($slotOptions[$slotName])) {
49
                $optionsForSlot = $this->bundleSlotOptionsFactory->createNewWithValues(99, false);
50
            } else {
51
                $optionsForSlot = $slotOptions[$slotName];
52
            }
53
            $this->productBundleManipulator->addSlot($slotName, $optionsForSlot, $slotContent);
54
        }
55
    }
56
57
    /**
58
     * @return string[]
59
     */
60
    private function getExistingSlotNames(ProductBundleInterface $productBundle): array
61
    {
62
        $existingSlots = [];
63
        foreach ($productBundle->getSlots() as $bundleSlot) {
64
            $existingSlots[] = $bundleSlot->getName();
65
        }
66
        return $existingSlots;
67
    }
68
69
    /**
70
     * @param string[] $existingSlots
71
     */
72
    private function slotAlreadyExists(string $slotName, array $existingSlots): bool
73
    {
74
        return in_array($slotName, $existingSlots);
75
    }
76
}
77