Completed
Pull Request — master (#22)
by Matthias
06:13
created

ProductBundleCreator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 85
rs 10
c 0
b 0
f 0

9 Methods

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