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 create free shipping discount command
- Loading branch information
Showing
24 changed files
with
1,368 additions
and
34 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
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,60 @@ | ||
<?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\CartRule; | ||
|
||
use CartRule; | ||
use DateTimeImmutable; | ||
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; | ||
|
||
class CartRuleBuilder | ||
{ | ||
public function build(AddDiscountCommand $command): CartRule | ||
{ | ||
$cartRule = new CartRule(); | ||
$validFrom = $command->getValidFrom() ?: new DateTimeImmutable(); | ||
$validTo = $command->getValidTo() ?: $validFrom->modify('+1 month'); | ||
|
||
$cartRule->name = $command->getLocalizedNames(); | ||
$cartRule->description = $command->getDescription(); | ||
$cartRule->code = $command->getCode(); | ||
$cartRule->highlight = $command->isHighlightInCart(); | ||
$cartRule->partial_use = $command->allowPartialUse(); | ||
$cartRule->priority = $command->getPriority(); | ||
$cartRule->active = $command->isActive(); | ||
$cartRule->id_customer = $command->getCustomerId()?->getValue(); | ||
$cartRule->date_from = $validFrom->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT); | ||
$cartRule->date_to = $validTo->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT); | ||
$cartRule->quantity = $command->getTotalQuantity(); | ||
$cartRule->quantity_per_user = $command->getQuantityPerUser(); | ||
$cartRule->type = $command->getDiscountType()->getValue(); | ||
$cartRule->free_shipping = $command instanceof AddFreeShippingDiscountCommand; | ||
|
||
return $cartRule; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Adapter/Discount/CommandHandler/AddFreeShippingDiscountHandler.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\CartRule\Repository\CartRuleRepository; | ||
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler; | ||
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddFreeShippingDiscountCommand; | ||
use PrestaShop\PrestaShop\Core\Domain\Discount\CommandHandler\AddFreeShippingDiscountHandlerInterface; | ||
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\DiscountId; | ||
|
||
#[AsCommandHandler] | ||
class AddFreeShippingDiscountHandler implements AddFreeShippingDiscountHandlerInterface | ||
{ | ||
public function __construct( | ||
private readonly CartRuleRepository $cartRuleRepository, | ||
private readonly CartRuleBuilder $cartRuleBuilder | ||
) { | ||
} | ||
|
||
public function handle(AddFreeShippingDiscountCommand $command): DiscountId | ||
{ | ||
$BuiltCartRule = $this->cartRuleBuilder->build($command); | ||
$discount = $this->cartRuleRepository->add($BuiltCartRule); | ||
|
||
return new DiscountId((int) $discount->id); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Adapter/Discount/QueryHandler/GetDiscountForEditingHandler.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,72 @@ | ||
<?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\QueryHandler; | ||
|
||
use DateTimeImmutable; | ||
use Exception; | ||
use PrestaShop\PrestaShop\Adapter\CartRule\Repository\CartRuleRepository; | ||
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsQueryHandler; | ||
use PrestaShop\PrestaShop\Core\Domain\CartRule\ValueObject\CartRuleId; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\CustomerId; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\NoCustomerId; | ||
use PrestaShop\PrestaShop\Core\Domain\Discount\Query\GetDiscountForEditing; | ||
use PrestaShop\PrestaShop\Core\Domain\Discount\QueryHandler\GetDiscountForEditingHandlerInterface; | ||
use PrestaShop\PrestaShop\Core\Domain\Discount\QueryResult\DiscountForEditing; | ||
|
||
#[AsQueryHandler] | ||
class GetDiscountForEditingHandler implements GetDiscountForEditingHandlerInterface | ||
{ | ||
public function __construct( | ||
protected readonly CartRuleRepository $cartRuleRepository | ||
) { | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function handle(GetDiscountForEditing $query): DiscountForEditing | ||
{ | ||
$cartRuleId = new CartRuleId($query->discountId->getValue()); | ||
$cartRule = $this->cartRuleRepository->get($cartRuleId); | ||
|
||
return new DiscountForEditing( | ||
$query->discountId, | ||
$cartRule->priority, | ||
$cartRule->active, | ||
new DateTimeImmutable($cartRule->date_from), | ||
new DateTimeImmutable($cartRule->date_to), | ||
$cartRule->quantity, | ||
$cartRule->quantity_per_user, | ||
$cartRule->description, | ||
$cartRule->code, | ||
(int) $cartRule->id_customer !== NoCustomerId::NO_CUSTOMER_ID_VALUE ? new CustomerId((int) $cartRule->id_customer) : new NoCustomerId(), | ||
$cartRule->highlight, | ||
$cartRule->partial_use, | ||
$cartRule->type | ||
); | ||
} | ||
} |
Oops, something went wrong.