forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cqrs): add new discount level cqrs command
- Loading branch information
Showing
13 changed files
with
518 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Adapter/Discount/CommandHandler/AddCartLevelDiscountHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/Core/Domain/Discount/Command/AddCartLevelDiscountCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Core/Domain/Discount/CommandHandler/AddCartLevelDiscountHandlerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.