|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Created by solutionDrive GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright 2018 solutionDrive GmbH |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace solutionDrive\SyliusProductBundlesPlugin\Fixture\Factory; |
|
12
|
|
|
|
|
13
|
|
|
use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleInterface; |
|
14
|
|
|
use solutionDrive\SyliusProductBundlesPlugin\Service\Options\ProductBundleSlotOptions; |
|
15
|
|
|
use solutionDrive\SyliusProductBundlesPlugin\Service\Options\ProductBundleSlotOptionsInterface; |
|
16
|
|
|
use solutionDrive\SyliusProductBundlesPlugin\Service\ProductBundleCreatorInterface; |
|
17
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; |
|
18
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
19
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
|
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
21
|
|
|
use Webmozart\Assert\Assert; |
|
22
|
|
|
|
|
23
|
|
|
class ProductBundleExampleFactory extends AbstractExampleFactory |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ProductBundleCreatorInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $productBundleCreator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ProductRepositoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $productRepository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var OptionsResolver |
|
37
|
|
|
*/ |
|
38
|
|
|
private $optionsResolver; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct( |
|
41
|
|
|
ProductBundleCreatorInterface $productBundleCreator, |
|
42
|
|
|
ProductRepositoryInterface $productRepository |
|
43
|
|
|
) { |
|
44
|
|
|
$this->productBundleCreator = $productBundleCreator; |
|
45
|
|
|
$this->productRepository = $productRepository; |
|
46
|
|
|
$this->optionsResolver = new OptionsResolver(); |
|
47
|
|
|
$this->configureOptions($this->optionsResolver); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
|
54
|
|
|
{ |
|
55
|
|
|
$resolver |
|
56
|
|
|
->setDefined('productCode') |
|
57
|
|
|
->setAllowedTypes('productCode', 'string') |
|
58
|
|
|
->setDefined('slots') |
|
59
|
|
|
->setAllowedTypes('slots', 'array'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function create(array $options = []): ProductBundleInterface |
|
66
|
|
|
{ |
|
67
|
|
|
$options = $this->optionsResolver->resolve($options); |
|
68
|
|
|
|
|
69
|
|
|
/** @var ProductInterface $product */ |
|
70
|
|
|
$product = $this->productRepository->findOneByCode($options['productCode']); |
|
71
|
|
|
|
|
72
|
|
|
Assert::notNull($product, sprintf('Bundle product %s could not be found', $options['productCode'])); |
|
73
|
|
|
$productBundleCreator = $this->productBundleCreator->createProductBundle($product); |
|
74
|
|
|
|
|
75
|
|
|
foreach ($options['slots'] as $slot) { |
|
76
|
|
|
$slotProducts = []; |
|
77
|
|
|
foreach ($slot['productCodes'] as $productCode) { |
|
78
|
|
|
$slotProduct = $this->productRepository->findOneByCode($productCode); |
|
79
|
|
|
Assert::notNull($slotProduct, sprintf('Slot product %s could not be found', $productCode)); |
|
80
|
|
|
$slotProducts[] = $slotProduct; |
|
81
|
|
|
} |
|
82
|
|
|
$slotOptions = $this->createSlotOptions($slot['options']); |
|
83
|
|
|
$productBundleCreator->addSlot($slot['name'], $slotOptions, $slotProducts); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $productBundleCreator->getProductBundle(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string[] $rawSlotOptions |
|
91
|
|
|
*/ |
|
92
|
|
|
private function createSlotOptions(array $rawSlotOptions = []): ProductBundleSlotOptionsInterface |
|
93
|
|
|
{ |
|
94
|
|
|
$slotOptions = new ProductBundleSlotOptions(); |
|
95
|
|
|
foreach ($rawSlotOptions as $optionName => $optionValue) { |
|
96
|
|
|
$setter = 'set' . ucfirst($optionName); |
|
97
|
|
|
|
|
98
|
|
|
Assert::methodExists($slotOptions, $setter, sprintf('Setter %s for ProductBundleSlotOptions is not defined', $setter)); |
|
99
|
|
|
|
|
100
|
|
|
if (method_exists($slotOptions, $setter = 'set' . ucfirst($optionName))) { |
|
101
|
|
|
$slotOptions->$setter($optionValue); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $slotOptions; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|