CleanThemeRelatedContentObserver.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Observer;
  7. use Magento\Framework\Event\Observer as EventObserver;
  8. use Magento\Framework\Event\ObserverInterface;
  9. use Magento\Theme\Model\Theme;
  10. class CleanThemeRelatedContentObserver implements ObserverInterface
  11. {
  12. /**
  13. * @var \Magento\Framework\View\Design\Theme\ImageFactory
  14. */
  15. protected $themeImageFactory;
  16. /**
  17. * @var \Magento\Widget\Model\ResourceModel\Layout\Update\Collection
  18. */
  19. protected $updateCollection;
  20. /**
  21. * @var \Magento\Theme\Model\Config\Customization
  22. */
  23. protected $themeConfig;
  24. /**
  25. * @param \Magento\Framework\View\Design\Theme\ImageFactory $themeImageFactory
  26. * @param \Magento\Widget\Model\ResourceModel\Layout\Update\Collection $updateCollection
  27. * @param \Magento\Theme\Model\Config\Customization $themeConfig
  28. */
  29. public function __construct(
  30. \Magento\Framework\View\Design\Theme\ImageFactory $themeImageFactory,
  31. \Magento\Widget\Model\ResourceModel\Layout\Update\Collection $updateCollection,
  32. \Magento\Theme\Model\Config\Customization $themeConfig
  33. ) {
  34. $this->themeImageFactory = $themeImageFactory;
  35. $this->updateCollection = $updateCollection;
  36. $this->themeConfig = $themeConfig;
  37. }
  38. /**
  39. * Clean related contents to a theme (before save)
  40. *
  41. * @param EventObserver $observer
  42. * @return void
  43. * @throws \Magento\Framework\Exception\LocalizedException
  44. */
  45. public function execute(EventObserver $observer)
  46. {
  47. $theme = $observer->getEvent()->getData('theme');
  48. if (!($theme instanceof \Magento\Framework\View\Design\ThemeInterface)) {
  49. return;
  50. }
  51. /** @var $theme \Magento\Framework\View\Design\ThemeInterface */
  52. if ($this->themeConfig->isThemeAssignedToStore($theme)) {
  53. throw new \Magento\Framework\Exception\LocalizedException(__('Theme isn\'t deletable.'));
  54. }
  55. $this->themeImageFactory->create(['theme' => $theme])->removePreviewImage();
  56. $this->updateCollection->addThemeFilter($theme->getId())->delete();
  57. }
  58. }