ProductBundleManipulator::hasPositionOption()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
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\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
        if ($options instanceof ProductBundleSlotOptionsInterface) {
52
            $this->applyOptionsToSlot($options, $slot);
53
        }
54
        $this->addProductsToSlot($products, $slot);
55
        $this->addSlotToBundle($slot);
56
    }
57
58
    private function createSlot(string $slotName): ProductBundleSlotInterface
59
    {
60
        /** @var ProductBundleSlotInterface $slot */
61
        $slot = $this->productBundleSlotFactory->createNew();
62
        $slot->setName($slotName);
63
64
        return $slot;
65
    }
66
67
    private function applyOptionsToSlot(
68
        ProductBundleSlotOptionsInterface $options,
69
        ProductBundleSlotInterface $slot
70
    ): void {
71
        if ($this->hasPositionOption($options)) {
72
            $slot->setPosition($options->getPosition());
73
        }
74
        if ($this->shouldSetAsPresentationSlot($options)) {
75
            $this->setPresentationSlotOnBundle($slot);
76
        }
77
    }
78
79
    private function addSlotToBundle(ProductBundleSlotInterface $slot): void
80
    {
81
        $this->productBundle->addSlot($slot);
82
    }
83
84
    private function setPresentationSlotOnBundle(ProductBundleSlotInterface $slot): void
85
    {
86
        $this->productBundle->setPresentationSlot($slot);
87
    }
88
89
    /**
90
     * @param ProductInterface[] $products
91
     */
92
    private function addProductsToSlot(array $products, ProductBundleSlotInterface $slot): void
93
    {
94
        foreach ($products as $product) {
95
            $this->addProductToSlot($product, $slot);
96
        }
97
    }
98
99
    private function addProductToSlot(ProductInterface $product, ProductBundleSlotInterface $slot): void
100
    {
101
        $slot->addProduct($product);
102
    }
103
104
    private function hasPositionOption(?ProductBundleSlotOptionsInterface $options): bool
105
    {
106
        return null !== $options && null !== $options->getPosition();
107
    }
108
109
    private function shouldSetAsPresentationSlot(?ProductBundleSlotOptionsInterface $options): bool
110
    {
111
        return null !== $options && $options->isPresentationSlot();
112
    }
113
}
114