Skip to content

Commit 3564244

Browse files
committed
ACP2E-3424: adjust the error message displayed
1 parent cbf4a05 commit 3564244

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

app/code/Magento/PageBuilder/Model/TemplateRepository.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
1111
use Magento\Framework\Api\SearchCriteriaInterface;
12+
use Magento\Framework\App\Filesystem\DirectoryList;
1213
use Magento\Framework\Exception\CouldNotDeleteException;
1314
use Magento\Framework\Exception\CouldNotSaveException;
15+
use Magento\Framework\Exception\FileSystemException;
1416
use Magento\Framework\Exception\NoSuchEntityException;
1517
use Magento\Framework\Filesystem;
1618
use Magento\Framework\Image\Factory;
@@ -144,30 +146,14 @@ public function getList(SearchCriteriaInterface $criteria)
144146
*/
145147
public function delete(TemplateInterface $template) : bool
146148
{
147-
$mediaDir = $this->filesystem
148-
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
149-
150149
try {
151150
$templateModel = $this->templateFactory->create();
152151
$this->resource->load($templateModel, $template->getTemplateId());
153152
$this->resource->delete($templateModel);
154153
$previewImage = $template->getPreviewImage();
155154
$previewThumbImage = $templateModel->getPreviewThumbnailImage();
156-
157-
$this->imageFactory->create($mediaDir->getAbsolutePath().$previewImage);
158-
159-
// Remove the preview image from the media directory
160-
if ($mediaDir->isExist($previewImage)) {
161-
$mediaDir->delete($previewImage);
162-
}
163-
164-
$this->imageFactory->create($mediaDir->getAbsolutePath().$previewThumbImage);
165-
166-
if ($mediaDir->isExist($previewThumbImage)) {
167-
$mediaDir->delete($previewThumbImage);
168-
}
169-
$this->mediaStorage->deleteFile($previewImage);
170-
$this->mediaStorage->deleteFile($previewThumbImage);
155+
$this->deletePreviewImage($previewImage);
156+
$this->deletePreviewImage($previewThumbImage);
171157
} catch (\Exception $exception) {
172158
throw new CouldNotDeleteException(
173159
__('Could not delete the Template: %1', $exception->getMessage())
@@ -177,6 +163,32 @@ public function delete(TemplateInterface $template) : bool
177163
return true;
178164
}
179165

166+
/**
167+
* Checks if preview image is valid and tries to delete it
168+
*
169+
* @param string $imageName
170+
* @return void
171+
* @throws FileSystemException
172+
*/
173+
private function deletePreviewImage(string $imageName): void
174+
{
175+
$isValid = true;
176+
$mediaDir = $this->filesystem
177+
->getDirectoryWrite(DirectoryList::MEDIA);
178+
179+
try {
180+
$this->imageFactory->create($mediaDir->getAbsolutePath().$imageName);
181+
} catch (\Exception) {
182+
$isValid = false;
183+
}
184+
185+
if ($mediaDir->isExist($imageName) && $isValid) {
186+
$mediaDir->delete($imageName);
187+
}
188+
189+
$this->mediaStorage->deleteFile($imageName);
190+
}
191+
180192
/**
181193
* @inheritdoc
182194
*/

0 commit comments

Comments
 (0)