|
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
|
|
|
class ProductBundleCreator |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var FactoryInterface */ |
|
15
|
|
|
private $productBundleFactory; |
|
16
|
|
|
|
|
17
|
|
|
/** @var FactoryInterface */ |
|
18
|
|
|
private $productBundleSlotFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** @var ProductBundleInterface */ |
|
21
|
|
|
private $productBundle; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct( |
|
24
|
|
|
FactoryInterface $productBundleFactory, |
|
25
|
|
|
FactoryInterface $productBundleSlotFactory |
|
26
|
|
|
) { |
|
27
|
|
|
$this->productBundleFactory = $productBundleFactory; |
|
28
|
|
|
$this->productBundleSlotFactory = $productBundleSlotFactory; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function createProductBundle(string $productBundleName, ProductInterface $productBundleProduct): self |
|
32
|
|
|
{ |
|
33
|
|
|
$this->productBundle = $this->productBundleFactory->createNew(); |
|
34
|
|
|
$this->productBundle->setName($productBundleName); |
|
35
|
|
|
$this->productBundle->setProduct($productBundleProduct); |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getProductBundle(): ProductBundleInterface |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->productBundle; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function addSlot(string $slotName, array $options = [], array $products = []): self |
|
46
|
|
|
{ |
|
47
|
|
|
/** @var ProductBundleSlotInterface $slot */ |
|
48
|
|
|
$slot = $this->createSlot($slotName); |
|
49
|
|
|
|
|
50
|
|
|
$this->applyOptionsToSlot($options, $slot); |
|
51
|
|
|
$this->addProductsToSlot($products, $slot); |
|
52
|
|
|
$this->addSlotToBundle($options, $slot); |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function applyOptionsToSlot(array $options, ProductBundleSlotInterface $slot): void |
|
58
|
|
|
{ |
|
59
|
|
|
if (isset($options['position'])) { |
|
60
|
|
|
$slot->setPosition($options['position']); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function addProductsToSlot(array $products, ProductBundleSlotInterface $slot): void |
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($products as $product) { |
|
67
|
|
|
$this->addProductToSlot($product, $slot); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function addProductToSlot(ProductInterface $product, ProductBundleSlotInterface $slot): void |
|
72
|
|
|
{ |
|
73
|
|
|
$slot->addProduct($product); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function addSlotToBundle(array $options, $slot): void |
|
77
|
|
|
{ |
|
78
|
|
|
if (isset($options['isPresentation']) && $options['isPresentation'] === true) { |
|
79
|
|
|
$this->productBundle->setPresentationSlot($slot); |
|
80
|
|
|
} |
|
81
|
|
|
$this->productBundle->addSlot($slot); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function createSlot(string $slotName): ProductBundleSlotInterface |
|
85
|
|
|
{ |
|
86
|
|
|
/** @var ProductBundleSlotInterface $slot */ |
|
87
|
|
|
$slot = $this->productBundleSlotFactory->createNew(); |
|
88
|
|
|
$slot->setName($slotName); |
|
89
|
|
|
$slot->setBundle($this->productBundle); |
|
90
|
|
|
return $slot; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|