Skip to content

Commit

Permalink
Merge branch '2.4-develop' into Refactoring-AdminCreateSimpleProductZ…
Browse files Browse the repository at this point in the history
…eroPriceTest
  • Loading branch information
kate-kyzyma committed Mar 18, 2021
2 parents febd90d + 986f564 commit ddfc87a
Show file tree
Hide file tree
Showing 141 changed files with 10,340 additions and 546 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
namespace Magento\Catalog\Controller\Adminhtml\Product;

use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Ui\Component\MassAction\Filter;
Expand All @@ -20,7 +23,7 @@
/**
* Class \Magento\Catalog\Controller\Adminhtml\Product\MassDelete
*/
class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product implements HttpPostActionInterface
class MassDelete extends Product implements HttpPostActionInterface
{
/**
* Massactions filter
Expand Down Expand Up @@ -49,8 +52,8 @@ class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product implement
* @param Builder $productBuilder
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param ProductRepositoryInterface $productRepository
* @param LoggerInterface $logger
* @param ProductRepositoryInterface|null $productRepository
* @param LoggerInterface|null $logger
*/
public function __construct(
Context $context,
Expand All @@ -63,20 +66,23 @@ public function __construct(
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->productRepository = $productRepository ?:
\Magento\Framework\App\ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
$this->logger = $logger ?:
\Magento\Framework\App\ObjectManager::getInstance()->create(LoggerInterface::class);
ObjectManager::getInstance()->create(LoggerInterface::class);
parent::__construct($context, $productBuilder);
}

/**
* Mass Delete Action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @return Redirect
* @throws LocalizedException
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$collection->addMediaGalleryData();

$productDeleted = 0;
$productDeletedError = 0;
/** @var \Magento\Catalog\Model\Product $product */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\ResourceModel;

use Exception;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Gallery\Processor;
use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig;
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem;
use Psr\Log\LoggerInterface;

/**
* Process media gallery and delete media image after product delete
*/
class MediaImageDeleteProcessor
{
/**
* @var MediaConfig
*/
private $imageConfig;

/**
* @var Filesystem
*/
private $mediaDirectory;

/**
* @var Processor
*/
private $imageProcessor;

/**
* @var Gallery
*/
private $productGallery;

/**
* @var LoggerInterface
*/
private $logger;

/**
* Product constructor.
*
* @param MediaConfig $imageConfig
* @param Filesystem $filesystem
* @param Processor $imageProcessor
* @param Gallery $productGallery
* @param LoggerInterface $logger
* @throws FileSystemException
*/
public function __construct(
MediaConfig $imageConfig,
Filesystem $filesystem,
Processor $imageProcessor,
Gallery $productGallery,
LoggerInterface $logger
) {
$this->imageConfig = $imageConfig;
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->imageProcessor = $imageProcessor;
$this->productGallery = $productGallery;
$this->logger = $logger;
}

/**
* Process $product data and remove image from gallery after product delete
*
* @param DataObject $product
* @return void
*/
public function execute(DataObject $product): void
{
try {
$productImages = $product->getMediaGalleryImages();
foreach ($productImages as $image) {
$imageFile = $image->getFile();
if ($imageFile) {
$this->deleteProductImage($image, $product, $imageFile);
}
}
} catch (Exception $e) {
$this->logger->critical($e);
}
}

/**
* Check if image exists and is not used by any other products
*
* @param string $file
* @return bool
*/
private function canDeleteImage(string $file): bool
{
return $this->productGallery->countImageUses($file) <= 1;
}

/**
* Delete the physical image if it's existed and not used by other products
*
* @param string $imageFile
* @param string $filePath
* @throws FileSystemException
*/
private function deletePhysicalImage(string $imageFile, string $filePath): void
{
if ($this->canDeleteImage($imageFile)) {
$this->mediaDirectory->delete($filePath);
}
}

/**
* Remove product image
*
* @param DataObject $image
* @param ProductInterface $product
* @param string $imageFile
*/
private function deleteProductImage(
DataObject $image,
ProductInterface $product,
string $imageFile
): void {
$catalogPath = $this->imageConfig->getBaseMediaPath();
$filePath = $catalogPath . $imageFile;

if ($this->mediaDirectory->isFile($filePath)) {
try {
$this->productGallery->deleteGallery($image->getValueId());
$this->imageProcessor->removeImage($product, $imageFile);
$this->deletePhysicalImage($imageFile, $filePath);
} catch (Exception $e) {
$this->logger->critical($e);
}
}
}
}
23 changes: 22 additions & 1 deletion app/code/Magento/Catalog/Model/ResourceModel/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ class Product extends AbstractResource
*/
private $eavAttributeManagement;

/**
* @var MediaImageDeleteProcessor
*/
private $mediaImageDeleteProcessor;

/**
* @param \Magento\Eav\Model\Entity\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
Expand All @@ -114,6 +119,7 @@ class Product extends AbstractResource
* @param TableMaintainer|null $tableMaintainer
* @param UniqueValidationInterface|null $uniqueValidator
* @param AttributeManagementInterface|null $eavAttributeManagement
* @param MediaImageDeleteProcessor|null $mediaImageDeleteProcessor
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -129,7 +135,8 @@ public function __construct(
$data = [],
TableMaintainer $tableMaintainer = null,
UniqueValidationInterface $uniqueValidator = null,
AttributeManagementInterface $eavAttributeManagement = null
AttributeManagementInterface $eavAttributeManagement = null,
?MediaImageDeleteProcessor $mediaImageDeleteProcessor = null
) {
$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_catalogCategory = $catalogCategory;
Expand All @@ -148,6 +155,8 @@ public function __construct(
$this->tableMaintainer = $tableMaintainer ?: ObjectManager::getInstance()->get(TableMaintainer::class);
$this->eavAttributeManagement = $eavAttributeManagement
?? ObjectManager::getInstance()->get(AttributeManagementInterface::class);
$this->mediaImageDeleteProcessor = $mediaImageDeleteProcessor
?? ObjectManager::getInstance()->get(MediaImageDeleteProcessor::class);
}

/**
Expand Down Expand Up @@ -819,4 +828,16 @@ protected function getAttributeRow($entity, $object, $attribute)
$data['store_id'] = $object->getStoreId();
return $data;
}

/**
* After delete entity process
*
* @param DataObject $object
* @return $this
*/
protected function _afterDelete(DataObject $object)
{
$this->mediaImageDeleteProcessor->execute($object);
return parent::_afterDelete($object);
}
}
Loading

0 comments on commit ddfc87a

Please sign in to comment.