Skip to content

Commit

Permalink
Add category to product CQRS command
Browse files Browse the repository at this point in the history
- add category to product CQRS command
- use the command in categoryController
  • Loading branch information
matthieu-rolland committed Jul 23, 2019
1 parent 14b713a commit 9bd783e
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* 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.txt.
* 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://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Adapter\Product\CommandHandler;

use PrestaShop\PrestaShop\Adapter\Domain\AbstractObjectModelHandler;
use PrestaShop\PrestaShop\Core\Domain\Product\CommandHandler\AssignProductToCategoryHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Product\Command\AssignProductToCategoryCommand;
use PrestaShop\PrestaShop\Adapter\Product\ProductDataProvider;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\CannotAssignProductToCategoryException;


/**
* Adds a category to a product
*
* @internal
*/
final class AssignProductToCategoryHandler extends AbstractObjectModelHandler implements AssignProductToCategoryHandlerInterface
{
/**
* @param $command*
*/
public function handle(AssignProductToCategoryCommand $command)
{
$this->assignProductToCategory($command);
}

/**
* @param AssignProductToCategoryCommand $command
*
* @throws CannotAssignProductToCategoryException
*
*/
private function assignProductToCategory(AssignProductToCategoryCommand $command)
{
throw new CannotAssignProductToCategoryException(
sprintf(
'Failed to add category to product %d',
$command->getProductId()->getValue()
)
);

$productDataProvider = new ProductDataProvider();
$product = $productDataProvider->getProductInstance($command->getProductId()->getValue());
$product->addToCategories($command->getCategoryId()->getValue());
if (false === $product->save()) {
throw new CannotAssignProductToCategoryException(
sprintf(
'Failed to add category to product %d',
$command->getProductId()->getValue()
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class CategoryConstraintException extends CategoryException
*/
const TOO_MANY_MENU_THUMBNAILS = 8;

/**
* Code is used when invalid id is supplied.
*/
const INVALID_ID = 10;

/**
* Code is used when performing bulk delete of categories with empty data.
*/
Expand Down
100 changes: 100 additions & 0 deletions src/Core/Domain/Product/Command/AssignProductToCategoryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* 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.txt.
* 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://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Core\Domain\Product\Command;

use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\ProductConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\ProductId;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\CategoryId;

/**
* Class AssignProductToCategoryCommand adds a product to a category.
*/
class AssignProductToCategoryCommand
{
/**
* @var CategoryId
*/
private $categoryId;

/**
* @var ProductId
*/
private $productId;

/**
* @param int $categoryId
* @param int $productId
*
* @throws CategoryConstraintException
* @throws ProductConstraintException */
public function __construct($categoryId, $productId)
{
$this->setCategoryId($categoryId);
$this->setProductId($productId);
}

/**
* @param int $categoryId
*
* @return self
*/
public function setCategoryId(int $categoryId)
{
$this->categoryId = new CategoryId($categoryId);

return $this;
}

/**
* @return CategoryId
*/
public function getCategoryId()
{
return $this->categoryId;
}

/**
* @param int $productId
*
* @return self
*/
public function setProductId(int $productId)
{
$this->productId = new ProductId($productId);

return $this;
}

/**
* @return ProductId
*/
public function getProductId()
{
return $this->productId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* 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.txt.
* 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://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Core\Domain\Product\CommandHandler;

use PrestaShop\PrestaShop\Core\Domain\Product\Command\AssignProductToCategoryCommand;

interface AssignProductToCategoryHandlerInterface
{
/**
* @param AssignProductToCategoryCommand $command
*/
public function handle(AssignProductToCategoryCommand $command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* 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.txt.
* 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://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Core\Domain\Product\Exception;

/**
* Is thrown when the assignation of a product to a category failed
*/
class CannotAssignProductToCategoryException extends ProductException
{
}
35 changes: 35 additions & 0 deletions src/Core/Domain/Product/Exception/ProductConstraintException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* 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.txt.
* 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://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Core\Domain\Product\Exception;

class ProductConstraintException extends ProductException
{
/**
* Code is used when invalid id is supplied.
*/
const INVALID_ID = 10;
}
4 changes: 2 additions & 2 deletions src/Core/Domain/Product/ValueObject/ProductId.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace PrestaShop\PrestaShop\Core\Domain\Product\ValueObject;

use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderException;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\ProductConstraintException;

/**
* Product identity
Expand Down Expand Up @@ -62,7 +62,7 @@ public function getValue()
private function assertIntegerIsGreaterThanZero($productId)
{
if (!is_int($productId) || 0 > $productId) {
throw new OrderException(
throw new ProductConstraintException(
sprintf(
'Product id %s is invalid. Product id must be number that is greater than zero.',
var_export($productId, true)
Expand Down
43 changes: 36 additions & 7 deletions src/PrestaShopBundle/Controller/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use PrestaShop\PrestaShop\Core\Domain\Category\Command\AddCategoryCommand;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\CategoryId;
use PrestaShop\PrestaShop\Core\Domain\Product\Command\AssignProductToCategoryCommand;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\ProductException;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\CannotAssignProductToCategoryException;
use PrestaShopBundle\Form\Admin\Category\SimpleCategory;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -92,16 +95,22 @@ public function addSimpleCategoryFormAction(Request $request)
],
]
);

if ($request->query->has('id_product')) {
$productAdapter = $this->get('prestashop.adapter.data_provider.product');
$product = $productAdapter->getProduct($request->query->get('id_product'));
$product->addToCategories($categoryId->getValue());
$product->save();
$assignProductToCategoryCommand = new AssignProductToCategoryCommand(
$categoryId->getValue(),
$request->query->get('id_product')
);
$commandBus->handle($assignProductToCategoryCommand);
}
}
} catch (CategoryException $e) {
// @todo error handling should be implemented.
} catch (CategoryException $e) {
// TODO: do some frontend work to display this error message from ajax query
$response->setStatusCode(400);
$response->setData(['error' => $this->getErrorMessageForException($e, $this->getErrorMessages($data['category']['name']))]);
} catch (ProductException $e) {
// TODO: do some frontend work to display this error message from ajax query
$response->setStatusCode(400);
$response->setData(['error' => $this->getErrorMessageForException($e, $this->getErrorMessages($data['category']['name']))]);
}
} else {
$response->setStatusCode(400);
Expand Down Expand Up @@ -129,4 +138,24 @@ public function getAjaxCategoriesAction($limit, Request $request)
$this->get('prestashop.adapter.data_provider.category')->getAjaxCategories($request->get('query'), $limit, true)
);
}

/**
* @param string $categoryName
* @return array
*/
private function getErrorMessages($categoryName)
{
return [
CategoryException::class => $this->trans(
'Category "%s" could not be created.',
'Admin.Notifications.Error',
[$categoryName]
),
CannotAssignProductToCategoryException::class => $this->trans(
'This product could not be assigned to category "%s".',
'Admin.Notifications.Error',
[$categoryName]
),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ services:

prestashop.adapter.product.list_parameters_updater:
class: 'PrestaShop\PrestaShop\Adapter\Product\ListParametersUpdater'

prestashop:.adapter.product.command_handler.add_category_to_product_handler:
class: PrestaShop\PrestaShop\Adapter\Product\CommandHandler\AssignProductToCategoryHandler
public: true
tags:
- { name: tactician.handler, command: PrestaShop\PrestaShop\Core\Domain\Product\Command\AssignProductToCategoryCommand }

0 comments on commit 9bd783e

Please sign in to comment.