LtiToolProviderEvent.php in LTI Tool Provider 8
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';
private $cancelled = false;
private $message;
public function isCancelled() : bool {
return $this->cancelled;
}
public function cancel(string $message = 'Launch has been cancelled.') : void {
$this->cancelled = true;
$this->message = $message;
$this
->stopPropagation();
}
public function getMessage() : string {
return $this->message;
}
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());
}
}
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();
}
}
}