You are here

LtiToolProviderEvent.php in LTI Tool Provider 2.x

Same filename and directory in other branches
  1. 8 src/LtiToolProviderEvent.php

File

src/LtiToolProviderEvent.php
View source
<?php

namespace Drupal\lti_tool_provider;

use Drupal\Core\Url;
use Exception;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class LtiToolProviderEvent extends Event {
  const EVENT_NAME = 'LTI_TOOL_PROVIDER_EVENT';

  /**
   * @var bool
   */
  private $cancelled = FALSE;

  /**
   * @var string
   */
  private $message;

  /**
   * Dispatch an LTI Tool Provider event.
   *
   * @param EventDispatcherInterface $eventDispatcher
   *   The event dispatcher.
   * @param LtiToolProviderEvent $event
   *   The event to dispatch.
   *
   * @throws Exception
   */
  static function dispatchEvent(EventDispatcherInterface $eventDispatcher, LtiToolProviderEvent &$event) {
    $event = $eventDispatcher
      ->dispatch($event::EVENT_NAME, $event);
    if ($event instanceof LtiToolProviderEvent && $event
      ->isCancelled()) {
      throw new Exception($event
        ->getMessage());
    }
  }

  /**
   * @return bool
   */
  public function isCancelled() : bool {
    return $this->cancelled;
  }

  /**
   * @return string
   */
  public function getMessage() : string {
    return $this->message;
  }
  public function cancel(string $message = 'Launch has been cancelled.') : void {
    $this->cancelled = TRUE;
    $this->message = $message;
    $this
      ->stopPropagation();
  }

  /**
   * Send an error back to the LMS.
   *
   * @param array $context
   *   The LTI context.
   * @param string $message
   *   The error message to send.
   */
  public function sendLtiError(array $context, string $message) {
    if (isset($context['launch_presentation_return_url']) && !empty($context['launch_presentation_return_url'])) {
      $url = Url::fromUri($context['launch_presentation_return_url'])
        ->setOption('query', [
        'lti_errormsg' => $message,
      ])
        ->setAbsolute(TRUE)
        ->toString();
      $response = new RedirectResponse($url);
      $response
        ->send();
    }
  }

}

Classes