LTIToolProviderController.php in LTI Tool Provider 8
File
src/Controller/LTIToolProviderController.php
View source
<?php
namespace Drupal\lti_tool_provider\Controller;
use Drupal;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Drupal\Core\PageCache\ResponsePolicyInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\lti_tool_provider\Event\LtiToolProviderLaunchRedirectEvent;
use Drupal\lti_tool_provider\Event\LtiToolProviderReturnEvent;
use Drupal\lti_tool_provider\LtiToolProviderEvent;
use Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class LTIToolProviderController extends ControllerBase {
protected $configFactory;
protected $loggerFactory;
protected $eventDispatcher;
protected $killSwitch;
protected $request;
protected $session;
protected $context;
protected $destination;
public function __construct(ConfigFactoryInterface $configFactory, LoggerChannelFactoryInterface $loggerFactory, EventDispatcherInterface $eventDispatcher, ResponsePolicyInterface $killSwitch, Request $request, SessionInterface $session, $context, ?string $destination) {
$this->configFactory = $configFactory;
$this->loggerFactory = $loggerFactory
->get('lti_tool_provider');
$this->eventDispatcher = $eventDispatcher;
$this->killSwitch = $killSwitch;
$this->request = $request;
$this->session = $session;
$this->context = $context;
$this->destination = $destination;
}
public static function create(ContainerInterface $container) : LTIToolProviderController {
$configFactory = $container
->get('config.factory');
$loggerFactory = $container
->get('logger.factory');
$eventDispatcher = $container
->get('event_dispatcher');
$killSwitch = $container
->get('page_cache_kill_switch');
$request = Drupal::request();
$session = $request
->getSession();
$context = $session
->get('lti_tool_provider_context');
$destination = Drupal::config('lti_tool_provider.settings')
->get('destination');
return new static($configFactory, $loggerFactory, $eventDispatcher, $killSwitch, $request, $session, $context, $destination);
}
public function ltiLaunch() : RedirectResponse {
try {
$destination = '/';
if (empty($this->context)) {
throw new Exception('LTI context missing.');
}
if (!empty($this->destination)) {
$destination = $this->destination;
}
if (isset($this->context['custom_destination']) && !empty($this->context['custom_destination'])) {
$destination = $this->context['custom_destination'];
}
$this->killSwitch
->trigger();
$event = new LtiToolProviderLaunchRedirectEvent($this->context, $destination);
LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $event);
if ($event
->isCancelled()) {
throw new Exception($event
->getMessage());
}
$destination = $event
->getDestination();
return new RedirectResponse($destination);
} catch (Exception $e) {
$this->loggerFactory
->warning($e
->getMessage());
return new RedirectResponse('/', 500);
}
}
public function ltiReturn() {
try {
$destination = '/';
if (empty($this->context)) {
throw new Exception('LTI context missing in return request.');
}
if (!empty($this->destination)) {
$destination = $this->destination;
}
if (isset($this->context['launch_presentation_return_url']) && !empty($this->context['launch_presentation_return_url'])) {
$destination = $this->context['launch_presentation_return_url'];
}
$this->killSwitch
->trigger();
$event = new LtiToolProviderReturnEvent($this->context, $destination);
LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $event);
if ($event
->isCancelled()) {
throw new Exception($event
->getMessage());
}
$destination = $event
->getDestination();
$this
->userLogout();
return new TrustedRedirectResponse($destination);
} catch (Exception $e) {
$this->loggerFactory
->warning($e
->getMessage());
return new RedirectResponse('/', 500);
}
}
public function access() : AccessResult {
return AccessResult::allowedIf(!empty($this->context));
}
protected function userLogout() {
user_logout();
}
}