Completed
Pull Request — master (#44)
by Matthias
22:54
created

ProductBundleUpdater::slotAlreadyExists()   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 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
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(ProductBundleInterface $productBundle, array $allProductsPerSlot): void
36
    {
37
        $this->productBundleManipulator->setProductBundle($productBundle);
38
        $existingSlots = $this->getExistingSlotNames($productBundle);
39
        foreach ($allProductsPerSlot as $slotName => $slotContent) {
40
            if ($this->slotAlreadyExists($slotName, $existingSlots)) {
41
                continue;
42
            }
43
            //@todo find a way to set a configured sort-value, maybe it is better to inject bundleSlotOptions from the outside
44
            $slotOptions = $this->bundleSlotOptionsFactory->createNewWithValues(99, false);
45
            $this->productBundleManipulator->addSlot($slotName, $slotOptions, $slotContent);
46
        }
47
    }
48
49
    /**
50
     * @return string[]
51
     */
52
    private function getExistingSlotNames(ProductBundleInterface $productBundle): array
53
    {
54
        $existingSlots = [];
55
        foreach ($productBundle->getSlots() as $bundleSlot) {
56
            $existingSlots[] = $bundleSlot->getName();
57
        }
58
        return $existingSlots;
59
    }
60
61
    /**
62
     * @param string[] $existingSlots
63
     */
64
    private function slotAlreadyExists(string $slotName, array $existingSlots): bool
65
    {
66
        return in_array($slotName, $existingSlots);
67
    }
68
}
69