1212
1313namespace BitBag \SyliusElasticsearchPlugin \PropertyBuilder ;
1414
15+ use BitBag \SyliusElasticsearchPlugin \EntityRepository \ProductVariantRepositoryInterface ;
16+ use BitBag \SyliusElasticsearchPlugin \PropertyBuilder \Mapper \ProductTaxonsMapperInterface ;
1517use FOS \ElasticaBundle \Event \TransformEvent ;
1618use Sylius \Component \Core \Model \ProductInterface ;
17- use Sylius \Component \Core \Repository \ProductRepositoryInterface ;
1819use Sylius \Component \Product \Model \ProductOptionInterface ;
20+ use Sylius \Component \Product \Model \ProductOptionValueInterface ;
21+ use Sylius \Component \Resource \Repository \RepositoryInterface ;
1922
2023final class OptionTaxonsBuilder extends AbstractBuilder
2124{
2225 /**
23- * @var ProductRepositoryInterface
26+ * @var RepositoryInterface
2427 */
25- private $ productRepository ;
28+ private $ productOptionValueRepository ;
29+
30+ /**
31+ * @var ProductVariantRepositoryInterface
32+ */
33+ private $ productVariantRepository ;
34+
35+ /**
36+ * @var ProductTaxonsMapperInterface
37+ */
38+ private $ productTaxonsMapper ;
2639
2740 /**
2841 * @var string
@@ -40,18 +53,24 @@ final class OptionTaxonsBuilder extends AbstractBuilder
4053 private $ excludedOptions ;
4154
4255 /**
43- * @param ProductRepositoryInterface $productRepository
56+ * @param RepositoryInterface $productOptionValueRepository
57+ * @param ProductVariantRepositoryInterface $productVariantRepository
58+ * @param ProductTaxonsMapperInterface $productTaxonsMapper
4459 * @param string $optionProperty
4560 * @param string $taxonsProperty
4661 * @param array $excludedOptions
4762 */
4863 public function __construct (
49- ProductRepositoryInterface $ productRepository ,
64+ RepositoryInterface $ productOptionValueRepository ,
65+ ProductVariantRepositoryInterface $ productVariantRepository ,
66+ ProductTaxonsMapperInterface $ productTaxonsMapper ,
5067 string $ optionProperty ,
5168 string $ taxonsProperty ,
5269 array $ excludedOptions = []
5370 ) {
54- $ this ->productRepository = $ productRepository ;
71+ $ this ->productOptionValueRepository = $ productOptionValueRepository ;
72+ $ this ->productVariantRepository = $ productVariantRepository ;
73+ $ this ->productTaxonsMapper = $ productTaxonsMapper ;
5574 $ this ->optionProperty = $ optionProperty ;
5675 $ this ->taxonsProperty = $ taxonsProperty ;
5776 $ this ->excludedOptions = $ excludedOptions ;
@@ -64,30 +83,23 @@ public function buildProperty(TransformEvent $event): void
6483 {
6584 $ documentProductOption = $ event ->getObject ();
6685
67- if (
68- !$ documentProductOption instanceof ProductOptionInterface ||
69- in_array ($ documentProductOption ->getCode (), $ this ->excludedOptions )
86+ if (!$ documentProductOption instanceof ProductOptionInterface
87+ || in_array ($ documentProductOption ->getCode (), $ this ->excludedOptions )
7088 ) {
7189 return ;
7290 }
7391
7492 $ document = $ event ->getDocument ();
75- $ products = $ this ->productRepository ->findAll ();
93+ $ optionValues = $ this ->productOptionValueRepository ->findAll ();
7694 $ taxons = [];
7795
78- /** @var ProductInterface $product */
79- foreach ($ products as $ product ) {
80- foreach ($ product ->getVariants () as $ productVariant ) {
81- foreach ($ productVariant ->getOptionValues () as $ productOptionValue ) {
82- if ($ documentProductOption === $ productOptionValue ->getOption ()) {
83- foreach ($ product ->getTaxons () as $ taxon ) {
84- $ code = $ taxon ->getCode ();
85- if (!in_array ($ code , $ taxons )) {
86- $ taxons [] = $ code ;
87- }
88- }
89- }
90- }
96+ /** @var ProductOptionValueInterface $optionValue */
97+ foreach ($ optionValues as $ optionValue ) {
98+ $ option = $ optionValue ->getOption ();
99+ if ($ documentProductOption === $ option ) {
100+ /** @var ProductInterface $product */
101+ $ product = $ this ->productVariantRepository ->findOneByOptionValue ($ optionValue )->getProduct ();
102+ $ taxons = $ this ->productTaxonsMapper ->mapToUniqueCodes ($ product );
91103 }
92104 }
93105
0 commit comments