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

ProductBundleUpdater::setProductBundle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
    public function setProductBundle(ProductBundleInterface $productBundle): void
33
    {
34
        $this->productBundleManipulator->setProductBundle($productBundle);
35
    }
36
37
    public function getProductBundle(): ?ProductBundleInterface
38
    {
39
        return $this->productBundleManipulator->getProductBundle();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function addMissingSlotsToBundle(ProductBundleInterface $productBundle, array $allProductsPerSlot): void
46
    {
47
48
        // @todo maybe we should call the setter here and make the setter private?
49
        // @todo is there a use case for the getter since the bundle comes from the outside and could only be returned to the caller anyway and he should have the bundle already
50
        $existingSlots = $this->getExistingSlotNames($productBundle);
51
        foreach ($allProductsPerSlot as $slotName => $slotContent) {
52
            if ($this->slotAlreadyExists($slotName, $existingSlots)) {
53
                continue;
54
            }
55
            //@todo find a way to set a configured sort-value, maybe it is better to inject bundleSlotOptions from the outside
56
            $slotOptions = $this->bundleSlotOptionsFactory->createNewWithValues(99, false);
57
            $this->productBundleManipulator->addSlot($slotName, $slotOptions, $slotContent);
58
        }
59
    }
60
61
    /**
62
     * @return string[]
63
     */
64
    private function getExistingSlotNames(ProductBundleInterface $productBundle): array
65
    {
66
        $existingSlots = [];
67
        foreach ($productBundle->getSlots() as $bundleSlot) {
68
            $existingSlots[] = $bundleSlot->getName();
69
        }
70
        return $existingSlots;
71
    }
72
73
    /**
74
     * @param string[] $existingSlots
75
     */
76
    private function slotAlreadyExists(string $slotName, array $existingSlots): bool
77
    {
78
        return in_array($slotName, $existingSlots);
79
    }
80
}
81