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
|
|
|
/** @var ProductBundleCreatorInterface */ |
26
|
|
|
private $productBundleCreator; |
27
|
|
|
|
28
|
|
|
/** @var ProductRepositoryInterface */ |
29
|
|
|
private $productRepository; |
30
|
|
|
|
31
|
|
|
/** @var OptionsResolver */ |
32
|
|
|
private $optionsResolver; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
ProductBundleCreatorInterface $productBundleCreator, |
36
|
|
|
ProductRepositoryInterface $productRepository |
37
|
|
|
) { |
38
|
|
|
$this->productBundleCreator = $productBundleCreator; |
39
|
|
|
$this->productRepository = $productRepository; |
40
|
|
|
$this->optionsResolver = new OptionsResolver(); |
41
|
|
|
$this->configureOptions($this->optionsResolver); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
48
|
|
|
{ |
49
|
|
|
$resolver |
50
|
|
|
->setDefined('productCode') |
51
|
|
|
->setAllowedTypes('productCode', 'string') |
52
|
|
|
->setDefined('slots') |
53
|
|
|
->setAllowedTypes('slots', 'array'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function create(array $options = []): ?ProductBundleInterface |
60
|
|
|
{ |
61
|
|
|
$options = $this->optionsResolver->resolve($options); |
62
|
|
|
|
63
|
|
|
/** @var ProductInterface $product */ |
64
|
|
|
$product = $this->productRepository->findOneByCode($options['productCode']); |
65
|
|
|
|
66
|
|
|
Assert::notNull($product, sprintf('Bundle product %s could not be found', $options['productCode'])); |
67
|
|
|
$this->productBundleCreator->createProductBundle($product); |
68
|
|
|
|
69
|
|
|
foreach ($options['slots'] as $slot) { |
70
|
|
|
$slotProducts = []; |
71
|
|
|
foreach ($slot['productCodes'] as $productCode) { |
72
|
|
|
$slotProduct = $this->productRepository->findOneByCode($productCode); |
73
|
|
|
Assert::notNull($slotProduct, sprintf('Slot product %s could not be found', $productCode)); |
74
|
|
|
$slotProducts[] = $slotProduct; |
75
|
|
|
} |
76
|
|
|
$slotOptions = $this->createSlotOptions($slot['options']); |
77
|
|
|
$this->productBundleCreator->addSlot($slot['name'], $slotOptions, $slotProducts); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->productBundleCreator->getProductBundle(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string[] $rawSlotOptions |
85
|
|
|
*/ |
86
|
|
|
private function createSlotOptions(array $rawSlotOptions = []): ProductBundleSlotOptionsInterface |
87
|
|
|
{ |
88
|
|
|
$slotOptions = new ProductBundleSlotOptions(); |
89
|
|
|
foreach ($rawSlotOptions as $optionName => $optionValue) { |
90
|
|
|
$setter = 'set' . ucfirst($optionName); |
91
|
|
|
|
92
|
|
|
Assert::methodExists($slotOptions, $setter, sprintf('Setter %s for ProductBundleSlotOptions is not defined', $setter)); |
93
|
|
|
|
94
|
|
|
if (method_exists($slotOptions, $setter = 'set' . ucfirst($optionName))) { |
95
|
|
|
$slotOptions->$setter($optionValue); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $slotOptions; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|