* @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\Category\CommandHandler; use Category; use PrestaShop\PrestaShop\Core\Domain\Category\Command\BulkUpdateCategoriesStatusCommand; use PrestaShop\PrestaShop\Core\Domain\Category\CommandHandler\BulkUpdateCategoriesStatusHandlerInterface; use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CannotUpdateCategoryStatusException; use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryNotFoundException; /** * Class ChangeCategoriesStatusHandler. * * @internal */ final class BulkUpdateCategoriesStatusHandler implements BulkUpdateCategoriesStatusHandlerInterface { /** * {@inheritdoc} * * @throws CannotUpdateCategoryStatusException * @throws CategoryNotFoundException */ public function handle(BulkUpdateCategoriesStatusCommand $command) { foreach ($command->getCategoryIds() as $categoryId) { $entity = new Category($categoryId->getValue()); $entity->active = $command->getNewStatus(); if (!$entity->id) { throw new CategoryNotFoundException($categoryId, sprintf('Category with id "%s" was not found', $categoryId->getValue())); } if (!$entity->update()) { throw new CannotUpdateCategoryStatusException(sprintf('Cannot update status for category with id "%s"', $categoryId->getValue())); } } } }