Skip to content

Commit

Permalink
chore(cqrs): add new discount level cqrs command
Browse files Browse the repository at this point in the history
  • Loading branch information
tleon committed Feb 27, 2025
1 parent 1332054 commit 9915ab8
Show file tree
Hide file tree
Showing 13 changed files with 518 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Adapter/CartRule/CartRuleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use CartRule;
use DateTimeImmutable;
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddCartLevelDiscountCommand;
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddDiscountCommand;
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddFreeShippingDiscountCommand;
use PrestaShop\PrestaShop\Core\Util\DateTime\DateTime as DateTimeUtil;
Expand Down Expand Up @@ -55,6 +56,11 @@ public function build(AddDiscountCommand $command): CartRule
$cartRule->type = $command->getDiscountType()->getValue();
$cartRule->free_shipping = $command instanceof AddFreeShippingDiscountCommand;

if ($command instanceof AddCartLevelDiscountCommand) {
$cartRule->reduction_percent = (float) (string) $command->getPercentDiscount()?->getValue();
$cartRule->reduction_amount = (float) (string) $command->getAmountDiscount()?->getValue();
}

return $cartRule;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Adapter\Discount\CommandHandler;

use PrestaShop\PrestaShop\Adapter\CartRule\CartRuleBuilder;
use PrestaShop\PrestaShop\Adapter\Discount\Repository\DiscountRepository;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddCartLevelDiscountCommand;
use PrestaShop\PrestaShop\Core\Domain\Discount\CommandHandler\AddCartLevelDiscountHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\DiscountId;

#[AsCommandHandler]
class AddCartLevelDiscountHandler implements AddCartLevelDiscountHandlerInterface
{
public function __construct(
private readonly DiscountRepository $discountRepository,
private readonly CartRuleBuilder $cartRuleBuilder
) {
}

public function handle(AddCartLevelDiscountCommand $command): DiscountId
{
$BuiltCartRule = $this->cartRuleBuilder->build($command);
$discount = $this->discountRepository->add($BuiltCartRule);

return new DiscountId((int) $discount->id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public function handle(GetDiscountForEditing $query): DiscountForEditing
(int) $cartRule->id_customer,
$cartRule->highlight,
$cartRule->partial_use,
$cartRule->type
$cartRule->type,
$cartRule->reduction_percent,
$cartRule->reduction_amount
);
}
}
74 changes: 74 additions & 0 deletions src/Core/Domain/Discount/Command/AddCartLevelDiscountCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\Discount\Command;

use PrestaShop\Decimal\DecimalNumber;
use PrestaShop\PrestaShop\Core\Domain\Discount\Exception\DiscountConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\AmountDiscount;
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\DiscountType;
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\PercentDiscount;

class AddCartLevelDiscountCommand extends AddDiscountCommand
{
private ?PercentDiscount $percentDiscount = null;
private ?AmountDiscount $amountDiscount = null;

public function __construct(
) {
parent::__construct(DiscountType::CART_DISCOUNT);
}

public function getPercentDiscount(): ?PercentDiscount
{
return $this->percentDiscount;
}

/**
* @throws DiscountConstraintException
*/
public function setPercentDiscount(DecimalNumber $percentDiscount): self
{
$this->percentDiscount = new PercentDiscount($percentDiscount);

return $this;
}

public function getAmountDiscount(): ?AmountDiscount
{
return $this->amountDiscount;
}

/**
* @throws DiscountConstraintException
*/
public function setAmountDiscount(DecimalNumber $amountDiscount): self
{
$this->amountDiscount = new AmountDiscount($amountDiscount);

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\Discount\CommandHandler;

use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddCartLevelDiscountCommand;
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\DiscountId;

interface AddCartLevelDiscountHandlerInterface
{
public function handle(AddCartLevelDiscountCommand $command): DiscountId;
}
12 changes: 12 additions & 0 deletions src/Core/Domain/Discount/QueryResult/DiscountForEditing.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function __construct(
private readonly bool $highlightInCart,
private readonly bool $allowPartialUse,
private readonly string $type,
private readonly ?float $percentDiscount,
private readonly ?float $amountDiscount,
) {
}

Expand Down Expand Up @@ -112,4 +114,14 @@ public function getType(): DiscountType
{
return new DiscountType($this->type);
}

public function getPercentDiscount(): ?float
{
return $this->percentDiscount;
}

public function getAmountDiscount(): ?float
{
return $this->amountDiscount;
}
}
61 changes: 61 additions & 0 deletions src/Core/Domain/Discount/ValueObject/AmountDiscount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject;

use PrestaShop\Decimal\DecimalNumber;
use PrestaShop\PrestaShop\Core\Domain\Discount\Exception\DiscountConstraintException;

class AmountDiscount
{
private DecimalNumber $amount;

/**
* @throws DiscountConstraintException
*/
public function __construct(DecimalNumber $amount)
{
$this->assertIsPositive($amount);
$this->amount = $amount;
}

public function getValue(): DecimalNumber
{
return $this->amount;
}

/**
* @param DecimalNumber $value
*
* @throws DiscountConstraintException
*/
private function assertIsPositive(DecimalNumber $value): void
{
if ($value->isLowerThanZero()) {
throw new DiscountConstraintException(sprintf('Invalid amount reduction "%s".', $value), DiscountConstraintException::INVALID_REDUCTION_AMOUNT);
}
}
}
3 changes: 1 addition & 2 deletions src/Core/Domain/Discount/ValueObject/DiscountType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
class DiscountType
{
public const FREE_SHIPPING = 'free_shipping';
public const PERCENT_DISCOUNT = 'percent_discount';
public const AMOUNT_DISCOUNT = 'amount_discount';
public const CART_DISCOUNT = 'cart_discount';
public const FREE_GIFT = 'free_gift';

public function __construct(private readonly string $value)
Expand Down
61 changes: 61 additions & 0 deletions src/Core/Domain/Discount/ValueObject/PercentDiscount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject;

use PrestaShop\Decimal\DecimalNumber;
use PrestaShop\PrestaShop\Core\Domain\Discount\Exception\DiscountConstraintException;

class PercentDiscount
{
private DecimalNumber $percent;

/**
* @throws DiscountConstraintException
*/
public function __construct(DecimalNumber $percent)
{
$this->assertIsPositive($percent);
$this->percent = $percent;
}

public function getValue(): DecimalNumber
{
return $this->percent;
}

/**
* @param DecimalNumber $value
*
* @throws DiscountConstraintException
*/
private function assertIsPositive(DecimalNumber $value): void
{
if ($value->isLowerThanZero()) {
throw new DiscountConstraintException(sprintf('Invalid percent reduction "%s".', $value), DiscountConstraintException::INVALID_REDUCTION_PERCENT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ services:
public: false
autoconfigure: true

PrestaShop\PrestaShop\Adapter\Discount\CommandHandler\AddCartLevelDiscountHandler:
autowire: true
public: false
autoconfigure: true

PrestaShop\PrestaShop\Adapter\Discount\QueryHandler\GetDiscountForEditingHandler:
autowire: true
public: false
Expand Down
Loading

0 comments on commit 9915ab8

Please sign in to comment.