9
9
10
10
use Magento \Framework \Api \SearchCriteria \CollectionProcessorInterface ;
11
11
use Magento \Framework \Api \SearchCriteriaInterface ;
12
+ use Magento \Framework \App \Filesystem \DirectoryList ;
12
13
use Magento \Framework \Exception \CouldNotDeleteException ;
13
14
use Magento \Framework \Exception \CouldNotSaveException ;
15
+ use Magento \Framework \Exception \FileSystemException ;
14
16
use Magento \Framework \Exception \NoSuchEntityException ;
15
17
use Magento \Framework \Filesystem ;
16
18
use Magento \Framework \Image \Factory ;
@@ -144,30 +146,14 @@ public function getList(SearchCriteriaInterface $criteria)
144
146
*/
145
147
public function delete (TemplateInterface $ template ) : bool
146
148
{
147
- $ mediaDir = $ this ->filesystem
148
- ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA );
149
-
150
149
try {
151
150
$ templateModel = $ this ->templateFactory ->create ();
152
151
$ this ->resource ->load ($ templateModel , $ template ->getTemplateId ());
153
152
$ this ->resource ->delete ($ templateModel );
154
153
$ previewImage = $ template ->getPreviewImage ();
155
154
$ 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 );
171
157
} catch (\Exception $ exception ) {
172
158
throw new CouldNotDeleteException (
173
159
__ ('Could not delete the Template: %1 ' , $ exception ->getMessage ())
@@ -177,6 +163,32 @@ public function delete(TemplateInterface $template) : bool
177
163
return true ;
178
164
}
179
165
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
+
180
192
/**
181
193
* @inheritdoc
182
194
*/
0 commit comments