Skip to content

Commit

Permalink
Merge pull request PrestaShop#31583 from 0x346e3730/customer_groups_m…
Browse files Browse the repository at this point in the history
…igration_cqrs_create

Customer Group CQRS - Create
  • Loading branch information
zuk3975 authored Mar 31, 2023
2 parents 080b986 + 2e20943 commit 9718dd5
Show file tree
Hide file tree
Showing 17 changed files with 1,051 additions and 4 deletions.
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);
}
}
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()
);
}
}
41 changes: 38 additions & 3 deletions src/Adapter/Customer/Group/Repository/GroupRepository.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
/**
/*
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
Expand Down Expand Up @@ -27,15 +27,38 @@

namespace PrestaShop\PrestaShop\Adapter\Customer\Group\Repository;

use Group as CustomerGroup;
use PrestaShop\PrestaShop\Adapter\CoreException;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\CannotAddGroupException;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\GroupNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\ValueObject\GroupId;
use PrestaShop\PrestaShop\Core\Repository\AbstractObjectModelRepository;
use PrestaShop\PrestaShop\Core\Repository\AbstractMultiShopObjectModelRepository;

/**
* Provides methods to access Group data storage
*/
class GroupRepository extends AbstractObjectModelRepository
class GroupRepository extends AbstractMultiShopObjectModelRepository
{
/**
* @param GroupId $customerGroupId
*
* @throws CoreException
* @throws GroupNotFoundException
*
* @return CustomerGroup
*/
public function get(GroupId $customerGroupId): CustomerGroup
{
/** @var CustomerGroup $customerGroup */
$customerGroup = $this->getObjectModel(
$customerGroupId->getValue(),
CustomerGroup::class,
GroupNotFoundException::class
);

return $customerGroup;
}

/**
* @param GroupId $groupId
*
Expand All @@ -49,4 +72,16 @@ public function assertGroupExists(GroupId $groupId): void
GroupNotFoundException::class
);
}

/**
* @param CustomerGroup $customerGroup
*
* @throws CoreException
*
* @return GroupId
*/
public function create(CustomerGroup $customerGroup): GroupId
{
return new GroupId($this->addObjectModelToShops($customerGroup, $customerGroup->id_shop_list, CannotAddGroupException::class));
}
}
153 changes: 153 additions & 0 deletions src/Adapter/Customer/Group/Validate/CustomerGroupValidator.php
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
);
}
}
}
Loading

0 comments on commit 9718dd5

Please sign in to comment.