1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\Product\Generator; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Product\Checker\ProductVariantsParityCheckerInterface; |
15
|
|
|
use Sylius\Component\Product\Factory\ProductVariantFactoryInterface; |
16
|
|
|
use Sylius\Component\Product\Model\ProductInterface; |
17
|
|
|
use Sylius\Component\Product\Model\ProductVariantInterface; |
18
|
|
|
use Webmozart\Assert\Assert; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
22
|
|
|
* @author Łukasz Chruściel <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class ProductVariantGenerator implements ProductVariantGeneratorInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ProductVariantFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $productVariantFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CartesianSetBuilder |
33
|
|
|
*/ |
34
|
|
|
private $setBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ProductVariantsParityCheckerInterface |
38
|
|
|
*/ |
39
|
|
|
private $variantsParityChecker; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ProductVariantFactoryInterface $productVariantFactory |
43
|
|
|
* @param ProductVariantsParityCheckerInterface $variantsParityChecker |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
ProductVariantFactoryInterface $productVariantFactory, |
|
|
|
|
47
|
|
|
ProductVariantsParityCheckerInterface $variantsParityChecker |
|
|
|
|
48
|
|
|
) { |
49
|
|
|
$this->productVariantFactory = $productVariantFactory; |
50
|
|
|
$this->setBuilder = new CartesianSetBuilder(); |
51
|
|
|
$this->variantsParityChecker = $variantsParityChecker; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function generate(ProductInterface $product) |
58
|
|
|
{ |
59
|
|
|
Assert::true($product->hasOptions(), 'Cannot generate variants for an object without options.'); |
60
|
|
|
|
61
|
|
|
$optionSet = []; |
62
|
|
|
$optionMap = []; |
63
|
|
|
|
64
|
|
|
foreach ($product->getOptions() as $key => $option) { |
65
|
|
|
foreach ($option->getValues() as $value) { |
66
|
|
|
$optionSet[$key][] = $value->getId(); |
67
|
|
|
$optionMap[$value->getId()] = $value; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$permutations = $this->setBuilder->build($optionSet); |
72
|
|
|
|
73
|
|
|
foreach ($permutations as $permutation) { |
74
|
|
|
$variant = $this->createVariant($product, $optionMap, $permutation); |
75
|
|
|
|
76
|
|
|
if (!$this->variantsParityChecker->checkParity($variant, $product)) { |
77
|
|
|
$product->addVariant($variant); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param ProductInterface $product |
84
|
|
|
* @param array $optionMap |
85
|
|
|
* @param mixed $permutation |
86
|
|
|
* |
87
|
|
|
* @return ProductVariantInterface |
88
|
|
|
*/ |
89
|
|
|
protected function createVariant(ProductInterface $product, array $optionMap, $permutation) |
90
|
|
|
{ |
91
|
|
|
/** @var ProductVariantInterface $variant */ |
92
|
|
|
$variant = $this->productVariantFactory->createForProduct($product); |
93
|
|
|
$this->addOptionValue($variant, $optionMap, $permutation); |
94
|
|
|
|
95
|
|
|
return $variant; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param ProductVariantInterface $variant |
100
|
|
|
* @param array $optionMap |
101
|
|
|
* @param mixed $permutation |
102
|
|
|
*/ |
103
|
|
|
private function addOptionValue(ProductVariantInterface $variant, array $optionMap, $permutation) |
104
|
|
|
{ |
105
|
|
|
if (!is_array($permutation)) { |
106
|
|
|
$variant->addOptionValue($optionMap[$permutation]); |
107
|
|
|
|
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach ($permutation as $id) { |
112
|
|
|
$variant->addOptionValue($optionMap[$id]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.