From 768b778607859aa5d6f4414bc7777f070dab9b29 Mon Sep 17 00:00:00 2001 From: Raimondas Date: Tue, 1 Oct 2019 11:40:43 +0300 Subject: [PATCH] Minor CR fixes --- .../AbstractAttachmentHandler.php | 17 +++++++++++++++++ .../CommandHandler/AddAttachmentHandler.php | 15 ++++----------- .../CommandHandler/EditAttachmentHandler.php | 11 +++-------- .../File/Uploader/AttachmentFileUploader.php | 7 ++++--- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/Adapter/Attachment/CommandHandler/AbstractAttachmentHandler.php b/src/Adapter/Attachment/CommandHandler/AbstractAttachmentHandler.php index 72299bfc0e1ad..c5780ab38b593 100644 --- a/src/Adapter/Attachment/CommandHandler/AbstractAttachmentHandler.php +++ b/src/Adapter/Attachment/CommandHandler/AbstractAttachmentHandler.php @@ -26,9 +26,11 @@ namespace PrestaShop\PrestaShop\Adapter\Attachment\CommandHandler; +use Attachment; use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\CleanHtml; use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\DefaultLanguage; use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentConstraintException; +use PrestaShopException; use Symfony\Component\Validator\Validator\ValidatorInterface; /** @@ -97,4 +99,19 @@ protected function getUniqueFileName(): string return $uniqueFileName; } + + /** + * @param Attachment $attachment + * @throws AttachmentConstraintException + * @throws PrestaShopException + */ + protected function assertValidFields(Attachment $attachment) + { + if (!$attachment->validateFields(false) && !$attachment->validateFieldsLang(false)) { + throw new AttachmentConstraintException( + 'Attachment contains invalid field values', + AttachmentConstraintException::INVALID_FIELDS + ); + } + } } diff --git a/src/Adapter/Attachment/CommandHandler/AddAttachmentHandler.php b/src/Adapter/Attachment/CommandHandler/AddAttachmentHandler.php index 7472593a43494..e57b62239f508 100644 --- a/src/Adapter/Attachment/CommandHandler/AddAttachmentHandler.php +++ b/src/Adapter/Attachment/CommandHandler/AddAttachmentHandler.php @@ -77,22 +77,15 @@ public function handle(AddAttachmentCommand $command): AttachmentId $uniqueFileName = $this->getUniqueFileName(); - $attachment->file_name = $command->getOriginalName(); - $attachment->file = $uniqueFileName; $attachment->description = $command->getLocalizedDescriptions(); $attachment->name = $command->getLocalizedNames(); + $attachment->file_name = $command->getOriginalName(); + $attachment->file = $uniqueFileName; $attachment->mime = $command->getMimeType(); - if (!$attachment->validateFields(false) && !$attachment->validateFieldsLang(false)) { - throw new AttachmentConstraintException( - 'Attachment contains invalid field values', - AttachmentConstraintException::INVALID_FIELDS - ); - } + $this->assertValidFields($attachment); - if (null !== $command->getFilePathName()) { - $this->fileUploader->upload($command->getFilePathName(), $uniqueFileName, $command->getFileSize()); - } + $this->fileUploader->upload($command->getFilePathName(), $uniqueFileName, $command->getFileSize()); if (false === $attachment->add()) { throw new CannotAddAttachmentException('Failed to add attachment'); diff --git a/src/Adapter/Attachment/CommandHandler/EditAttachmentHandler.php b/src/Adapter/Attachment/CommandHandler/EditAttachmentHandler.php index 369deacd44d0f..b6ed37bef12ed 100644 --- a/src/Adapter/Attachment/CommandHandler/EditAttachmentHandler.php +++ b/src/Adapter/Attachment/CommandHandler/EditAttachmentHandler.php @@ -67,7 +67,6 @@ public function __construct(ValidatorInterface $validator, AttachmentFileUploade * @throws AttachmentException * @throws AttachmentNotFoundException * @throws CannotUpdateAttachmentException - * @throws FileNotFoundException */ public function handle(EditAttachmentCommand $command) { @@ -98,7 +97,6 @@ public function handle(EditAttachmentCommand $command) * @throws AttachmentException * @throws AttachmentNotFoundException * @throws CannotUpdateAttachmentException - * @throws FileNotFoundException */ private function updateAttachmentFromCommandData(Attachment $attachment, EditAttachmentCommand $command) { @@ -116,12 +114,7 @@ private function updateAttachmentFromCommandData(Attachment $attachment, EditAtt $attachment->description = $command->getLocalizedDescriptions(); $attachment->name = $command->getLocalizedNames(); - if (!$attachment->validateFields(false) && !$attachment->validateFieldsLang(false)) { - throw new AttachmentConstraintException( - 'Attachment contains invalid field values', - AttachmentConstraintException::INVALID_FIELDS - ); - } + $this->assertValidFields($attachment); if (null !== $command->getPathName()) { $uniqueFileName = $this->getUniqueFileName(); @@ -130,6 +123,8 @@ private function updateAttachmentFromCommandData(Attachment $attachment, EditAtt $attachment->file = $uniqueFileName; $attachment->mime = $command->getMimeType(); + $this->assertValidFields($attachment); + $this->fileUploader->upload( $command->getPathName(), $uniqueFileName, diff --git a/src/Adapter/File/Uploader/AttachmentFileUploader.php b/src/Adapter/File/Uploader/AttachmentFileUploader.php index 71b88cd42684f..2dcd588c0f1d0 100644 --- a/src/Adapter/File/Uploader/AttachmentFileUploader.php +++ b/src/Adapter/File/Uploader/AttachmentFileUploader.php @@ -82,7 +82,7 @@ private function deleteOldFile(int $attachmentId): void { try { $attachment = new Attachment($attachmentId); - @unlink(_PS_DOWNLOAD_DIR_ . $attachment->file); + unlink(_PS_DOWNLOAD_DIR_ . $attachment->file); } catch (PrestaShopException $e) { throw new AttachmentNotFoundException(sprintf('Attachment with id "%s" was not found.', $attachmentId)); } @@ -104,13 +104,14 @@ private function uploadFile(string $filePath, string $uniqid, int $fileSize): vo (string) ($this->configuration->get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024), number_format(($fileSize / 1024), 2, '.', '') ), - AttachmentConstraintException::INVALID_FILE_SIZE); + AttachmentConstraintException::INVALID_FILE_SIZE + ); } try { move_uploaded_file($filePath, _PS_DOWNLOAD_DIR_ . $uniqid); } catch (FileException $e) { - throw new AttachmentUploadFailedException('Failed to copy the file.'); + throw new AttachmentUploadFailedException(sprintf('Failed to copy the file %s.', $filePath)); } }