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( |
55
|
|
|
string $slotName, |
56
|
|
|
?ProductBundleSlotOptionsInterface $options = null, |
57
|
|
|
array $products = [] |
58
|
|
|
): self { |
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( |
70
|
|
|
?ProductBundleSlotOptionsInterface $options, |
71
|
|
|
ProductBundleSlotInterface $slot |
72
|
|
|
): void { |
73
|
|
|
if (null !== $options && null !== $options->getPosition()) { |
74
|
|
|
$slot->setPosition($options->getPosition()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function addProductsToSlot(array $products, ProductBundleSlotInterface $slot): void |
79
|
|
|
{ |
80
|
|
|
foreach ($products as $product) { |
81
|
|
|
$this->addProductToSlot($product, $slot); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function addProductToSlot(ProductInterface $product, ProductBundleSlotInterface $slot): void |
86
|
|
|
{ |
87
|
|
|
$slot->addProduct($product); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function addSlotToBundle(?ProductBundleSlotOptionsInterface $options, $slot): void |
91
|
|
|
{ |
92
|
|
|
if (null !== $options && $options->isPresentationSlot()) { |
93
|
|
|
$this->productBundle->setPresentationSlot($slot); |
94
|
|
|
} |
95
|
|
|
$this->productBundle->addSlot($slot); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function createSlot(string $slotName): ProductBundleSlotInterface |
99
|
|
|
{ |
100
|
|
|
/** @var ProductBundleSlotInterface $slot */ |
101
|
|
|
$slot = $this->productBundleSlotFactory->createNew(); |
102
|
|
|
$slot->setName($slotName); |
103
|
|
|
$slot->setBundle($this->productBundle); |
104
|
|
|
|
105
|
|
|
return $slot; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|