Skip to content

Commit

Permalink
Merge pull request #2265 from dpfaffenbauer/issues/2245
Browse files Browse the repository at this point in the history
Performance optimization
  • Loading branch information
dpfaffenbauer committed May 2, 2023
2 parents e5f0661 + 09e44e0 commit f297b5c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 41 deletions.
3 changes: 3 additions & 0 deletions src/CoreShop/Bundle/OrderBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ services:
arguments:
- '@CoreShop\Component\Order\Factory\OrderItemFactory.inner'

CoreShop\Component\Resource\TokenGenerator\UniqueTokenGenerator: ~

CoreShop\Component\Order\Factory\OrderFactory:
decorates: coreshop.factory.order
decoration_priority: 256
public: false
arguments:
- '@CoreShop\Component\Order\Factory\OrderFactory.inner'
- '@CoreShop\Component\Resource\TokenGenerator\UniqueTokenGenerator'

CoreShop\Bundle\OrderBundle\Factory\AddToCartFactoryInterface: '@CoreShop\Bundle\OrderBundle\Factory\AddToCartFactory'
CoreShop\Bundle\OrderBundle\Factory\AddToCartFactory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CoreShop\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use CoreShop\Component\Sequence\Model\SequenceInterface;
use CoreShop\Component\Sequence\Repository\SequenceRepositoryInterface;
use Doctrine\DBAL\LockMode;

class SequenceRepository extends EntityRepository implements SequenceRepositoryInterface
{
Expand All @@ -30,6 +31,7 @@ public function findForType(string $type): ?SequenceInterface
->andWhere('o.type = :type')
->setParameter('type', $type)
->getQuery()
->setLockMode(LockMode::PESSIMISTIC_WRITE)
->getOneOrNullResult()
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function onKernelResponse(ResponseEvent $event): void
return;
}

if (0 !== $list->getId()) {
if (null !== $list->getId()) {
$session = $request->getSession();

$session->set(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public function commitOrder(OrderInterface $order): void
$order->setPaymentState(OrderPaymentStates::STATE_NEW);
$order->setInvoiceState(OrderInvoiceStates::STATE_NEW);

$tokenGenerator = new UniqueTokenGenerator();
$order->setToken($tokenGenerator->generate(10));

$this->cartManager->persistCart($order);

$originalShippingAddress = $order->hasShippableItems() === false ? $order->getInvoiceAddress() : $order->getShippingAddress();
Expand Down
4 changes: 4 additions & 0 deletions src/CoreShop/Component/Order/Factory/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
namespace CoreShop\Component\Order\Factory;

use CoreShop\Component\Resource\Factory\FactoryInterface;
use CoreShop\Component\Resource\TokenGenerator\UniqueTokenGenerator;

class OrderFactory implements FactoryInterface
{
public function __construct(
private FactoryInterface $cartFactory,
private UniqueTokenGenerator $tokenGenerator,
private int $tokenLength = 10
) {
}

Expand All @@ -32,6 +35,7 @@ public function createNew()
$cart = $this->cartFactory->createNew();
$cart->setKey(uniqid('cart', true));
$cart->setPublished(true);
$cart->setToken($this->tokenGenerator->generate($this->tokenLength));

return $cart;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public function __construct(

public function getNextSequenceForType(string $type): int
{
$this->entityManager->beginTransaction();

$sequence = $this->getSequence($type);
$sequence->incrementIndex();

$this->entityManager->persist($sequence);
$this->entityManager->flush();
$this->entityManager->commit();

return $sequence->getIndex();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,47 @@ public function persist(StorageListInterface $storageList): void
'path' => 'storage-list',
]);

$this->connection->transactional(function () use ($storageList, $folder) {
VersionHelper::useVersioning(function () use ($storageList, $folder) {
$tempItems = $storageList->getItems();
VersionHelper::useVersioning(function () use ($storageList, $folder) {
$tempItems = $storageList->getItems();

if (!$storageList->getId()) {
$storageList->setItems([]);

/**
* @psalm-suppress DocblockTypeContradiction
*/
if (!$storageList->getParent()) {
$storageList->setParent($folder);
}

$storageList->save();
}
if (!$storageList->getId()) {
$storageList->setItems([]);

/**
* @var AbstractPimcoreModel $item
* @psalm-suppress DocblockTypeContradiction
*/
foreach ($tempItems as $index => $item) {
$item->setParent(
$this->folderCreationService->createFolderForResource(
$item,
['prefix' => $storageList->getFullPath()],
),
);
$item->setPublished(true);
$item->setKey($index + 1);
$item->save();
if (!$storageList->getParent()) {
$storageList->setParent($folder);
}

$storageList->setItems($tempItems);
$storageList->save();
}

/**
* @var AbstractPimcoreModel $storageListItem
*/
foreach ($storageList->getItems() as $storageListItem) {
$storageListItem->save();
}
/**
* @var AbstractPimcoreModel $item
*/
foreach ($tempItems as $index => $item) {
$item->setParent(
$this->folderCreationService->createFolderForResource(
$item,
['prefix' => $storageList->getFullPath()],
),
);
$item->setPublished(true);
$item->setKey($index + 1);
$item->save();
}

$storageList->save();
}, false);
});
$storageList->setItems($tempItems);

/**
* @var AbstractPimcoreModel $storageListItem
*/
foreach ($storageList->getItems() as $storageListItem) {
$storageListItem->save();
}

$storageList->save();
}, false);
}
}

0 comments on commit f297b5c

Please sign in to comment.