* @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\Attachment\CommandHandler; use PrestaShop\PrestaShop\Adapter\Attachment\AbstractAttachmentHandler; use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\BulkDeleteAttachmentsCommand; use PrestaShop\PrestaShop\Core\Domain\Attachment\CommandHandler\BulkDeleteAttachmentsHandlerInterface; use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentException; use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\BulkDeleteAttachmentsException; /** * Bulk delete attachments handler */ final class BulkDeleteAttachmentsHandler extends AbstractAttachmentHandler implements BulkDeleteAttachmentsHandlerInterface { /** * {@inheritdoc} * * @throws BulkDeleteAttachmentsException */ public function handle(BulkDeleteAttachmentsCommand $command) { $errors = []; foreach ($command->getAttachmentIds() as $attachmentId) { try { $attachment = $this->getAttachment($attachmentId); if (!$this->deleteAttachment($attachment)) { $errors[] = $attachment->id; } } catch (AttachmentException $e) { $errors[] = $attachmentId->getValue(); } } if (!empty($errors)) { throw new BulkDeleteAttachmentsException($errors, 'Failed to delete all of selected attachments'); } } }