|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.io and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusVueStorefrontPlugin\Sylius\Repository; |
|
14
|
|
|
|
|
15
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Model\Request\Cart\CartItem\ConfigurableItemOption; |
|
16
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
|
17
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
|
18
|
|
|
use Sylius\Component\Product\Model\ProductOptionValueInterface; |
|
19
|
|
|
|
|
20
|
|
|
final class ProductVariantRepository implements ProductVariantRepositoryInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var ProductRepositoryInterface */ |
|
23
|
|
|
private $productRepository; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(ProductRepositoryInterface $productRepository) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->productRepository = $productRepository; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** @param ConfigurableItemOption[] $configurableItemOptions */ |
|
31
|
|
|
public function getVariantForOptionValuesBySku(string $sku, array $configurableItemOptions): ?ProductVariantInterface |
|
32
|
|
|
{ |
|
33
|
|
|
$product = $this->productRepository->findOneByCode($sku); |
|
34
|
|
|
|
|
35
|
|
|
/** @var ProductVariantInterface $productVariant */ |
|
36
|
|
|
foreach ($product->getVariants() as $productVariant) { |
|
37
|
|
|
$productVariantOptionValues = $productVariant->getOptionValues()->map( |
|
38
|
|
|
static function (ProductOptionValueInterface $productOptionValue) { |
|
39
|
|
|
return [ |
|
40
|
|
|
'option_value' => $productOptionValue->getId(), |
|
41
|
|
|
'option_id' => (string) $productOptionValue->getOption()->getId(), |
|
42
|
|
|
]; |
|
43
|
|
|
}) |
|
44
|
|
|
; |
|
45
|
|
|
|
|
46
|
|
|
if ($this->areArraysEqual( |
|
47
|
|
|
$this->convertConfigurableItemOptionsToArray($configurableItemOptions), |
|
48
|
|
|
$productVariantOptionValues->toArray() |
|
49
|
|
|
)) { |
|
50
|
|
|
return $productVariant; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function convertConfigurableItemOptionsToArray(array $configurableItemOptions): array |
|
58
|
|
|
{ |
|
59
|
|
|
$itemOptions = []; |
|
60
|
|
|
|
|
61
|
|
|
/** @var ConfigurableItemOption $configurableItemOption */ |
|
62
|
|
|
foreach ($configurableItemOptions as $configurableItemOption) { |
|
63
|
|
|
$itemOptions[] = [ |
|
64
|
|
|
'option_value' => $configurableItemOption->option_value, |
|
65
|
|
|
'option_id' => $configurableItemOption->option_id, |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $itemOptions; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function areArraysEqual(array $first, array $second): bool |
|
73
|
|
|
{ |
|
74
|
|
|
if (count($first) !== count($second)) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
foreach ($second as $key => $value) { |
|
78
|
|
|
if (!in_array($value, $first, false)) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (count(array_keys($first, $value, false)) !== count(array_keys($second, $value, false))) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|