1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace solutionDrive\SyliusProductBundlesPlugin\Service; |
4
|
|
|
|
5
|
|
|
use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleInterface; |
6
|
|
|
use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleSlotInterface; |
7
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
8
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
9
|
|
|
|
10
|
|
|
class ProductBundleCreator |
11
|
|
|
{ |
12
|
|
|
/** @var FactoryInterface */ |
13
|
|
|
private $productBundleFactory; |
14
|
|
|
|
15
|
|
|
/** @var FactoryInterface */ |
16
|
|
|
private $productBundleSlotFactory; |
17
|
|
|
|
18
|
|
|
/** @var ProductBundleInterface */ |
19
|
|
|
private $productBundle; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
FactoryInterface $productBundleFactory, |
23
|
|
|
FactoryInterface $productBundleSlotFactory |
24
|
|
|
) { |
25
|
|
|
$this->productBundleFactory = $productBundleFactory; |
26
|
|
|
$this->productBundleSlotFactory = $productBundleSlotFactory; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function createProductBundle(): ProductBundleCreator |
30
|
|
|
{ |
31
|
|
|
$this->productBundle = $this->productBundleFactory->createNew(); |
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getProductBundle(): ProductBundleInterface |
36
|
|
|
{ |
37
|
|
|
return $this->productBundle; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function addSlot(string $slotName, array $options = [], array $products = []): ProductBundleCreator |
41
|
|
|
{ |
42
|
|
|
/** @var ProductBundleSlotInterface $slot */ |
43
|
|
|
$slot = $this->productBundleSlotFactory->createNew(); |
44
|
|
|
$slot->setName($slotName); |
45
|
|
|
$slot->setBundle($this->productBundle); |
46
|
|
|
$this->applyOptionsToSlot($options, $slot); |
47
|
|
|
$this->addProductsToSlot($products, $slot); |
48
|
|
|
|
49
|
|
|
$this->productBundle->addSlot($slot); |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function applyOptionsToSlot(array $options, ProductBundleSlotInterface $slot): void |
54
|
|
|
{ |
55
|
|
|
if (isset($options['position'])) { |
56
|
|
|
$slot->setPosition($options['position']); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function addProductsToSlot(array $products, ProductBundleSlotInterface $slot): void |
61
|
|
|
{ |
62
|
|
|
foreach ($products as $product) { |
63
|
|
|
$this->addProductToSlot($product, $slot); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function addProductToSlot(ProductInterface $product, ProductBundleSlotInterface $slot): void |
68
|
|
|
{ |
69
|
|
|
$slot->addProduct($product); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|