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.
Merge pull request PrestaShop#31583 from 0x346e3730/customer_groups_m…
…igration_cqrs_create Customer Group CQRS - Create
- Loading branch information
Showing
17 changed files
with
1,051 additions
and
4 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/Adapter/Customer/Group/CommandHandler/AddCustomerGroupHandler.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,68 @@ | ||
<?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 [email protected] 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 <[email protected]> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace PrestaShop\PrestaShop\Adapter\Customer\Group\CommandHandler; | ||
|
||
use Group as CustomerGroup; | ||
use PrestaShop\PrestaShop\Adapter\Customer\Group\Repository\GroupRepository; | ||
use PrestaShop\PrestaShop\Adapter\Customer\Group\Validate\CustomerGroupValidator; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\CommandHandler\AddCustomerGroupHandlerInterface; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\ValueObject\GroupId; | ||
|
||
class AddCustomerGroupHandler implements AddCustomerGroupHandlerInterface | ||
{ | ||
/** | ||
* @var CustomerGroupValidator | ||
*/ | ||
private $customerGroupValidator; | ||
|
||
/** | ||
* @var GroupRepository | ||
*/ | ||
private $customerGroupRepository; | ||
|
||
public function __construct(CustomerGroupValidator $customerGroupValidator, GroupRepository $customerGroupRepository) | ||
{ | ||
$this->customerGroupValidator = $customerGroupValidator; | ||
$this->customerGroupRepository = $customerGroupRepository; | ||
} | ||
|
||
public function handle(AddCustomerGroupCommand $command): GroupId | ||
{ | ||
$customerGroup = new CustomerGroup(); | ||
$customerGroup->name = $command->getLocalizedNames(); | ||
$customerGroup->reduction = (string) $command->getReductionPercent(); | ||
$customerGroup->price_display_method = (int) $command->displayPriceTaxExcluded(); | ||
$customerGroup->show_prices = $command->showPrice(); | ||
$customerGroup->id_shop_list = $command->getShopIds(); | ||
|
||
$this->customerGroupValidator->validate($customerGroup); | ||
|
||
return $this->customerGroupRepository->create($customerGroup); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/Adapter/Customer/Group/QueryHandler/GetCustomerGroupForEditingHandler.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,70 @@ | ||
<?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 [email protected] 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 <[email protected]> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace PrestaShop\PrestaShop\Adapter\Customer\Group\QueryHandler; | ||
|
||
use PrestaShop\Decimal\DecimalNumber; | ||
use PrestaShop\PrestaShop\Adapter\Customer\Group\Repository\GroupRepository; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\GroupNotFoundException; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Query\GetCustomerGroupForEditing; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryHandler\GetCustomerGroupForEditingHandlerInterface; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryResult\EditableCustomerGroup; | ||
|
||
class GetCustomerGroupForEditingHandler implements GetCustomerGroupForEditingHandlerInterface | ||
{ | ||
/** | ||
* @var GroupRepository | ||
*/ | ||
private $customerGroupRepository; | ||
|
||
public function __construct(GroupRepository $customerGroupRepository) | ||
{ | ||
$this->customerGroupRepository = $customerGroupRepository; | ||
} | ||
|
||
/** | ||
* @param GetCustomerGroupForEditing $query | ||
* | ||
* @throws GroupNotFoundException | ||
* | ||
* @return EditableCustomerGroup | ||
*/ | ||
public function handle(GetCustomerGroupForEditing $query): EditableCustomerGroup | ||
{ | ||
$customerGroupId = $query->getCustomerGroupId(); | ||
$customerGroup = $this->customerGroupRepository->get($customerGroupId); | ||
|
||
return new EditableCustomerGroup( | ||
$customerGroupId->getValue(), | ||
$customerGroup->name, | ||
new DecimalNumber($customerGroup->reduction), | ||
(bool) $customerGroup->price_display_method, | ||
(bool) $customerGroup->show_prices, | ||
$customerGroup->getAssociatedShops() | ||
); | ||
} | ||
} |
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
153 changes: 153 additions & 0 deletions
153
src/Adapter/Customer/Group/Validate/CustomerGroupValidator.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,153 @@ | ||
<?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 [email protected] 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 <[email protected]> | ||
* @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\Customer\Group\Validate; | ||
|
||
use Group as CustomerGroup; | ||
use PrestaShop\PrestaShop\Adapter\AbstractObjectModelValidator; | ||
use PrestaShop\PrestaShop\Adapter\Shop\Repository\ShopRepository; | ||
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\GroupConstraintException; | ||
|
||
class CustomerGroupValidator extends AbstractObjectModelValidator | ||
{ | ||
/** | ||
* @var ShopRepository | ||
*/ | ||
private $shopRepository; | ||
|
||
public function __construct(ShopRepository $shopRepository) | ||
{ | ||
$this->shopRepository = $shopRepository; | ||
} | ||
|
||
/** | ||
* @param CustomerGroup $customerGroup | ||
* | ||
* @throws GroupConstraintException | ||
* | ||
* @return void | ||
*/ | ||
public function validate(CustomerGroup $customerGroup): void | ||
{ | ||
$this->validateThereIsAtLeastOneShop($customerGroup->id_shop_list); | ||
$this->validateShopsExists($customerGroup->id_shop_list); | ||
$this->validateGroupNames($customerGroup->name); | ||
$this->validatePriceDisplayMethod($customerGroup->price_display_method); | ||
} | ||
|
||
/** | ||
* @param array $shopIds | ||
* | ||
* @throws GroupConstraintException | ||
* | ||
* @return void | ||
*/ | ||
private function validateThereIsAtLeastOneShop(array $shopIds): void | ||
{ | ||
if (empty($shopIds)) { | ||
throw new GroupConstraintException( | ||
'Customer group must be associated with at least one shop', | ||
GroupConstraintException::EMPTY_SHOP_LIST | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $shopIds | ||
* | ||
* @return void | ||
*/ | ||
private function validateShopsExists(array $shopIds): void | ||
{ | ||
foreach ($shopIds as $shopId) { | ||
$this->shopRepository->assertShopExists($shopId); | ||
} | ||
} | ||
|
||
/** | ||
* @param int $priceDisplayMethod | ||
* | ||
* @throws GroupConstraintException | ||
* | ||
* @return void | ||
*/ | ||
private function validatePriceDisplayMethod(int $priceDisplayMethod): void | ||
{ | ||
switch ($priceDisplayMethod) { | ||
case CustomerGroup::PRICE_DISPLAY_METHOD_TAX_INCL: | ||
case CustomerGroup::PRICE_DISPLAY_METHOD_TAX_EXCL: | ||
return; | ||
default: | ||
throw new GroupConstraintException( | ||
sprintf('Invalid price display method "%s"', $priceDisplayMethod), | ||
GroupConstraintException::INVALID_PRICE_DISPLAY_METHOD | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param string[] $names | ||
* | ||
* @throws GroupConstraintException | ||
* | ||
* @return void | ||
*/ | ||
private function validateGroupNames(array $names): void | ||
{ | ||
if (empty($names)) { | ||
throw new GroupConstraintException( | ||
'Customer group name cannot be empty', | ||
GroupConstraintException::EMPTY_NAME | ||
); | ||
} | ||
foreach ($names as $name) { | ||
$this->validateGroupName($name); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @throws GroupConstraintException | ||
* | ||
* @return void | ||
*/ | ||
private function validateGroupName(string $name): void | ||
{ | ||
if (strlen($name) > 32) { | ||
throw new GroupConstraintException( | ||
sprintf('Customer group name cannot be longer than 32 characters. Got "%s"', $name), | ||
GroupConstraintException::NAME_TOO_LONG | ||
); | ||
} | ||
if (false === preg_match('/^[^<>={}]*$/u', $name)) { | ||
throw new GroupConstraintException( | ||
'Customer group name cannot contain these characters: < > = { }', | ||
GroupConstraintException::INVALID_NAME | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.