PredispatchNewsletterObserver.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types = 1);
  7. namespace Magento\Newsletter\Observer;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\Event\Observer;
  10. use Magento\Framework\Event\ObserverInterface;
  11. use Magento\Framework\UrlInterface;
  12. use Magento\Store\Model\ScopeInterface;
  13. /**
  14. * Class PredispatchNewsletterObserver
  15. */
  16. class PredispatchNewsletterObserver implements ObserverInterface
  17. {
  18. /**
  19. * Configuration path to newsletter active setting
  20. */
  21. const XML_PATH_NEWSLETTER_ACTIVE = 'newsletter/general/active';
  22. /**
  23. * @var ScopeConfigInterface
  24. */
  25. private $scopeConfig;
  26. /**
  27. * @var UrlInterface
  28. */
  29. private $url;
  30. /**
  31. * PredispatchNewsletterObserver constructor.
  32. *
  33. * @param ScopeConfigInterface $scopeConfig
  34. * @param UrlInterface $url
  35. */
  36. public function __construct(ScopeConfigInterface $scopeConfig, UrlInterface $url)
  37. {
  38. $this->scopeConfig = $scopeConfig;
  39. $this->url = $url;
  40. }
  41. /**
  42. * Redirect newsletter routes to 404 when newsletter module is disabled.
  43. *
  44. * @param Observer $observer
  45. */
  46. public function execute(Observer $observer) : void
  47. {
  48. if (!$this->scopeConfig->getValue(
  49. self::XML_PATH_NEWSLETTER_ACTIVE,
  50. ScopeInterface::SCOPE_STORE
  51. )
  52. ) {
  53. $defaultNoRouteUrl = $this->scopeConfig->getValue(
  54. 'web/default/no_route',
  55. ScopeInterface::SCOPE_STORE
  56. );
  57. $redirectUrl = $this->url->getUrl($defaultNoRouteUrl);
  58. $observer->getControllerAction()
  59. ->getResponse()
  60. ->setRedirect($redirectUrl);
  61. }
  62. }
  63. }