Skip to content

Commit

Permalink
[shopsys] increase speed of Product creation (#2903)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik authored Oct 26, 2023
1 parent 9bcda51 commit fe7497a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/src/Component/Router/FriendlyUrl/FriendlyUrlFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @property \App\Component\Router\FriendlyUrl\FriendlyUrlRepository $friendlyUrlRepository
* @property \App\Component\Router\FriendlyUrl\FriendlyUrlFactory $friendlyUrlFactory
* @method __construct(\Doctrine\ORM\EntityManagerInterface $em, \Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory $domainRouterFactory, \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlUniqueResultFactory $friendlyUrlUniqueResultFactory, \App\Component\Router\FriendlyUrl\FriendlyUrlRepository $friendlyUrlRepository, \Shopsys\FrameworkBundle\Component\Domain\Domain $domain, \App\Component\Router\FriendlyUrl\FriendlyUrlFactory $friendlyUrlFactory, \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlCacheKeyProvider $friendlyUrlCacheKeyProvider, \Symfony\Contracts\Cache\CacheInterface $mainFriendlyUrlSlugCache)
* @method resolveUniquenessOfFriendlyUrlAndFlush(\App\Component\Router\FriendlyUrl\FriendlyUrl $friendlyUrl, string $entityName)
* @method resolveUniquenessOfFriendlyUrl(\App\Component\Router\FriendlyUrl\FriendlyUrl $friendlyUrl, string $entityName)
* @method \App\Component\Router\FriendlyUrl\FriendlyUrl[] getAllByRouteNameAndEntityId(string $routeName, int $entityId)
* @method \App\Component\Router\FriendlyUrl\FriendlyUrl|null findMainFriendlyUrl(int $domainId, string $routeName, int $entityId)
* @method setFriendlyUrlAsMain(\App\Component\Router\FriendlyUrl\FriendlyUrl $mainFriendlyUrl)
Expand Down
9 changes: 3 additions & 6 deletions app/src/DataFixtures/Performance/ProductDataFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

class ProductDataFixture
{
private const BATCH_SIZE = 1000;

public const FIRST_PERFORMANCE_PRODUCT = 'first_performance_product';

private int $productTotalCount;
Expand Down Expand Up @@ -98,7 +96,6 @@ public function load(OutputInterface $output)
if ($productTemplate === false) {
$this->createVariants($variantCatnumsByMainVariantCatnum);
$productTemplate = reset($this->productTemplates);
$this->demoDataIterationCounter++;
}
$productData = $this->productDataFactory->createFromProduct($productTemplate);
$this->makeProductDataUnique($productData);
Expand All @@ -115,9 +112,7 @@ public function load(OutputInterface $output)
$this->productsByCatnum[$product->getCatnum()] = $product;
}

if ($this->countImported % self::BATCH_SIZE === 0) {
$this->cleanAndLoadReferences();
}
$this->cleanAndLoadReferences();

$this->countImported++;

Expand Down Expand Up @@ -196,6 +191,8 @@ private function makeProductDataUnique(ProductData $productData)
*/
private function getUniqueIndex()
{
$this->demoDataIterationCounter++;

return ' #' . $this->demoDataIterationCounter;
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/Model/Product/ProductFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function edit($productId, ProductData $productData)
$this->saveParameters($product, $productData->parameters);

if (!$product->isMainVariant()) {
$this->refreshProductManualInputPrices($product, $productData->manualInputPricesByPricingGroupId);
$this->productManualInputPriceFacade->refreshProductManualInputPrices($product, $productData->manualInputPricesByPricingGroupId);
}

if ($product->isMainVariant()) {
Expand Down Expand Up @@ -263,7 +263,7 @@ public function setAdditionalDataAfterCreate(BaseProduct $product, ProductData $

$this->saveParameters($product, $productData->parameters);
$this->createProductVisibilities($product);
$this->refreshProductManualInputPrices($product, $productData->manualInputPricesByPricingGroupId);
$this->productManualInputPriceFacade->refreshProductManualInputPrices($product, $productData->manualInputPricesByPricingGroupId);
$this->refreshProductAccessories($product, $productData->accessories);
$this->imageFacade->manageImages($product, $productData->images);
$this->productHiddenRecalculator->calculateHiddenForProduct($product);
Expand Down
1 change: 0 additions & 1 deletion app/src/Model/Stock/ProductStockFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ private function createProductStock(Product $product, Stock $stock): ProductStoc
{
$productStock = new ProductStock($stock, $product);
$this->em->persist($productStock);
$this->em->flush();

return $productStock;
}
Expand Down
18 changes: 13 additions & 5 deletions app/tests/App/Functional/Model/Cart/Watcher/CartWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Shopsys\FrameworkBundle\Model\Cart\Watcher\CartWatcher;
use Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserIdentifier;
use Shopsys\FrameworkBundle\Model\Pricing\Group\PricingGroupRepository;
use Shopsys\FrameworkBundle\Model\Pricing\Vat\VatFacade;
use Shopsys\FrameworkBundle\Model\Product\Pricing\ProductManualInputPriceFacade;
use Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPriceCalculationForCustomerUser;
Expand Down Expand Up @@ -51,9 +52,15 @@ class CartWatcherTest extends TransactionFunctionalTestCase
*/
private VatFacade $vatFacade;

/**
* @inject
*/
private PricingGroupRepository $pricingGroupRepository;

public function testGetModifiedPriceItemsAndUpdatePrices()
{
$customerUserIdentifier = new CustomerUserIdentifier('randomString');
/** @var \App\Model\Product\Product $product */
$product = $this->getReference(ProductDataFixture::PRODUCT_PREFIX . '1');

$productPrice = $this->productPriceCalculationForCustomerUser->calculatePriceForCurrentUser($product);
Expand All @@ -64,12 +71,13 @@ public function testGetModifiedPriceItemsAndUpdatePrices()
$modifiedItems1 = $this->cartWatcher->getModifiedPriceItemsAndUpdatePrices($cart);
$this->assertEmpty($modifiedItems1);

$pricingGroup = $this->getReferenceForDomain(
PricingGroupDataFixture::PRICING_GROUP_ORDINARY,
Domain::FIRST_DOMAIN_ID,
);
$pricesByPricingGroupId = [];

foreach ($this->pricingGroupRepository->getAll() as $pricingGroup) {
$pricesByPricingGroupId[$pricingGroup->getId()] = Money::create(10);
}

$this->manualInputPriceFacade->refresh($product, $pricingGroup, Money::create(10));
$this->manualInputPriceFacade->refreshProductManualInputPrices($product, $pricesByPricingGroupId);

$modifiedItems2 = $this->cartWatcher->getModifiedPriceItemsAndUpdatePrices($cart);
$this->assertNotEmpty($modifiedItems2);
Expand Down

0 comments on commit fe7497a

Please sign in to comment.