CleanImages.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Backend\Controller\Adminhtml\Cache;
  8. use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Controller\ResultFactory;
  11. class CleanImages extends \Magento\Backend\Controller\Adminhtml\Cache implements HttpGetActionInterface
  12. {
  13. /**
  14. * Authorization level of a basic admin session
  15. *
  16. * @see _isAllowed()
  17. */
  18. const ADMIN_RESOURCE = 'Magento_Backend::flush_catalog_images';
  19. /**
  20. * Clean JS/css files cache
  21. *
  22. * @return \Magento\Backend\Model\View\Result\Redirect
  23. */
  24. public function execute()
  25. {
  26. try {
  27. $this->_objectManager->create(\Magento\Catalog\Model\Product\Image::class)->clearCache();
  28. $this->_eventManager->dispatch('clean_catalog_images_cache_after');
  29. $this->messageManager->addSuccessMessage(__('The image cache was cleaned.'));
  30. } catch (LocalizedException $e) {
  31. $this->messageManager->addErrorMessage($e->getMessage());
  32. } catch (\Exception $e) {
  33. $this->messageManager->addExceptionMessage($e, __('An error occurred while clearing the image cache.'));
  34. }
  35. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  36. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  37. return $resultRedirect->setPath('adminhtml/*');
  38. }
  39. }