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

Php 8.1 / Sylius 1.12 compatible #265

Open
wants to merge 9 commits into
base: 0.12.x
Choose a base branch
from
Open
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
51 changes: 0 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,6 @@ class Order extends BaseOrder implements SetonoSyliusGiftCardPluginOrderInterfac
}
```

**Extend `OrderItem`**

```php
<?php

# src/Entity/Order/OrderItem.php

declare(strict_types=1);

namespace App\Entity\Order;

use Doctrine\ORM\Mapping as ORM;
use Setono\SyliusGiftCardPlugin\Model\OrderItemTrait as SetonoSyliusGiftCardOrderItemTrait;
use Sylius\Component\Core\Model\OrderItem as BaseOrderItem;

/**
* @ORM\Entity
* @ORM\Table(name="sylius_order_item")
*/
class OrderItem extends BaseOrderItem
{
use SetonoSyliusGiftCardOrderItemTrait;
}
```

**Extend `OrderItemUnit`**

```php
Expand Down Expand Up @@ -273,32 +248,6 @@ sylius_product:
model: App\Entity\Product\Product
```

### Copy Api Resources

Resources declaration that need to be copied are:
* [Order.xml](src/Resources/config/api_resources/Order.xml)

If you already have them overriden, just change the following routes:

**[Order.xml](src/Resources/config/api_resources/Order.xml)**
```xml
<itemOperation name="shop_add_item">
<attribute name="method">PATCH</attribute>
<attribute name="path">/shop/orders/{tokenValue}/items</attribute>
<attribute name="messenger">input</attribute>
<attribute name="input">Setono\SyliusGiftCardPlugin\Api\Command\AddItemToCart</attribute> <!-- This has been changed compared to the core -->
<attribute name="normalization_context">
<attribute name="groups">shop:cart:read</attribute>
</attribute>
<attribute name="denormalization_context">
<attribute name="groups">shop:cart:add_item</attribute>
</attribute>
<attribute name="openapi_context">
<attribute name="summary">Adds Item to cart</attribute>
</attribute>
</itemOperation>
```

### Update your database:

```bash
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
"require-dev": {
"babdev/pagerfanta-bundle": "^3.8",
"behat/behat": "^3.14",
"dmore/behat-chrome-extension": "^1.4",
"dmore/chrome-mink-driver": "^2.9",
"doctrine/doctrine-bundle": "^2.7",
"infection/infection": "^0.27.10",
"jms/serializer-bundle": "^4.2",
Expand All @@ -75,6 +77,7 @@
"setono/code-quality-pack": "^2.7",
"setono/sylius-behat-pack": "^0.2.2",
"sylius/sylius": "~1.12.13",
"sylius/sylius-rector": "^2.0",
"symfony/debug-bundle": "^5.4 || ^6.4",
"symfony/dotenv": "^5.4 || ^6.4",
"symfony/intl": "^5.4 || ^6.4",
Expand Down
25 changes: 25 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames();
$rectorConfig->import(__DIR__ . '/vendor/sylius/sylius-rector/config/config.php');
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$rectorConfig->skip([
__DIR__ . '/tests/Application/var'
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81, // Adapt this to your PHP version
SetList::CODE_QUALITY,
]);
};
5 changes: 1 addition & 4 deletions src/Api/Command/AddGiftCardToOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ class AddGiftCardToOrder implements GiftCardCodeAwareInterface
{
public ?string $giftCardCode = null;

public string $orderTokenValue;

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

public function getGiftCardCode(): ?string
Expand Down
9 changes: 1 addition & 8 deletions src/Api/Command/AddItemToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@

class AddItemToCart extends BaseAddItemToCart
{
protected ?int $amount;

protected ?string $customMessage;

public function __construct(string $productVariantCode, int $quantity, int $amount = null, string $customMessage = null)
public function __construct(string $productVariantCode, int $quantity, protected ?int $amount = null, protected ?string $customMessage = null)
{
parent::__construct($productVariantCode, $quantity);

$this->amount = $amount;
$this->customMessage = $customMessage;
}

public function getAmount(): ?int
Expand Down
8 changes: 1 addition & 7 deletions src/Api/Command/AssociateConfigurationToChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ class AssociateConfigurationToChannel implements ConfigurationCodeAwareInterface
{
public ?string $configurationCode = null;

public string $localeCode;

public string $channelCode;

public function __construct(string $localeCode, string $channelCode)
public function __construct(public string $localeCode, public string $channelCode)
{
$this->localeCode = $localeCode;
$this->channelCode = $channelCode;
}

public function getConfigurationCode(): ?string
Expand Down
32 changes: 2 additions & 30 deletions src/Api/Command/CreateGiftCardConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,7 @@

class CreateGiftCardConfiguration
{
public string $code;

public bool $default = false;

public bool $enabled = true;

public ?string $defaultValidityPeriod = null;

public ?string $pageSize = null;

public ?string $orientation = null;

public ?string $template = null;

public function __construct(
string $code,
bool $default = false,
bool $enabled = true,
?string $defaultValidityPeriod = null,
?string $pageSize = null,
?string $orientation = null,
?string $template = null,
) {
$this->code = $code;
$this->default = $default;
$this->enabled = $enabled;
$this->defaultValidityPeriod = $defaultValidityPeriod;
$this->pageSize = $pageSize;
$this->orientation = $orientation;
$this->template = $template;
public function __construct(public string $code, public bool $default = false, public bool $enabled = true, public ?string $defaultValidityPeriod = null, public ?string $pageSize = null, public ?string $orientation = null, public ?string $template = null)
{
}
}
5 changes: 1 addition & 4 deletions src/Api/Command/RemoveGiftCardFromOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ class RemoveGiftCardFromOrder implements GiftCardCodeAwareInterface
{
public ?string $giftCardCode = null;

public string $orderTokenValue;

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

public function getGiftCardCode(): ?string
Expand Down
16 changes: 2 additions & 14 deletions src/Api/CommandHandler/AddGiftCardToOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,8 @@

final class AddGiftCardToOrderHandler
{
private GiftCardRepositoryInterface $giftCardRepository;

private OrderRepositoryInterface $orderRepository;

private GiftCardApplicatorInterface $giftCardApplicator;

public function __construct(
GiftCardRepositoryInterface $giftCardRepository,
OrderRepositoryInterface $orderRepository,
GiftCardApplicatorInterface $giftCardApplicator,
) {
$this->giftCardRepository = $giftCardRepository;
$this->orderRepository = $orderRepository;
$this->giftCardApplicator = $giftCardApplicator;
public function __construct(private readonly GiftCardRepositoryInterface $giftCardRepository, private readonly OrderRepositoryInterface $orderRepository, private readonly GiftCardApplicatorInterface $giftCardApplicator)
{
}

public function __invoke(AddGiftCardToOrder $command): GiftCardInterface
Expand Down
50 changes: 18 additions & 32 deletions src/Api/CommandHandler/AddItemToCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,8 @@

final class AddItemToCartHandler
{
private OrderRepositoryInterface $orderRepository;

private ProductVariantRepositoryInterface $productVariantRepository;

private OrderModifierInterface $orderModifier;

private CartItemFactoryInterface $cartItemFactory;

private OrderItemQuantityModifierInterface $orderItemQuantityModifier;

private GiftCardFactoryInterface $giftCardFactory;

private EntityManagerInterface $giftCardManager;

public function __construct(
OrderRepositoryInterface $orderRepository,
ProductVariantRepositoryInterface $productVariantRepository,
OrderModifierInterface $orderModifier,
CartItemFactoryInterface $cartItemFactory,
OrderItemQuantityModifierInterface $orderItemQuantityModifier,
GiftCardFactoryInterface $giftCardFactory,
EntityManagerInterface $giftCardManager,
) {
$this->orderRepository = $orderRepository;
$this->productVariantRepository = $productVariantRepository;
$this->orderModifier = $orderModifier;
$this->cartItemFactory = $cartItemFactory;
$this->orderItemQuantityModifier = $orderItemQuantityModifier;
$this->giftCardFactory = $giftCardFactory;
$this->giftCardManager = $giftCardManager;
public function __construct(private readonly OrderRepositoryInterface $orderRepository, private readonly ProductVariantRepositoryInterface $productVariantRepository, private readonly OrderModifierInterface $orderModifier, private readonly CartItemFactoryInterface $cartItemFactory, private readonly OrderItemQuantityModifierInterface $orderItemQuantityModifier, private readonly GiftCardFactoryInterface $giftCardFactory, private readonly EntityManagerInterface $giftCardManager)
{
}

public function __invoke(SyliusAddItemToCart $addItemToCart): OrderInterface
Expand All @@ -71,10 +43,13 @@ public function __invoke(SyliusAddItemToCart $addItemToCart): OrderInterface
$cartItem = $this->cartItemFactory->createNew();
$cartItem->setVariant($productVariant);

if ($addItemToCart instanceof SetonoSyliusGiftCardAddItemToCart) {
if ($this->isGiftCard($addItemToCart, $productVariant)) {
/** @var ProductInterface $product */
$product = $productVariant->getProduct();
if ($product->isGiftCardAmountConfigurable()) {
/** @var SetonoSyliusGiftCardAddItemToCart $addItemToCart */
$addItemToCart = $addItemToCart;
/** @var int|null $giftCardAmount */
$giftCardAmount = $addItemToCart->getAmount();
Assert::notNull($giftCardAmount);
$cartItem->setUnitPrice($giftCardAmount);
Expand All @@ -85,10 +60,12 @@ public function __invoke(SyliusAddItemToCart $addItemToCart): OrderInterface
$this->orderItemQuantityModifier->modify($cartItem, $addItemToCart->quantity);
$this->orderModifier->addToOrder($cart, $cartItem);

if ($addItemToCart instanceof SetonoSyliusGiftCardAddItemToCart) {
if ($this->isGiftCard($addItemToCart, $productVariant)) {
/** @var OrderItemUnitInterface $unit */
foreach ($cartItem->getUnits() as $unit) {
$giftCard = $this->giftCardFactory->createFromOrderItemUnitAndCart($unit, $cart);
/** @var SetonoSyliusGiftCardAddItemToCart $addItemToCart */
$addItemToCart = $addItemToCart;
$giftCard->setCustomMessage($addItemToCart->getCustomMessage());

// As the common flow for any add to cart action will flush later. Do not flush here.
Expand All @@ -98,4 +75,13 @@ public function __invoke(SyliusAddItemToCart $addItemToCart): OrderInterface

return $cart;
}

private function isGiftCard(SyliusAddItemToCart $addItemToCart, ProductVariantInterface $productVariant): bool
{
$product = $productVariant->getProduct();

return $addItemToCart instanceof SetonoSyliusGiftCardAddItemToCart &&
$product instanceof ProductInterface &&
$product->isGiftCard();
}
}
24 changes: 2 additions & 22 deletions src/Api/CommandHandler/AssociateConfigurationToChannelHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,8 @@

final class AssociateConfigurationToChannelHandler
{
private RepositoryInterface $giftCardConfigurationRepository;

private ChannelRepositoryInterface $channelRepository;

private RepositoryInterface $localeRepository;

private RepositoryInterface $giftCardChannelConfigurationRepository;

private FactoryInterface $giftCardChannelConfigurationFactory;

public function __construct(
RepositoryInterface $giftCardConfigurationRepository,
ChannelRepositoryInterface $channelRepository,
RepositoryInterface $localeRepository,
RepositoryInterface $giftCardChannelConfigurationRepository,
FactoryInterface $giftCardChannelConfigurationFactory,
) {
$this->giftCardConfigurationRepository = $giftCardConfigurationRepository;
$this->channelRepository = $channelRepository;
$this->localeRepository = $localeRepository;
$this->giftCardChannelConfigurationRepository = $giftCardChannelConfigurationRepository;
$this->giftCardChannelConfigurationFactory = $giftCardChannelConfigurationFactory;
public function __construct(private readonly RepositoryInterface $giftCardConfigurationRepository, private readonly ChannelRepositoryInterface $channelRepository, private readonly RepositoryInterface $localeRepository, private readonly RepositoryInterface $giftCardChannelConfigurationRepository, private readonly FactoryInterface $giftCardChannelConfigurationFactory)
{
}

public function __invoke(AssociateConfigurationToChannel $command): GiftCardConfigurationInterface
Expand Down
12 changes: 2 additions & 10 deletions src/Api/CommandHandler/CreateGiftCardConfigurationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@

final class CreateGiftCardConfigurationHandler
{
private GiftCardConfigurationFactoryInterface $giftCardConfigurationFactory;

private ObjectManager $giftCardConfigurationManager;

public function __construct(
GiftCardConfigurationFactoryInterface $giftCardConfigurationFactory,
ObjectManager $giftCardConfigurationManager,
) {
$this->giftCardConfigurationFactory = $giftCardConfigurationFactory;
$this->giftCardConfigurationManager = $giftCardConfigurationManager;
public function __construct(private readonly GiftCardConfigurationFactoryInterface $giftCardConfigurationFactory, private readonly ObjectManager $giftCardConfigurationManager)
{
}

public function __invoke(CreateGiftCardConfiguration $command): GiftCardConfigurationInterface
Expand Down
16 changes: 2 additions & 14 deletions src/Api/CommandHandler/RemoveGiftCardFromOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,8 @@

final class RemoveGiftCardFromOrderHandler
{
private GiftCardRepositoryInterface $giftCardRepository;

private OrderRepositoryInterface $orderRepository;

private GiftCardApplicatorInterface $giftCardApplicator;

public function __construct(
GiftCardRepositoryInterface $giftCardRepository,
OrderRepositoryInterface $orderRepository,
GiftCardApplicatorInterface $giftCardApplicator,
) {
$this->giftCardRepository = $giftCardRepository;
$this->orderRepository = $orderRepository;
$this->giftCardApplicator = $giftCardApplicator;
public function __construct(private readonly GiftCardRepositoryInterface $giftCardRepository, private readonly OrderRepositoryInterface $orderRepository, private readonly GiftCardApplicatorInterface $giftCardApplicator)
{
}

public function __invoke(RemoveGiftCardFromOrder $command): GiftCardInterface
Expand Down
Loading
Loading