Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve customization of RefundType #323

Merged
merged 6 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,68 @@
}
```

1. The constructor of `Sylius\RefundPlugin\Twig\OrderRefundsExtension` has been changed:

```diff
public function __construct(
OrderRefundedTotalProviderInterface $orderRefundedTotalProvider,
UnitRefundedTotalProviderInterface $unitRefundedTotalProvider,
UnitRefundingAvailabilityCheckerInterface $unitRefundingAvailabilityChecker,
OrderRepositoryInterface $orderRepository,
RepositoryInterface $refundPaymentRepository
RepositoryInterface $refundPaymentRepository,
+ RefundTypeFactoryInterface $refundTypeFactory
) {
...
}
```

1. Return type on `getType` method of `Sylius\RefundPlugin\Entity\Refund` and `Sylius\RefundPlugin\Entity\RefundInterface` has been changed from `RefundType` to `RefundTypeInterface`

1. Return type on `convertToPHPValue` method of `Sylius\RefundPlugin\Entity\Type\RefundEnumType` has been changed from `RefundType` to `RefundTypeInterface`

1. We replaced `RefundType` with `RefundTypeInterface` in the given list of classes:
* `Sylius\RefundPlugin\Creator\RefundCreator`
* `Sylius\RefundPlugin\Checker\UnitRefundingAvailabilityChecker`
* `Sylius\RefundPlugin\Entity\Type\RefundEnumType`
* `Sylius\RefundPlugin\Entity\Refund`
* `Sylius\RefundPlugin\Factory\RefundFactory`
* `Sylius\RefundPlugin\Provider\RemainingTotalProvider`
* `Sylius\RefundPlugin\Provider\UnitRefundedTotalProvider`
* `Sylius\RefundPlugin\Validator\RefundAmountValidator`

1. `Sylius\RefundPlugin\Model\RefundType` implements `Sylius\RefundPlugin\Model\RefundTypeInterface` and consts `ORDER_ITEM_UNIT` and `SHIPMENT` has been moved to `RefundTypeInterface`

1. Service `Sylius\RefundPlugin\Twig\OrderRefundsExtension` has been changed:

```diff
<service id="Sylius\RefundPlugin\Twig\OrderRefundsExtension">
<argument type="service" id="Sylius\RefundPlugin\Provider\OrderRefundedTotalProviderInterface" />
<argument type="service" id="Sylius\RefundPlugin\Provider\UnitRefundedTotalProviderInterface" />
<argument type="service" id="Sylius\RefundPlugin\Checker\UnitRefundingAvailabilityCheckerInterface" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius_refund.repository.refund_payment" />
+ <argument type="service" id="Sylius\RefundPlugin\Factory\RefundTypeFactoryInterface" />
<tag name="twig.extension"/>
</service>
```

1. The constructor of `Sylius\RefundPlugin\Twig\OrderRefundsExtension` has been changed:

```diff
public function __construct(
OrderRefundedTotalProviderInterface $orderRefundedTotalProvider,
UnitRefundedTotalProviderInterface $unitRefundedTotalProvider,
UnitRefundingAvailabilityCheckerInterface $unitRefundingAvailabilityChecker,
OrderRepositoryInterface $orderRepository,
RepositoryInterface $refundPaymentRepository
RepositoryInterface $refundPaymentRepository,
+ RefundTypeFactoryInterface $refundTypeFactory
) {
...
}
```

### UPGRADE FROM 1.0.0-RC.9 TO 1.0.0-RC.10

1. Support for Sylius 1.8 has been dropped, upgrade your application to [Sylius 1.9](https://github.com/Sylius/Sylius/blob/master/UPGRADE-1.9.md)
Expand Down
4 changes: 2 additions & 2 deletions src/Calculator/UnitRefundTotalCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sylius\RefundPlugin\Calculator;

use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;
use Sylius\RefundPlugin\Provider\RemainingTotalProviderInterface;

final class UnitRefundTotalCalculator implements UnitRefundTotalCalculatorInterface
Expand All @@ -26,7 +26,7 @@ public function __construct(RemainingTotalProviderInterface $remainingTotalProvi
$this->remainingTotalProvider = $remainingTotalProvider;
}

public function calculateForUnitWithIdAndType(int $id, RefundType $refundType, ?float $amount = null): int
public function calculateForUnitWithIdAndType(int $id, RefundTypeInterface $refundType, ?float $amount = null): int
{
if ($amount !== null) {
return (int) round($amount * 100);
Expand Down
4 changes: 2 additions & 2 deletions src/Calculator/UnitRefundTotalCalculatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Sylius\RefundPlugin\Calculator;

use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;

interface UnitRefundTotalCalculatorInterface
{
public function calculateForUnitWithIdAndType(int $id, RefundType $refundType, ?float $amount = null): int;
public function calculateForUnitWithIdAndType(int $id, RefundTypeInterface $refundType, ?float $amount = null): int;
}
9 changes: 4 additions & 5 deletions src/Checker/UnitRefundingAvailabilityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@

namespace Sylius\RefundPlugin\Checker;

use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;
use Sylius\RefundPlugin\Provider\RemainingTotalProviderInterface;

final class UnitRefundingAvailabilityChecker implements UnitRefundingAvailabilityCheckerInterface
{
/** @var RemainingTotalProviderInterface */
private $remainingTotalProvider;

public function __construct(
RemainingTotalProviderInterface $remainingTotalProvider
) {
public function __construct(RemainingTotalProviderInterface $remainingTotalProvider)
{
$this->remainingTotalProvider = $remainingTotalProvider;
}

public function __invoke(int $unitId, RefundType $refundType): bool
public function __invoke(int $unitId, RefundTypeInterface $refundType): bool
{
return $this->remainingTotalProvider->getTotalLeftToRefund($unitId, $refundType) > 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Checker/UnitRefundingAvailabilityCheckerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Sylius\RefundPlugin\Checker;

use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;

interface UnitRefundingAvailabilityCheckerInterface
{
public function __invoke(int $unitId, RefundType $refundType): bool;
public function __invoke(int $unitId, RefundTypeInterface $refundType): bool;
}
4 changes: 2 additions & 2 deletions src/Converter/RefundUnitsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Sylius\RefundPlugin\Converter;

use Sylius\RefundPlugin\Calculator\UnitRefundTotalCalculatorInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;
use Webmozart\Assert\Assert;

final class RefundUnitsConverter implements RefundUnitsConverterInterface
Expand All @@ -27,7 +27,7 @@ public function __construct(UnitRefundTotalCalculatorInterface $unitRefundTotalC
$this->unitRefundTotalCalculator = $unitRefundTotalCalculator;
}

public function convert(array $units, RefundType $refundType, string $unitRefundClass): array
public function convert(array $units, RefundTypeInterface $refundType, string $unitRefundClass): array
{
$units = $this->filterEmptyRefundUnits($units);
$refundUnits = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Converter/RefundUnitsConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

namespace Sylius\RefundPlugin\Converter;

use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;
use Sylius\RefundPlugin\Model\UnitRefundInterface;

interface RefundUnitsConverterInterface
{
/**
* @return UnitRefundInterface[]
*/
public function convert(array $units, RefundType $refundType, string $unitRefundClass): array;
public function convert(array $units, RefundTypeInterface $refundType, string $unitRefundClass): array;
}
4 changes: 2 additions & 2 deletions src/Creator/RefundCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Exception\UnitAlreadyRefunded;
use Sylius\RefundPlugin\Factory\RefundFactoryInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;
use Sylius\RefundPlugin\Provider\RemainingTotalProviderInterface;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -48,7 +48,7 @@ public function __construct(
$this->refundManager = $refundManager;
}

public function __invoke(string $orderNumber, int $unitId, int $amount, RefundType $refundType): void
public function __invoke(string $orderNumber, int $unitId, int $amount, RefundTypeInterface $refundType): void
{
/** @var OrderInterface|null $order */
$order = $this->orderRepository->findOneByNumber($orderNumber);
Expand Down
4 changes: 2 additions & 2 deletions src/Creator/RefundCreatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Sylius\RefundPlugin\Creator;

use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;

interface RefundCreatorInterface
{
public function __invoke(string $orderNumber, int $unitId, int $amount, RefundType $refundType): void;
public function __invoke(string $orderNumber, int $unitId, int $amount, RefundTypeInterface $refundType): void;
}
8 changes: 4 additions & 4 deletions src/Entity/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Sylius\RefundPlugin\Entity;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;

class Refund implements RefundInterface
{
Expand All @@ -30,10 +30,10 @@ class Refund implements RefundInterface
/** @var int */
protected $refundedUnitId;

/** @var RefundType */
/** @var RefundTypeInterface */
protected $type;

public function __construct(OrderInterface $order, int $amount, int $refundedUnitId, RefundType $type)
public function __construct(OrderInterface $order, int $amount, int $refundedUnitId, RefundTypeInterface $type)
{
$this->order = $order;
$this->amount = $amount;
Expand Down Expand Up @@ -66,7 +66,7 @@ public function getRefundedUnitId(): int
return $this->refundedUnitId;
}

public function getType(): RefundType
public function getType(): RefundTypeInterface
{
return $this->type;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/RefundInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;

interface RefundInterface extends ResourceInterface
{
Expand All @@ -28,5 +28,5 @@ public function getAmount(): int;

public function getRefundedUnitId(): int;

public function getType(): RefundType;
public function getType(): RefundTypeInterface;
}
31 changes: 19 additions & 12 deletions src/Entity/Type/RefundEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use MyCLabs\Enum\Enum;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;

final class RefundEnumType extends Type
class RefundEnumType extends Type
{
public function getName(): string
{
Expand All @@ -30,31 +32,36 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
return 'VARCHAR(256)';
}

public function convertToPHPValue($value, AbstractPlatform $platform): RefundType
public function convertToPHPValue($value, AbstractPlatform $platform): RefundTypeInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function convertToPHPValue($value, AbstractPlatform $platform): RefundTypeInterface
public function convertToPhpValue($value, AbstractPlatform $platform): RefundTypeInterface

this way is more camelCase :)
like getId instead getID

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot change the name of this method, it is defined in class below

{
if (!RefundType::isValid($value)) {
if ($value instanceof RefundTypeInterface && $value instanceof Enum && !$value::isValid($value)) {
throw new \InvalidArgumentException(sprintf(
'The value "%s" is not valid for the enum "%s". Expected one of ["%s"]',
$value,
RefundType::class,
implode('", "', RefundType::keys())
))
;
(string) $value->getValue(),
RefundTypeInterface::class,
implode('", "', $value::keys())
));
}

return new RefundType($value);
return $this->createType($value);
}

/** @param mixed $value */
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if (null === $value) {
return null;
}

if ($value instanceof RefundType) {
return (string) $value;
if ($value instanceof RefundTypeInterface) {
return (string) $value->getValue();
}

throw ConversionException::conversionFailed($value, 'sylius_refund_refund_type');
throw ConversionException::conversionFailed((string) $value, 'sylius_refund_refund_type');
}

protected function createType(string $value): RefundTypeInterface
{
return new RefundType($value);
}
}
4 changes: 2 additions & 2 deletions src/Factory/RefundFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\RefundPlugin\Entity\RefundInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;

final class RefundFactory implements RefundFactoryInterface
{
Expand All @@ -32,7 +32,7 @@ public function createNew(): RefundInterface
throw new \InvalidArgumentException('Default creation method is forbidden for this object. Use `createWithData` instead.');
}

public function createWithData(OrderInterface $order, int $unitId, int $amount, RefundType $type): RefundInterface
public function createWithData(OrderInterface $order, int $unitId, int $amount, RefundTypeInterface $type): RefundInterface
{
return new $this->className($order, $amount, $unitId, $type);
}
Expand Down
9 changes: 7 additions & 2 deletions src/Factory/RefundFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\RefundPlugin\Entity\RefundInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\RefundTypeInterface;

interface RefundFactoryInterface extends FactoryInterface
{
public function createWithData(OrderInterface $order, int $unitId, int $amount, RefundType $type): RefundInterface;
public function createWithData(
OrderInterface $order,
int $unitId,
int $amount,
RefundTypeInterface $type
): RefundInterface;
}
32 changes: 32 additions & 0 deletions src/Factory/RefundTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\RefundPlugin\Factory;

use Sylius\RefundPlugin\Model\RefundTypeInterface;

final class RefundTypeFactory implements RefundTypeFactoryInterface
{
/** @var string */
public $className;

public function __construct(string $className)
{
$this->className = $className;
}

public function createNew(string $refundType): RefundTypeInterface
{
return new $this->className($refundType);
}
}
21 changes: 21 additions & 0 deletions src/Factory/RefundTypeFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\RefundPlugin\Factory;

use Sylius\RefundPlugin\Model\RefundTypeInterface;

interface RefundTypeFactoryInterface
{
public function createNew(string $refundType): RefundTypeInterface;
}
Loading