@@ -22,7 +22,7 @@ class Media extends \Magento\Framework\App\Helper\AbstractHelper
2222 * Swatch area inside media folder
2323 *
2424 */
25- const SWATCH_MEDIA_PATH = 'attribute/swatch ' ;
25+ public const SWATCH_MEDIA_PATH = 'attribute/swatch ' ;
2626
2727 /**
2828 * @var \Magento\Catalog\Model\Product\Media\Config
@@ -42,8 +42,6 @@ class Media extends \Magento\Framework\App\Helper\AbstractHelper
4242 protected $ fileStorageDb = null ;
4343
4444 /**
45- * Store manager
46- *
4745 * @var \Magento\Store\Model\StoreManagerInterface
4846 */
4947 protected $ storeManager ;
@@ -112,6 +110,8 @@ public function __construct(
112110 }
113111
114112 /**
113+ * Method to get swatch attribute image.
114+ *
115115 * @param string $swatchType
116116 * @param string $file
117117 * @return string
@@ -138,6 +138,14 @@ public function getSwatchAttributeImage($swatchType, $file)
138138 return $ basePath . '/ ' . $ this ->getRelativeTransformationParametersPath ($ swatchType , $ file );
139139 }
140140
141+ /**
142+ * Method to get relative transformation parameters path.
143+ *
144+ * @param string $swatchType
145+ * @param string $file
146+ * @return string
147+ * @throws \Magento\Framework\Exception\NoSuchEntityException
148+ */
141149 private function getRelativeTransformationParametersPath ($ swatchType , $ file )
142150 {
143151 $ imageConfig = $ this ->getImageConfig ();
@@ -150,14 +158,14 @@ private function getRelativeTransformationParametersPath($swatchType, $file)
150158 }
151159
152160 /**
153- * move image from tmp to catalog dir
161+ * Move image from tmp to catalog dir
154162 *
155163 * @param string $file
156164 * @return string path
157165 */
158166 public function moveImageFromTmp ($ file )
159167 {
160- if (strrpos ($ file , '.tmp ' ) == strlen ($ file ) - 4 ) {
168+ if ($ file && strrpos ($ file , '.tmp ' ) == strlen ($ file ) - 4 ) {
161169 $ file = substr ($ file , 0 , strlen ($ file ) - 4 );
162170 }
163171 $ destinationFile = $ this ->getUniqueFileName ($ file );
@@ -182,7 +190,7 @@ public function moveImageFromTmp($file)
182190 );
183191 }
184192
185- return str_replace ('\\' , '/ ' , $ destinationFile );
193+ return $ destinationFile !== null ? str_replace ('\\' , '/ ' , $ destinationFile ) : '' ;
186194 }
187195
188196 /**
@@ -199,6 +207,7 @@ protected function getUniqueFileName($file)
199207 $ file
200208 );
201209 } else {
210+ //phpcs:ignore Magento2.Functions.DiscouragedFunction
202211 $ destFile = dirname ($ file ) . '/ ' . \Magento \MediaStorage \Model \File \Uploader::getNewFileName (
203212 $ this ->getOriginalFilePath ($ file )
204213 );
@@ -207,6 +216,12 @@ protected function getUniqueFileName($file)
207216 return $ destFile ;
208217 }
209218
219+ /**
220+ * Method to get original file path.
221+ *
222+ * @param string $file
223+ * @return string
224+ */
210225 private function getOriginalFilePath ($ file )
211226 {
212227 return $ this ->mediaDirectory ->getAbsolutePath ($ this ->getAttributeSwatchPath ($ file ));
@@ -278,7 +293,7 @@ protected function generateNamePath($imageConfig, $imageUrl, $swatchType)
278293 * Generate folder name WIDTHxHEIGHT based on config in view.xml
279294 *
280295 * @param string $swatchType
281- * @param null $imageConfig
296+ * @param array| null $imageConfig
282297 * @return string
283298 */
284299 public function getFolderNameSize ($ swatchType , $ imageConfig = null )
@@ -314,7 +329,7 @@ public function getImageConfig()
314329 */
315330 protected function prepareFileName ($ imageUrl )
316331 {
317- $ fileArray = explode ('/ ' , $ imageUrl );
332+ $ fileArray = explode ('/ ' , $ imageUrl ?: '' );
318333 $ fileName = array_pop ($ fileArray );
319334 $ filePath = implode ('/ ' , $ fileArray );
320335 return ['name ' => $ fileName , 'path ' => $ filePath ];
@@ -372,6 +387,6 @@ public function getSwatchCachePath($swatchType)
372387 */
373388 protected function prepareFile ($ file )
374389 {
375- return ltrim (str_replace ('\\' , '/ ' , $ file ), '/ ' );
390+ return $ file !== null ? ltrim (str_replace ('\\' , '/ ' , $ file ), '/ ' ) : '' ;
376391 }
377392}
0 commit comments