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

ProductBundleManipulator::createSlot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\Entity\ProductBundleSlotInterface;
15
use solutionDrive\SyliusProductBundlesPlugin\Service\Options\ProductBundleSlotOptionsInterface;
16
use Sylius\Component\Core\Model\ProductInterface;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
19
class ProductBundleManipulator implements ProductBundleManipulatorInterface
20
{
21
    /** @var FactoryInterface */
22
    private $productBundleSlotFactory;
23
24
    /** @var ProductBundleInterface */
25
    private $productBundle;
26
27
    public function __construct(FactoryInterface $productBundleSlotFactory)
28
    {
29
        $this->productBundleSlotFactory = $productBundleSlotFactory;
30
    }
31
32
    public function setProductBundle(ProductBundleInterface $productBundle): void
33
    {
34
        $this->productBundle = $productBundle;
35
    }
36
37
    public function getProductBundle(): ?ProductBundleInterface
38
    {
39
        return $this->productBundle;
40
    }
41
42
    /**
43
     * @param ProductInterface[] $products
44
     */
45
    public function addSlot(
46
        string $slotName,
47
        ProductBundleSlotOptionsInterface $options = null,
48
        array $products = []
49
    ): void {
50
        $slot = $this->createSlot($slotName);
51
        $this->applyOptionsToSlot($options, $slot);
52
        $this->addProductsToSlot($products, $slot);
53
        $this->addSlotToBundle($slot);
54
    }
55
56
    private function createSlot(string $slotName): ProductBundleSlotInterface
57
    {
58
        /** @var ProductBundleSlotInterface $slot */
59
        $slot = $this->productBundleSlotFactory->createNew();
60
        $slot->setName($slotName);
61
62
        return $slot;
63
    }
64
65
    private function applyOptionsToSlot(
66
        ?ProductBundleSlotOptionsInterface $options,
67
        ProductBundleSlotInterface $slot
68
    ): void {
69
        if ($this->hasPositionOption($options)) {
70
            $slot->setPosition($options->getPosition());
71
        }
72
        if ($this->shouldSetAsPresentationSlot($options)) {
73
            $this->setPresentationSlotOnBundle($slot);
74
        }
75
    }
76
77
    private function addSlotToBundle(ProductBundleSlotInterface $slot): void
78
    {
79
        $this->productBundle->addSlot($slot);
80
    }
81
82
    private function setPresentationSlotOnBundle(ProductBundleSlotInterface $slot): void
83
    {
84
        $this->productBundle->setPresentationSlot($slot);
85
    }
86
87
    /**
88
     * @param ProductInterface[] $products
89
     */
90
    private function addProductsToSlot(array $products, ProductBundleSlotInterface $slot): void
91
    {
92
        foreach ($products as $product) {
93
            $this->addProductToSlot($product, $slot);
94
        }
95
    }
96
97
    private function addProductToSlot(ProductInterface $product, ProductBundleSlotInterface $slot): void
98
    {
99
        $slot->addProduct($product);
100
    }
101
102
    private function hasPositionOption(?ProductBundleSlotOptionsInterface $options): bool
103
    {
104
        return null !== $options && null !== $options->getPosition();
105
    }
106
107
    private function shouldSetAsPresentationSlot(?ProductBundleSlotOptionsInterface $options): bool
108
    {
109
        return null !== $options && $options->isPresentationSlot();
110
    }
111
}
112