Completed
Pull Request — master (#22)
by Matthias
04:23
created

ProductBundleCreator::addSlot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace solutionDrive\SyliusProductBundlesPlugin\Service;
6
7
use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleInterface;
8
use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleSlotInterface;
9
use Sylius\Component\Core\Model\ProductInterface;
10
use Sylius\Component\Resource\Factory\FactoryInterface;
11
12
/**
13
 * Created by solutionDrive GmbH
14
 *
15
 * @copyright 2018 solutionDrive GmbH
16
 */
17
class ProductBundleCreator implements ProductBundleCreatorInterface
18
{
19
    /** @var FactoryInterface */
20
    private $productBundleFactory;
21
22
    /** @var FactoryInterface */
23
    private $productBundleSlotFactory;
24
25
    /** @var ProductBundleInterface */
26
    private $productBundle;
27
28
    public function __construct(
29
        FactoryInterface $productBundleFactory,
30
        FactoryInterface $productBundleSlotFactory
31
    ) {
32
        $this->productBundleFactory = $productBundleFactory;
33
        $this->productBundleSlotFactory = $productBundleSlotFactory;
34
    }
35
36
    public function createProductBundle(string $productBundleName, ProductInterface $productBundleProduct): self
37
    {
38
        $this->productBundle = $this->productBundleFactory->createNew();
39
        $this->productBundle->setName($productBundleName);
40
        $this->productBundle->setProduct($productBundleProduct);
41
42
        return $this;
43
    }
44
45
    public function getProductBundle(): ProductBundleInterface
46
    {
47
        return $this->productBundle;
48
    }
49
50
    /**
51
     * @param string $slotName
52
     * @param array $options
53
     * @param ProductInterface[] $products
54
     *
55
     * @return ProductBundleCreator
56
     */
57
    public function addSlot(string $slotName, array $options = [], array $products = []): self
58
    {
59
        /** @var ProductBundleSlotInterface $slot */
60
        $slot = $this->createSlot($slotName);
61
62
        $this->applyOptionsToSlot($options, $slot);
63
        $this->addProductsToSlot($products, $slot);
64
        $this->addSlotToBundle($options, $slot);
65
66
        return $this;
67
    }
68
69
    private function applyOptionsToSlot(array $options, ProductBundleSlotInterface $slot): void
70
    {
71
        if (isset($options['position'])) {
72
            $slot->setPosition($options['position']);
73
        }
74
    }
75
76
    private function addProductsToSlot(array $products, ProductBundleSlotInterface $slot): void
77
    {
78
        foreach ($products as $product) {
79
            $this->addProductToSlot($product, $slot);
80
        }
81
    }
82
83
    private function addProductToSlot(ProductInterface $product, ProductBundleSlotInterface $slot): void
84
    {
85
        $slot->addProduct($product);
86
    }
87
88
    private function addSlotToBundle(array $options, $slot): void
89
    {
90
        if (isset($options['isPresentation']) && $options['isPresentation'] === true) {
91
            $this->productBundle->setPresentationSlot($slot);
92
        }
93
        $this->productBundle->addSlot($slot);
94
    }
95
96
    private function createSlot(string $slotName): ProductBundleSlotInterface
97
    {
98
        /** @var ProductBundleSlotInterface $slot */
99
        $slot = $this->productBundleSlotFactory->createNew();
100
        $slot->setName($slotName);
101
        $slot->setBundle($this->productBundle);
102
103
        return $slot;
104
    }
105
}
106