You are here

class ShurlySubscriber in ShURLy 8

Hierarchy

  • class \Drupal\shurly\ShurlySubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ShurlySubscriber

1 string reference to 'ShurlySubscriber'
shurly.services.yml in ./shurly.services.yml
shurly.services.yml
1 service uses ShurlySubscriber
shurly.shurly_subscriber in ./shurly.services.yml
Drupal\shurly\ShurlySubscriber

File

src/ShurlySubscriber.php, line 11

Namespace

Drupal\shurly
View source
class ShurlySubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'shurlyOnRespond',
      0,
    ];
    return $events;
  }
  public function shurlyOnRespond(GetResponseEvent $event) {
    $path = \Drupal::service('path.current')
      ->getPath();
    $current_path = str_replace("/", "", $path);
    if (shurly_validate_custom($current_path)) {
      $row = \Drupal::database()
        ->query("SELECT rid, destination, active FROM {shurly} WHERE source = :q ", [
        ':q' => $current_path,
      ])
        ->fetchObject();
      if ($row) {
        if ($row->active == 1) {
          $this
            ->shurlyRedirectTo($row);
        }
        else {

          // Check if the redirect page is defined. Otherwise the request will
          // be handled normally by drupal.
          $shurly_redirect_url = trim(\Drupal::config('shurly.settings')
            ->get('shurly_redirect_page'));
          if (!empty($shurly_redirect_url)) {
            $url = Url::fromUserInput($shurly_redirect_url);
            $response = new RedirectResponse($url
              ->toString());
            $response
              ->send();
            return;
          }
        }
      }
    }
  }
  protected function shurlyRedirectTo($row) {
    \Drupal::moduleHandler()
      ->invokeAll('shurly_redirect_before', [
      $row,
    ]);
    $url = $row->destination;
    $url = str_replace([
      "\n",
      "\r",
    ], '', $url);
    session_write_close();
    $response = new RedirectResponse($url);
    $response
      ->send();
    $request_time = \Drupal::time()
      ->getRequestTime();
    \Drupal::database()
      ->query('UPDATE {shurly} SET count = count + 1, last_used = :time WHERE rid = :rid', [
      'time' => $request_time,
      'rid' => $row->rid,
    ]);
    \Drupal::moduleHandler()
      ->invokeAll('shurly_redirect_after', [
      $row,
    ]);
    exit;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ShurlySubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ShurlySubscriber::shurlyOnRespond public function
ShurlySubscriber::shurlyRedirectTo protected function