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->getName(), $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
|
|
|
/** @var ProductBundleInterface $productBundle */ |
87
|
|
|
$productBundle = $productBundleCreator->getProductBundle(); |
88
|
|
|
$productBundle->setCode($product->getCode()); |
89
|
|
|
|
90
|
|
|
return $productBundle; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string[] $rawSlotOptions |
95
|
|
|
*/ |
96
|
|
|
private function createSlotOptions(array $rawSlotOptions = []): ProductBundleSlotOptionsInterface |
97
|
|
|
{ |
98
|
|
|
$slotOptions = new ProductBundleSlotOptions(); |
99
|
|
|
foreach ($rawSlotOptions as $optionName => $optionValue) { |
100
|
|
|
$setter = 'set' . ucfirst($optionName); |
101
|
|
|
|
102
|
|
|
Assert::methodExists($slotOptions, $setter, sprintf('Setter %s for ProductBundleSlotOptions is not defined', $setter)); |
103
|
|
|
|
104
|
|
|
if (method_exists($slotOptions, $setter = 'set' . ucfirst($optionName))) { |
105
|
|
|
$slotOptions->$setter($optionValue); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $slotOptions; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.