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 3ceb3bd
Show file tree
Hide file tree
Showing 26 changed files with 339 additions and 128 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ jobs:
- "highest"

steps:
- name: "Start MySQL"
run: "sudo /etc/init.d/mysql start"

- name: "Checkout"
uses: "actions/checkout@v4"

Expand Down Expand Up @@ -325,7 +328,7 @@ jobs:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Collect code coverage with pcov and phpunit/phpunit"
run: "vendor/bin/phpunit --coverage-clover=.build/logs/clover.xml"
run: "vendor/bin/phpunit tests/Unit/ --coverage-clover=.build/logs/clover.xml"

- name: "Send code coverage report to Codecov.io"
uses: "codecov/codecov-action@v3"
Expand Down
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;
}
57 changes: 35 additions & 22 deletions src/Controller/EditOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,64 @@

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;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class EditOrderAction
{
public function __construct(

Check warning on line 22 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L22

Added line #L22 was not covered by tests
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,
) {
}

Check warning on line 31 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L31

Added line #L31 was not covered by tests

public function __invoke(Request $request): Response
public function __invoke(Request $request, int $id): Response

Check warning on line 33 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L33

Added line #L33 was not covered by tests
{
$orderId = (int) $request->attributes->get('id');
$order = $this->oldOrderProvider->provide($orderId);
$order = $this->oldOrderProvider->prepareToUpdate($id);

Check warning on line 35 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L35

Added line #L35 was not covered by tests

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

Check warning on line 38 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L37-L38

Added lines #L37 - L38 were not covered by tests

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.total_error',
'sylius_admin_order_update',
$orderId,
$id,
);
} catch (\Exception) {
return $this->addFlashAndRedirect(
'error',
'setono_sylius_order_edit.order_update.general_error',
'sylius_admin_order_update',
$id,
);

Check warning on line 57 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L41-L57

Added lines #L41 - L57 were not covered by tests
}

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

Check warning on line 65 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L60-L65

Added lines #L60 - L65 were not covered by tests
}

Expand All @@ -60,9 +72,10 @@ private function addFlashAndRedirect(
int $orderId,
): RedirectResponse {
$session = $this->requestStack->getSession();

Check warning on line 74 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L74

Added line #L74 was not covered by tests
/** @var FlashBagInterface $flashBag */
$flashBag = $session->getBag('flashes');
$flashBag->add($type, $message);

if ($session instanceof Session) {
$session->getFlashBag()->add($type, $message);

Check warning on line 77 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L76-L77

Added lines #L76 - L77 were not covered by tests
}

return new RedirectResponse($this->router->generate($route, ['id' => $orderId]));

Check warning on line 80 in src/Controller/EditOrderAction.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/EditOrderAction.php#L80

Added line #L80 was not covered by tests
}
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;
}
13 changes: 9 additions & 4 deletions src/Provider/UpdatedOrderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@

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)
{
}

public function fromRequest(OrderInterface $order, Request $request): OrderInterface
public function provideFromOldOrderAndRequest(OrderInterface $oldOrder, Request $request): OrderInterface
{
$form = $this->formFactory->create(OrderType::class, $order, ['validation_groups' => 'sylius']);
$form = $this->formFactory->create(OrderType::class, $oldOrder, ['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 provideFromOldOrderAndRequest(OrderInterface $oldOrder, Request $request): OrderInterface;
}
4 changes: 3 additions & 1 deletion src/Resources/config/routes/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ setono_sylius_order_edit_admin_update:
path: /orders/{id}/update-and-processs
methods: [POST, PUT, PATCH]
defaults:
_controller: Setono\SyliusOrderEditPlugin\Controller\EditOrderAction
_controller: setono_sylius_order_edit.controller.edit_order
requirements:
id: '\d+'
14 changes: 10 additions & 4 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
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" />
<service
id="setono_sylius_order_edit.controller.edit_order"
class="Setono\SyliusOrderEditPlugin\Controller\EditOrderAction"
public="true"
>
<argument type="service" id="setono_sylius_order_edit.preparer.order_preparer" />
<argument type="service" id="setono_sylius_order_edit.provider.updated_order_provider" />
<argument type="service" id="setono_sylius_order_edit.processor.updated_order_processor" />
<argument type="service" id="setono_sylius_order_edit.checker.post_update_changes_checker" />
<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>
Loading

0 comments on commit 3ceb3bd

Please sign in to comment.