You are here

class BrokenLinkSubscriber in Broken Link 8

Same name and namespace in other branches
  1. 8.3 src/EventSubscriber/BrokenLinkSubscriber.php \Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber
  2. 8.2 src/EventSubscriber/BrokenLinkSubscriber.php \Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber

Class BrokenLinkSubscriber.

@package Drupal\broken_link

Hierarchy

  • class \Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of BrokenLinkSubscriber

1 string reference to 'BrokenLinkSubscriber'
broken_link.services.yml in ./broken_link.services.yml
broken_link.services.yml
1 service uses BrokenLinkSubscriber
broken_link.broken_link in ./broken_link.services.yml
Drupal\broken_link\EventSubscriber\BrokenLinkSubscriber

File

src/EventSubscriber/BrokenLinkSubscriber.php, line 17

Namespace

Drupal\broken_link\EventSubscriber
View source
class BrokenLinkSubscriber implements EventSubscriberInterface {

  /**
   * Constructor.
   */
  public function __construct() {
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events['kernel.exception'] = [
      'handleBrokenLink',
    ];
    return $events;
  }

  /**
   * Method is called whenever the kernel.exception event is dispatched.
   *
   * Logs the broken link hit rate. And redirect to path configured, if broken
   * link matches any pattern defined.
   *
   * @param Event $event
   *   Event object.
   */
  public function handleBrokenLink(Event $event) {
    $exception = $event
      ->getException();
    if ($exception instanceof NotFoundHttpException) {
      $request_path = rtrim($event
        ->getRequest()
        ->getPathInfo(), '/');
      $broken_link = new BrokenLink([], 'broken_link');
      $broken_link
        ->merge($request_path);
      $broken_link_redirect = new BrokenLinkRedirect([], 'broken_link_redirect');
      $redirect_path = $broken_link_redirect
        ->getRedirectLink($request_path);
      if ($redirect_path) {
        $event
          ->setResponse(new RedirectResponse($redirect_path));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BrokenLinkSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
BrokenLinkSubscriber::handleBrokenLink public function Method is called whenever the kernel.exception event is dispatched.
BrokenLinkSubscriber::__construct public function Constructor.