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

ProductBundleUpdater   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 53
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addMissingSlotsToBundle() 0 13 3
A getExistingSlotNames() 0 8 2
A slotAlreadyExists() 0 4 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(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