Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CC-31551 Fixed performance issue in product search attribute fetching…
…. (#10630) CC-31551 Fixed performance issue in product search attribute fetching.
- Loading branch information
Showing
24 changed files
with
830 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"include": { | ||
"spryker/transfer": "Provides transfer objects decimal property type functionality." | ||
"spryker/transfer": "Provides transfer objects definition with strict types." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...pryker/Zed/ProductSearch/Business/Expander/LocalizedProductSearchAttributeKeyExpander.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Zed\ProductSearch\Business\Expander; | ||
|
||
use Generated\Shared\Transfer\LocalizedProductSearchAttributeKeyTransfer; | ||
use Generated\Shared\Transfer\ProductSearchAttributeCollectionTransfer; | ||
use Generated\Shared\Transfer\ProductSearchAttributeTransfer; | ||
use Spryker\Shared\ProductSearch\Code\KeyBuilder\GlossaryKeyBuilderInterface; | ||
use Spryker\Zed\ProductSearch\Dependency\Facade\ProductSearchToGlossaryInterface; | ||
use Spryker\Zed\ProductSearch\Dependency\Facade\ProductSearchToLocaleInterface; | ||
|
||
class LocalizedProductSearchAttributeKeyExpander implements LocalizedProductSearchAttributeKeyExpanderInterface | ||
{ | ||
/** | ||
* @var \Spryker\Zed\ProductSearch\Dependency\Facade\ProductSearchToLocaleInterface | ||
*/ | ||
protected ProductSearchToLocaleInterface $localeFacade; | ||
|
||
/** | ||
* @var \Spryker\Zed\ProductSearch\Dependency\Facade\ProductSearchToGlossaryInterface | ||
*/ | ||
protected ProductSearchToGlossaryInterface $glossaryFacade; | ||
|
||
/** | ||
* @var \Spryker\Shared\ProductSearch\Code\KeyBuilder\GlossaryKeyBuilderInterface | ||
*/ | ||
protected GlossaryKeyBuilderInterface $glossaryKeyBuilder; | ||
|
||
/** | ||
* @param \Spryker\Zed\ProductSearch\Dependency\Facade\ProductSearchToLocaleInterface $localeFacade | ||
* @param \Spryker\Zed\ProductSearch\Dependency\Facade\ProductSearchToGlossaryInterface $glossaryFacade | ||
* @param \Spryker\Shared\ProductSearch\Code\KeyBuilder\GlossaryKeyBuilderInterface $glossaryKeyBuilder | ||
*/ | ||
public function __construct( | ||
ProductSearchToLocaleInterface $localeFacade, | ||
ProductSearchToGlossaryInterface $glossaryFacade, | ||
GlossaryKeyBuilderInterface $glossaryKeyBuilder | ||
) { | ||
$this->localeFacade = $localeFacade; | ||
$this->glossaryFacade = $glossaryFacade; | ||
$this->glossaryKeyBuilder = $glossaryKeyBuilder; | ||
} | ||
|
||
/** | ||
* @param \Generated\Shared\Transfer\ProductSearchAttributeCollectionTransfer $productSearchAttributeCollectionTransfer | ||
* | ||
* @return \Generated\Shared\Transfer\ProductSearchAttributeCollectionTransfer | ||
*/ | ||
public function expandProductSearchAttributeCollectionWithLocalizedKeys( | ||
ProductSearchAttributeCollectionTransfer $productSearchAttributeCollectionTransfer | ||
): ProductSearchAttributeCollectionTransfer { | ||
$localeTransfers = $this->localeFacade->getLocaleCollection(); | ||
$glossaryKeys = $this->extractGlossaryKeysFromProductSearchAttributeCollectionTransfer($productSearchAttributeCollectionTransfer); | ||
|
||
$translationTransfers = $this->glossaryFacade->getTranslationsByGlossaryKeysAndLocaleTransfers($glossaryKeys, $localeTransfers); | ||
$translationsIndexedByKeyAndLocale = $this->getTranslationValuesIndexedByGlossaryKeyAndLocale($translationTransfers); | ||
|
||
foreach ($productSearchAttributeCollectionTransfer->getProductSearchAttributes() as $productSearchAttributeTransfer) { | ||
$this->expandProductSearchAttributeWithLocalizedKeys($localeTransfers, $translationsIndexedByKeyAndLocale, $productSearchAttributeTransfer); | ||
} | ||
|
||
return $productSearchAttributeCollectionTransfer; | ||
} | ||
|
||
/** | ||
* @param list<\Generated\Shared\Transfer\LocaleTransfer> $localeTransfers | ||
* @param array<int, array<string, string>> $translationsIndexedByKeyAndLocale | ||
* @param \Generated\Shared\Transfer\ProductSearchAttributeTransfer $productSearchAttributeTransfer | ||
* | ||
* @return \Generated\Shared\Transfer\ProductSearchAttributeTransfer | ||
*/ | ||
protected function expandProductSearchAttributeWithLocalizedKeys( | ||
array $localeTransfers, | ||
array $translationsIndexedByKeyAndLocale, | ||
ProductSearchAttributeTransfer $productSearchAttributeTransfer | ||
): ProductSearchAttributeTransfer { | ||
foreach ($localeTransfers as $localeTransfer) { | ||
$translation = $translationsIndexedByKeyAndLocale[$localeTransfer->getIdLocaleOrFail()][$productSearchAttributeTransfer->getKeyOrFail()] ?? null; | ||
$localizedProductSearchAttributeKeyTransfer = new LocalizedProductSearchAttributeKeyTransfer(); | ||
$localizedProductSearchAttributeKeyTransfer | ||
->setLocaleName($localeTransfer->getLocaleNameOrFail()) | ||
->setKeyTranslation($translation); | ||
|
||
$productSearchAttributeTransfer->addLocalizedKey($localizedProductSearchAttributeKeyTransfer); | ||
} | ||
|
||
return $productSearchAttributeTransfer; | ||
} | ||
|
||
/** | ||
* @param list<\Generated\Shared\Transfer\TranslationTransfer> $translationTransfers | ||
* | ||
* @return array<int, array<string, string>> | ||
*/ | ||
protected function getTranslationValuesIndexedByGlossaryKeyAndLocale(array $translationTransfers): array | ||
{ | ||
$translationsIndexedByKeyAndLocale = []; | ||
|
||
foreach ($translationTransfers as $translationTransfer) { | ||
$key = $translationTransfer->getGlossaryKeyOrFail()->getKeyOrFail(); | ||
$translationsIndexedByKeyAndLocale[$translationTransfer->getFkLocaleOrFail()][$key] = $translationTransfer->getValueOrFail(); | ||
} | ||
|
||
return $translationsIndexedByKeyAndLocale; | ||
} | ||
|
||
/** | ||
* @param \Generated\Shared\Transfer\ProductSearchAttributeCollectionTransfer $productSearchAttributeCollectionTransfer | ||
* | ||
* @return list<string> | ||
*/ | ||
protected function extractGlossaryKeysFromProductSearchAttributeCollectionTransfer( | ||
ProductSearchAttributeCollectionTransfer $productSearchAttributeCollectionTransfer | ||
): array { | ||
$glossaryKeys = []; | ||
|
||
foreach ($productSearchAttributeCollectionTransfer->getProductSearchAttributes() as $productSearchAttributeTransfer) { | ||
if (!$productSearchAttributeTransfer->getKey()) { | ||
continue; | ||
} | ||
|
||
$glossaryKeys[] = $productSearchAttributeTransfer->getKeyOrFail(); | ||
} | ||
|
||
return $glossaryKeys; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...d/ProductSearch/Business/Expander/LocalizedProductSearchAttributeKeyExpanderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Zed\ProductSearch\Business\Expander; | ||
|
||
use Generated\Shared\Transfer\ProductSearchAttributeCollectionTransfer; | ||
|
||
interface LocalizedProductSearchAttributeKeyExpanderInterface | ||
{ | ||
/** | ||
* @param \Generated\Shared\Transfer\ProductSearchAttributeCollectionTransfer $productSearchAttributeCollectionTransfer | ||
* | ||
* @return \Generated\Shared\Transfer\ProductSearchAttributeCollectionTransfer | ||
*/ | ||
public function expandProductSearchAttributeCollectionWithLocalizedKeys( | ||
ProductSearchAttributeCollectionTransfer $productSearchAttributeCollectionTransfer | ||
): ProductSearchAttributeCollectionTransfer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.