Skip to content

Commit

Permalink
PR review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 18, 2024
1 parent 96b9231 commit 805d4c7
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 88 deletions.
25 changes: 4 additions & 21 deletions src/DependencyInjection/SetonoSyliusOrderEditExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Setono\SyliusOrderEditPlugin\DependencyInjection;

use Sylius\Bundle\CoreBundle\DependencyInjection\PrependDoctrineMigrationsTrait;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand All @@ -13,8 +12,6 @@

final class SetonoSyliusOrderEditExtension extends Extension implements PrependExtensionInterface
{
use PrependDoctrineMigrationsTrait;

public function load(array $configs, ContainerBuilder $container): void
{
/**
Expand All @@ -34,7 +31,10 @@ public function prepend(ContainerBuilder $container): void
'after' => [
'setono_sylius_order_edit.set_initial_total' => [
'on' => ['create'],
'do' => ['@setono_sylius_order_edit.order_processing.order_initial_total_processor', 'process'],
'do' => [
'@setono_sylius_order_edit.order_processing.order_initial_total_processor',
'process',
],
'args' => ['object'],
],
],
Expand All @@ -53,22 +53,5 @@ public function prepend(ContainerBuilder $container): void
],
],
]);

$this->prependDoctrineMigrations($container);
}

protected function getMigrationsNamespace(): string
{
return 'Setono\SyliusOrderEditPlugin\Migrations';
}

protected function getMigrationsDirectory(): string
{
return '@SetonoSyliusOrderEditPlugin/Migrations';
}

protected function getNamespacesOfMigrationsExecutedBefore(): array
{
return ['Sylius\Bundle\CoreBundle\Migrations'];
}
}
11 changes: 11 additions & 0 deletions src/Entity/EditableOrderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Entity;

use Sylius\Component\Core\Model\OrderInterface;

interface EditableOrderInterface extends OrderInterface, InitialTotalAwareOrderInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

use Doctrine\ORM\Mapping as ORM;

trait InitialTotalAwareOrder
trait InitialTotalAwareOrderTrait
{
/** @ORM\Column(type="integer", name="initial_total", options={"default": 0}) */
/** @ORM\Column(type="integer") */
#[ORM\Column(type: 'integer')]
private int $initialTotal = 0;

public function getInitialTotal(): int
Expand Down
9 changes: 4 additions & 5 deletions src/Form/Type/OrderDiscountCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Setono\SyliusOrderEditPlugin\Form\Type;

use Setono\SyliusOrderEditPlugin\Model\OrderEditDiscountTypes;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
use Sylius\Component\Order\Model\AdjustmentInterface;
Expand All @@ -13,8 +14,6 @@

final class OrderDiscountCollectionType extends AbstractType
{
private const CUSTOM_ORDER_DISCOUNT = 'custom_order_discount';

public function __construct(private readonly AdjustmentFactoryInterface $adjustmentFactory)
{
}
Expand All @@ -36,19 +35,19 @@ public function configureOptions(OptionsResolver $resolver): void
'label' => false,
],
'getter' => function (OrderInterface &$order): array {
$adjustments = $order->getAdjustments(self::CUSTOM_ORDER_DISCOUNT)->toArray();
$adjustments = $order->getAdjustments(OrderEditDiscountTypes::SETONO_ADMIN_ORDER_DISCOUNT)->toArray();

return array_map(function (AdjustmentInterface $adjustment): int {
return -1 * $adjustment->getAmount();
}, $adjustments);
},
'setter' => function (OrderInterface &$order, array $discounts): void {
$order->removeAdjustments(self::CUSTOM_ORDER_DISCOUNT);
$order->removeAdjustments(OrderEditDiscountTypes::SETONO_ADMIN_ORDER_DISCOUNT);

/** @var int $discount */
foreach ($discounts as $discount) {
$adjustment = $this->adjustmentFactory->createWithData(
self::CUSTOM_ORDER_DISCOUNT,
OrderEditDiscountTypes::SETONO_ADMIN_ORDER_DISCOUNT,
'Custom discount: ' . number_format($discount, 2),
-1 * $discount,
);
Expand Down
31 changes: 0 additions & 31 deletions src/Migrations/Version20240617131409.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/Model/OrderEditDiscountTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Model;

final class OrderEditDiscountTypes
{
public const SETONO_ADMIN_ORDER_DISCOUNT = 'setono_admin_order_discount';

private function __construct()

Check warning on line 11 in src/Model/OrderEditDiscountTypes.php

View check run for this annotation

Codecov / codecov/patch

src/Model/OrderEditDiscountTypes.php#L11

Added line #L11 was not covered by tests
{
}

Check warning on line 13 in src/Model/OrderEditDiscountTypes.php

View check run for this annotation

Codecov / codecov/patch

src/Model/OrderEditDiscountTypes.php#L13

Added line #L13 was not covered by tests
}
7 changes: 3 additions & 4 deletions src/OrderProcessing/OrderInitialTotalProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
namespace Setono\SyliusOrderEditPlugin\OrderProcessing;

use Doctrine\ORM\EntityManagerInterface;
use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;

final class OrderInitialTotalProcessor
final class OrderInitialTotalProcessor implements OrderInitialTotalProcessorInterface
{
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}

public function process(OrderInterface&InitialTotalAwareOrderInterface $order): void
public function process(EditableOrderInterface $order): void
{
$order->setInitialTotal($order->getTotal());

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

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\OrderProcessing;

use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;

interface OrderInitialTotalProcessorInterface
{
public function process(EditableOrderInterface $order): void;
}
2 changes: 1 addition & 1 deletion tests/Application/config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ doctrine:
mappings:
App:
is_bundle: false
type: annotation
type: attribute
dir: '%kernel.project_dir%/src/Entity'
prefix: 'Setono\SyliusOrderEditPlugin\Tests\Application\Entity'
15 changes: 6 additions & 9 deletions tests/Application/src/Entity/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
namespace Setono\SyliusOrderEditPlugin\Tests\Application\Entity;

use Doctrine\ORM\Mapping as ORM;
use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrder;
use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderInterface;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderTrait;

/**
* @ORM\Entity
*
* @ORM\Table(name="sylius_order")
*/
class Order extends \Sylius\Component\Core\Model\Order implements InitialTotalAwareOrderInterface
#[ORM\Entity]
#[ORM\Table(name: 'sylius_order')]
class Order extends \Sylius\Component\Core\Model\Order implements EditableOrderInterface
{
use InitialTotalAwareOrder;
use InitialTotalAwareOrderTrait;
}
22 changes: 7 additions & 15 deletions tests/Unit/OrderProcessing/OrderInitialTotalProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrder;
use Setono\SyliusOrderEditPlugin\Entity\InitialTotalAwareOrderInterface;
use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface;
use Setono\SyliusOrderEditPlugin\OrderProcessing\OrderInitialTotalProcessor;
use Sylius\Component\Core\Model\Order;
use Sylius\Component\Core\Model\OrderInterface;

final class OrderInitialTotalProcessorTest extends TestCase
{
Expand All @@ -22,18 +19,13 @@ public function testItSetsInitialTotalAfterOrderIsCompleted(): void
$entityManager = $this->prophesize(EntityManagerInterface::class);

$processor = new OrderInitialTotalProcessor($entityManager->reveal());
$order = new class() extends Order implements OrderInterface, InitialTotalAwareOrderInterface {
use InitialTotalAwareOrder;

public function getTotal(): int
{
return 1000;
}
};
$entityManager->flush()->shouldBeCalled();
$order = $this->prophesize(EditableOrderInterface::class);

$order->getTotal()->willReturn(1000);
$order->setInitialTotal(1000)->shouldBeCalled();

$processor->process($order);
$entityManager->flush()->shouldBeCalled();

self::assertSame(1000, $order->getInitialTotal());
$processor->process($order->reveal());
}
}

0 comments on commit 805d4c7

Please sign in to comment.