PredispatchReviewObserver.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Review\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\Review\Block\Product\ReviewRenderer;
  13. use Magento\Store\Model\ScopeInterface;
  14. /**
  15. * Class PredispatchReviewObserver
  16. */
  17. class PredispatchReviewObserver implements ObserverInterface
  18. {
  19. /**
  20. * Configuration path to review active setting
  21. */
  22. const XML_PATH_REVIEW_ACTIVE = 'catalog/review/active';
  23. /**
  24. * @var ScopeConfigInterface
  25. */
  26. private $scopeConfig;
  27. /**
  28. * @var UrlInterface
  29. */
  30. private $url;
  31. /**
  32. * PredispatchReviewObserver constructor.
  33. *
  34. * @param ScopeConfigInterface $scopeConfig
  35. * @param UrlInterface $url
  36. */
  37. public function __construct(
  38. ScopeConfigInterface $scopeConfig,
  39. UrlInterface $url
  40. ) {
  41. $this->scopeConfig = $scopeConfig;
  42. $this->url = $url;
  43. }
  44. /**
  45. * Redirect review routes to 404 when review module is disabled.
  46. *
  47. * @param Observer $observer
  48. */
  49. public function execute(Observer $observer)
  50. {
  51. if (!$this->scopeConfig->getValue(
  52. self::XML_PATH_REVIEW_ACTIVE,
  53. ScopeInterface::SCOPE_STORE
  54. )
  55. ) {
  56. $defaultNoRouteUrl = $this->scopeConfig->getValue(
  57. 'web/default/no_route',
  58. ScopeInterface::SCOPE_STORE
  59. );
  60. $redirectUrl = $this->url->getUrl($defaultNoRouteUrl);
  61. $observer->getControllerAction()
  62. ->getResponse()
  63. ->setRedirect($redirectUrl);
  64. }
  65. }
  66. }