-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add category to product CQRS command
- add category to product CQRS command - use the command in categoryController
- Loading branch information
1 parent
14b713a
commit 9bd783e
Showing
9 changed files
with
333 additions
and
9 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/Adapter/Product/CommandHandler/AssignProductToCategoryHandler.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,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() | ||
) | ||
); | ||
} | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
src/Core/Domain/Product/Command/AssignProductToCategoryCommand.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,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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Core/Domain/Product/CommandHandler/AssignProductToCategoryHandlerInterface.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,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); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Core/Domain/Product/Exception/CannotAssignProductToCategoryException.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,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
35
src/Core/Domain/Product/Exception/ProductConstraintException.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 | ||
/** | ||
* 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; | ||
} |
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