You are here

class RedirectDestination in Bakery Single Sign-On System 8.2

Hierarchy

Expanded class hierarchy of RedirectDestination

1 string reference to 'RedirectDestination'
bakery.services.yml in ./bakery.services.yml
bakery.services.yml
1 service uses RedirectDestination
bakery.redirect in ./bakery.services.yml
Drupal\bakery\RedirectDestination

File

src/RedirectDestination.php, line 12

Namespace

Drupal\bakery
View source
class RedirectDestination implements RedirectDestinationInterface {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * Bakery settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Destination service.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface
   */
  protected $redirectDestination;

  /**
   * The destination used by the current request.
   *
   * @var \Drupal\Core\Url|string
   */
  protected $destination;

  /**
   * Constructs a new RedirectDestination instance.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The URL generator.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Configuration factory.
   * @param \Drupal\Core\Routing\RedirectDestinationInterface|null $destination
   *   Core destination redirect destination service.
   */
  public function __construct(RequestStack $request_stack, UrlGeneratorInterface $url_generator, ConfigFactoryInterface $config_factory, RedirectDestinationInterface $destination = NULL) {
    $this->requestStack = $request_stack;
    $this->urlGenerator = $url_generator;
    $this->config = $config_factory
      ->get('bakery.settings');
    $this->redirectDestination = $destination ?? \Drupal::destination();
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    if (!isset($this->destination)) {
      if ($this->config
        ->get('bakery_is_master')) {
        $this->destination = $this
          ->getChildDestination();
      }
      else {
        $this->destination = $this
          ->getParentDestination();
      }
    }
    if ($this->destination instanceof Url) {
      return $this->destination
        ->toString();
    }
    return $this->destination;
  }

  /**
   * Get link to destination on current site for parent.
   *
   * @return \Drupal\Core\Url
   */
  private function getParentDestination() {
    $query = $this->requestStack
      ->getCurrentRequest()->query;
    if ($query
      ->has('bd')) {

      // @TODO do something?
    }
    return Url::fromRoute('<current>')
      ->setAbsolute(TRUE);
  }

  /**
   * Get link to destination on referring child.
   *
   * @return \Drupal\Core\Url
   */
  private function getChildDestination() {
    $query = $this->requestStack
      ->getCurrentRequest()->query;
    if ($query
      ->has('bd')) {
      $external_uri = $query
        ->get('bd');
      if (is_string($external_uri) && ($external_uri = UrlHelper::stripDangerousProtocols($external_uri))) {
        foreach ($this->config
          ->get('bakery_slaves') as $subsite) {
          if (str_starts_with($external_uri, $subsite)) {
            return Url::fromUri(urldecode($external_uri))
              ->setAbsolute(TRUE);
          }
        }
      }
    }
    return Url::fromRoute('<front>')
      ->setAbsolute(TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function getAsArray() {

    // TODO...
    return [
      'bd' => $this
        ->get(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function set($new_destination) {
    $this->destination = $new_destination;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RedirectDestination::$config protected property Bakery settings.
RedirectDestination::$destination protected property The destination used by the current request.
RedirectDestination::$redirectDestination protected property Destination service.
RedirectDestination::$requestStack protected property The request stack.
RedirectDestination::$urlGenerator protected property The URL generator.
RedirectDestination::get public function Gets the destination as a path. Overrides RedirectDestinationInterface::get
RedirectDestination::getAsArray public function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. Overrides RedirectDestinationInterface::getAsArray
RedirectDestination::getChildDestination private function Get link to destination on referring child.
RedirectDestination::getParentDestination private function Get link to destination on current site for parent.
RedirectDestination::set public function Sets the destination as URL. Overrides RedirectDestinationInterface::set
RedirectDestination::__construct public function Constructs a new RedirectDestination instance.