Skip to content

Commit

Permalink
PR review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 14, 2024
1 parent 52b750e commit 648662d
Show file tree
Hide file tree
Showing 23 changed files with 315 additions and 116 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"sylius/resource-bundle": "^1.6",
"symfony/config": "^5.4 || ^6.4 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0",
"symfony/doctrine-bridge": "^5.4 || ^6.4 || ^7.0",
"symfony/doctrine-bridge": "^5.4 || ^6.3 || ^7.0",
"symfony/event-dispatcher": "^5.4 || ^6.4 || ^7.0",
"symfony/form": "^5.4 || ^6.4 || ^7.0",
"symfony/http-foundation": "^5.4 || ^6.4 || ^7.0",
Expand All @@ -42,7 +42,7 @@
"psalm/plugin-phpunit": "^0.18.4",
"setono/code-quality-pack": "^2.7",
"sylius/sylius": "~1.12.13",
"symfony/browser-kit": "^5.4",
"symfony/browser-kit": "^5.4 || ^6.4 || ^7.0",
"symfony/debug-bundle": "^5.4 || ^6.4 || ^7.0",
"symfony/dotenv": "^5.4 || ^6.4 || ^7.0",
"symfony/intl": "^5.4 || ^6.4 || ^7.0",
Expand Down Expand Up @@ -85,7 +85,7 @@
"analyse": "psalm",
"check-style": "ecs check",
"fix-style": "ecs check --fix",
"functional-tests": "vendor/bin/phpunit tests/Functional/",
"unit-tests": "vendor/bin/phpunit tests/Unit/"
"functional-tests": "phpunit tests/Functional/",
"unit-tests": "phpunit tests/Unit/"
}
}
18 changes: 18 additions & 0 deletions src/Checker/PostUpdateChangesChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Checker;

use Setono\SyliusOrderEditPlugin\Exception\NewOrderWrongTotalException;
use Sylius\Component\Core\Model\OrderInterface;

final class PostUpdateChangesChecker implements PostUpdateChangesCheckerInterface
{
public function check(OrderInterface $previousOrder, OrderInterface $newOrder): void
{
if ($newOrder->getTotal() > $previousOrder->getTotal()) {
throw new NewOrderWrongTotalException();
}
}
}
12 changes: 12 additions & 0 deletions src/Checker/PostUpdateChangesCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Checker;

use Sylius\Component\Core\Model\OrderInterface;

interface PostUpdateChangesCheckerInterface
{
public function check(OrderInterface $previousOrder, OrderInterface $newOrder): void;
}
41 changes: 23 additions & 18 deletions src/Controller/EditOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Setono\SyliusOrderEditPlugin\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesCheckerInterface;
use Setono\SyliusOrderEditPlugin\Exception\NewOrderWrongTotalException;
use Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessor;
use Setono\SyliusOrderEditPlugin\Provider\OldOrderProvider;
use Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProvider;
use Setono\SyliusOrderEditPlugin\Preparer\OrderPreparerInterface;
use Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessorInterface;
use Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProviderInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -18,38 +20,41 @@
final class EditOrderAction
{
public function __construct(
private OldOrderProvider $oldOrderProvider,
private UrlGeneratorInterface $router,
private RequestStack $requestStack,
private UpdatedOrderProvider $updatedOrderProvider,
private UpdatedOrderProcessor $updatedOrderProcessor,
private readonly OrderPreparerInterface $oldOrderProvider,
private readonly UpdatedOrderProviderInterface $updatedOrderProvider,
private readonly UpdatedOrderProcessorInterface $updatedOrderProcessor,
private readonly PostUpdateChangesCheckerInterface $postUpdateChangesChecker,
private readonly UrlGeneratorInterface $router,
private readonly RequestStack $requestStack,
private readonly EntityManagerInterface $entityManager,
) {
}

public function __invoke(Request $request): Response
public function __invoke(Request $request, int $id): Response
{
$orderId = (int) $request->attributes->get('id');
$order = $this->oldOrderProvider->provide($orderId);
$order = $this->oldOrderProvider->prepareToUpdate($id);

$initialTotal = $order->getTotal();
$resource = $this->updatedOrderProvider->fromRequest($order, $request);
$oldOrder = clone $order;
$updatedOrder = $this->updatedOrderProvider->fromRequest($order, $request);

try {
$this->updatedOrderProcessor->process($initialTotal, $resource);
$this->updatedOrderProcessor->process($updatedOrder);
$this->postUpdateChangesChecker->check($oldOrder, $updatedOrder);
$this->entityManager->flush();
} catch (NewOrderWrongTotalException) {
return $this->addFlashAndRedirect(
'error',
'setono_sylius_order_edit.error.order_update',
'setono_sylius_order_edit.order_update.error',
'sylius_admin_order_update',
$orderId,
$id,
);
}

return $this->addFlashAndRedirect(
'success',
'setono_sylius_order_edit.success.order_update',
'setono_sylius_order_edit.order_update.success',
'sylius_admin_order_show',
$orderId,
$id,
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Exception/NewOrderWrongTotalException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

final class NewOrderWrongTotalException extends \RuntimeException
{
public static function occur(): self
public function __construct()
{
return new self('New order total is greater than the initial order total');
parent::__construct('New order total is greater than the initial order total');
}
}
13 changes: 13 additions & 0 deletions src/Exception/OrderUpdateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Exception;

final class OrderUpdateException extends \RuntimeException
{
public function __construct()
{
parent::__construct('Something went wrong when updating order.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Provider;
namespace Setono\SyliusOrderEditPlugin\Preparer;

use Sylius\Component\Core\Inventory\Operator\OrderInventoryOperatorInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Webmozart\Assert\Assert;

final class OldOrderProvider
final class OrderPreparer implements OrderPreparerInterface
{
public function __construct(
private OrderRepositoryInterface $orderRepository,
private OrderInventoryOperatorInterface $orderInventoryOperator,
private readonly OrderRepositoryInterface $orderRepository,
private readonly OrderInventoryOperatorInterface $orderInventoryOperator,
) {
}

public function provide(int $orderId): OrderInterface
public function prepareToUpdate(int $orderId): OrderInterface
{
/** @var OrderInterface|null $order */
$order = $this->orderRepository->find($orderId);
Expand Down
12 changes: 12 additions & 0 deletions src/Preparer/OrderPreparerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Preparer;

use Sylius\Component\Core\Model\OrderInterface;

interface OrderPreparerInterface
{
public function prepareToUpdate(int $orderId): OrderInterface;
}
24 changes: 11 additions & 13 deletions src/Processor/UpdatedOrderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,32 @@

namespace Setono\SyliusOrderEditPlugin\Processor;

use Doctrine\ORM\EntityManagerInterface;
use Setono\SyliusOrderEditPlugin\Exception\NewOrderWrongTotalException;
use Sylius\Component\Core\Inventory\Operator\OrderInventoryOperatorInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;

final class UpdatedOrderProcessor
final class UpdatedOrderProcessor implements UpdatedOrderProcessorInterface
{
public function __construct(
private OrderProcessorInterface $orderProcessor,
private OrderInventoryOperatorInterface $orderInventoryOperator,
private OrderProcessorInterface $afterCheckoutOrderPaymentProcessor,
private EntityManagerInterface $entityManager,
private readonly OrderProcessorInterface $orderProcessor,
private readonly OrderInventoryOperatorInterface $orderInventoryOperator,
private readonly OrderProcessorInterface $afterCheckoutOrderPaymentProcessor,
) {
}

public function process(int $initialTotal, OrderInterface $newOrder): void
public function process(OrderInterface $newOrder): OrderInterface
{
$newOrder->setState(OrderInterface::STATE_CART);
$this->orderProcessor->process($newOrder);
$this->afterCheckoutOrderPaymentProcessor->process($newOrder);
$newOrder->setState(OrderInterface::STATE_NEW);
$this->orderInventoryOperator->hold($newOrder);

if ($initialTotal < $newOrder->getTotal()) {
throw NewOrderWrongTotalException::occur();
}

$this->entityManager->flush();
return $newOrder;
// if ($initialTotal < $newOrder->getTotal()) {
// throw new NewOrderWrongTotalException();
// }
//
// $this->entityManager->flush();
}
}
12 changes: 12 additions & 0 deletions src/Processor/UpdatedOrderProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Processor;

use Sylius\Component\Core\Model\OrderInterface;

interface UpdatedOrderProcessorInterface
{
public function process(OrderInterface $newOrder): OrderInterface;
}
9 changes: 7 additions & 2 deletions src/Provider/UpdatedOrderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

namespace Setono\SyliusOrderEditPlugin\Provider;

use Setono\SyliusOrderEditPlugin\Exception\OrderUpdateException;
use Sylius\Bundle\OrderBundle\Form\Type\OrderType;
use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Webmozart\Assert\Assert;

final class UpdatedOrderProvider
final class UpdatedOrderProvider implements UpdatedOrderProviderInterface
{
public function __construct(private FormFactoryInterface $formFactory)
public function __construct(private readonly FormFactoryInterface $formFactory)
{
}

Expand All @@ -21,6 +22,10 @@ public function fromRequest(OrderInterface $order, Request $request): OrderInter
$form = $this->formFactory->create(OrderType::class, $order, ['validation_groups' => 'sylius']);
$form->handleRequest($request);

if (!$form->isSubmitted() || !$form->isValid()) {
throw new OrderUpdateException();
}

$data = $form->getData();
Assert::isInstanceOf($data, OrderInterface::class);

Expand Down
13 changes: 13 additions & 0 deletions src/Provider/UpdatedOrderProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Provider;

use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\HttpFoundation\Request;

interface UpdatedOrderProviderInterface
{
public function fromRequest(OrderInterface $order, Request $request): OrderInterface;
}
2 changes: 2 additions & 0 deletions src/Resources/config/routes/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ setono_sylius_order_edit_admin_update:
methods: [POST, PUT, PATCH]
defaults:
_controller: Setono\SyliusOrderEditPlugin\Controller\EditOrderAction
requirements:
id: '\d+'
8 changes: 5 additions & 3 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Setono\SyliusOrderEditPlugin\Controller\EditOrderAction" public="true">
<argument type="service" id="Setono\SyliusOrderEditPlugin\Provider\OldOrderProvider" />
<argument type="service" id="Setono\SyliusOrderEditPlugin\Preparer\OrderPreparerInterface" />
<argument type="service" id="Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProviderInterface" />
<argument type="service" id="Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessorInterface" />
<argument type="service" id="Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesCheckerInterface" />
<argument type="service" id="router" />
<argument type="service" id="request_stack" />
<argument type="service" id="Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProvider" />
<argument type="service" id="Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessor" />
<argument type="service" id="sylius.manager.order" />
</service>
</services>
</container>
21 changes: 17 additions & 4 deletions src/Resources/config/services/order_processing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@
<argument type="service" id="request_stack"/>
</service>

<service id="Setono\SyliusOrderEditPlugin\Provider\OldOrderProvider">
<service
id="Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesCheckerInterface"
class="\Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesChecker"
/>

<service
id="Setono\SyliusOrderEditPlugin\Preparer\OrderPreparerInterface"
class="Setono\SyliusOrderEditPlugin\Preparer\OrderPreparer"
>
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.inventory.order_inventory_operator" />
</service>

<service id="Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProvider">
<service
id="Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProviderInterface"
class="Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProvider"
>
<argument type="service" id="form.factory" />
</service>

<service id="Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessor">
<service
id="Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessorInterface"
class="Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessor"
>
<argument type="service" id="sylius.order_processing.order_processor" />
<argument type="service" id="sylius.inventory.order_inventory_operator" />
<argument type="service" id="sylius.order_processing.order_payment_processor.after_checkout" />
<argument type="service" id="sylius.manager.order" />
</service>
</services>
</container>
8 changes: 4 additions & 4 deletions src/Resources/translations/flashes.en.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
setono_sylius_order_edit:
error:
order_update: 'Order should not have bigger total than before editing'
success:
order_update: 'Order has been successfully updated'
order_update:
general_error: 'An error occurred while updating the order'
total_error: 'Order should not have bigger total than before editing'
success: 'Order has been successfully updated'
2 changes: 2 additions & 0 deletions tests/Application/config/routes/setono_sylius_order_edit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
setono_sylius_edit_order:
resource: "@SetonoSyliusOrderEditPlugin/Resources/config/routes.yaml"
3 changes: 0 additions & 3 deletions tests/Application/config/routes/sylius_admin.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
sylius_admin:
resource: "@SyliusAdminBundle/Resources/config/routing.yml"
prefix: '/%sylius_admin.path_name%'

setono_edit_order:
resource: "@SetonoSyliusOrderEditPlugin/Resources/config/routes.yaml"
Loading

0 comments on commit 648662d

Please sign in to comment.