Skip to content

Commit

Permalink
Merge branch 'Sylius:2.0' into 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bunnyshellcloud authored Jan 4, 2025
2 parents 29b6934 + c2a89d1 commit fdfad57
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 17 deletions.
1 change: 1 addition & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,7 @@ and maintainability of the application and also follows Symfony's best practices
| `sylius.currency_converter` | `sylius.converter.currency` |
| `sylius.currency_name_converter` | `sylius.converter.currency_name` |
| **InventoryBundle** | |
| `sylius.availability_checker` | `sylius.checker.inventory.availability` |
| `sylius.availability_checker.default` | `sylius.checker.inventory.availability` |
| **LocaleBundle** | |
| `Sylius\Bundle\LocaleBundle\Context\RequestHeaderBasedLocaleContext` | `sylius.context.locale.request_header_based` |
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{% extends '@SyliusAttribute/Types/percent.html.twig' %}
{% extends '@SyliusAttribute/Types/select.html.twig' %}
20 changes: 7 additions & 13 deletions src/Sylius/Bundle/OrderBundle/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Bundle\OrderBundle\Controller;

use Sylius\Bundle\OrderBundle\Resetter\CartChangesResetterInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Order\Context\CartContextInterface;
use Sylius\Component\Order\Model\OrderInterface;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function saveAction(Request $request): Response
}

if ($form->isSubmitted() && !$form->isValid()) {
$this->resetChangesOnCart($resource);
$this->getCartResetter()->resetChanges($resource);
$this->addFlash('error', 'sylius.cart.not_recalculated');
}

Expand Down Expand Up @@ -130,18 +131,6 @@ protected function addFlash(string $type, mixed $message): void
$flashBag->add($type, $message);
}

private function resetChangesOnCart(OrderInterface $cart): void
{
if (!$this->manager->contains($cart)) {
return;
}

$this->manager->refresh($cart);
foreach ($cart->getItems() as $item) {
$this->manager->refresh($item);
}
}

protected function getCurrentCart(): OrderInterface
{
return $this->getContext()->getCart();
Expand All @@ -157,6 +146,11 @@ protected function getOrderRepository(): OrderRepositoryInterface
return $this->get('sylius.repository.order');
}

protected function getCartResetter(): CartChangesResetterInterface
{
return $this->get('sylius.resetter.cart_changes');
}

protected function getEventDispatcher(): EventDispatcherInterface
{
return $this->container->get('event_dispatcher');
Expand Down
44 changes: 44 additions & 0 deletions src/Sylius/Bundle/OrderBundle/Resetter/CartChangesResetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\OrderBundle\Resetter;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use Sylius\Component\Order\Model\OrderInterface;

final class CartChangesResetter implements CartChangesResetterInterface
{
public function __construct(private readonly EntityManagerInterface $manager)
{
}

public function resetChanges(OrderInterface $cart): void
{
if (!$this->manager->contains($cart)) {
return;
}

$uow = $this->manager->getUnitOfWork();

foreach ($cart->getItems() as $item) {
foreach ($item->getUnits() as $unit) {
if ($uow->getEntityState($unit) === UnitOfWork::STATE_NEW) {
$item->removeUnit($unit);
}
}
$this->manager->refresh($item);
}
$this->manager->refresh($cart);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\OrderBundle\Resetter;

use Sylius\Component\Order\Model\OrderInterface;

interface CartChangesResetterInterface
{
public function resetChanges(OrderInterface $cart): void;
}
5 changes: 5 additions & 0 deletions src/Sylius/Bundle/OrderBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,10 @@

<service id="sylius.factory.add_to_cart_command" class="Sylius\Bundle\OrderBundle\Factory\AddToCartCommandFactory" />
<service id="Sylius\Bundle\OrderBundle\Factory\AddToCartCommandFactoryInterface" alias="sylius.factory.add_to_cart_command" />

<service id="sylius.resetter.cart_changes" class="Sylius\Bundle\OrderBundle\Resetter\CartChangesResetter" public="true">
<argument type="service" id="sylius.manager.order" />
</service>
<service id="Sylius\Bundle\OrderBundle\Resetter\CartChangesResetterInterface" alias="sylius.resetter.cart_changes" />
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\OrderBundle\ChangesResetter;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\OrderBundle\Resetter\CartChangesResetter;
use Sylius\Component\Order\Model\OrderInterface;
use Sylius\Component\Order\Model\OrderItemInterface;
use Sylius\Component\Order\Model\OrderItemUnitInterface;

final class CartChangesResetterSpec extends ObjectBehavior
{
function let(EntityManagerInterface $manager): void
{
$this->beConstructedWith($manager);
}

function it_is_initializable(): void
{
$this->shouldHaveType(CartChangesResetter::class);
}

function it_does_nothing_if_cart_is_not_managed(
EntityManagerInterface $manager,
OrderInterface $cart
): void {
$manager->contains($cart)->willReturn(false);

$manager->refresh($cart)->shouldNotBeCalled();

$this->resetChanges($cart);
}

function it_resets_changes_for_cart_items_and_units(
EntityManagerInterface $manager,
UnitOfWork $unitOfWork,
OrderInterface $cart,
OrderItemInterface $item,
OrderItemUnitInterface $unitNew,
OrderItemUnitInterface $unitExisting,
Collection $itemsCollection
): void {
$manager->contains($cart)->willReturn(true);
$manager->getUnitOfWork()->willReturn($unitOfWork);

$cart->getItems()->willReturn(new ArrayCollection([$item->getWrappedObject()]));

$item->getUnits()->willReturn(new ArrayCollection([$unitNew->getWrappedObject(), $unitExisting->getWrappedObject()]));

$unitOfWork->getEntityState($unitNew)->willReturn(UnitOfWork::STATE_NEW);
$unitOfWork->getEntityState($unitExisting)->willReturn(UnitOfWork::STATE_MANAGED);

$item->removeUnit($unitNew)->shouldBeCalled();
$manager->refresh($item)->shouldBeCalled();
$manager->refresh($cart)->shouldBeCalled();

$this->resetChanges($cart);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4388,7 +4388,7 @@
"businessActivitySummary": {
"totalSales": 15000,
"paidOrdersCount": 6,
"newCustomersCount": 1,
"newCustomersCount": 0,
"averageOrderValue": 2500
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"businessActivitySummary": {
"totalSales": 15000,
"paidOrdersCount": 6,
"newCustomersCount": 1,
"newCustomersCount": 0,
"averageOrderValue": 2500
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"businessActivitySummary": {
"totalSales": 15000,
"paidOrdersCount": 6,
"newCustomersCount": 1,
"newCustomersCount": 0,
"averageOrderValue": 2500
}
}

0 comments on commit fdfad57

Please sign in to comment.