You are here

class TeamContextManager in Apigee Edge 8

Describes the `apigee_edge_teams.context_manager` service.

This service is responsible for understanding the context of the current route. Context will either be developer context or team context.

Hierarchy

Expanded class hierarchy of TeamContextManager

1 string reference to 'TeamContextManager'
apigee_edge_teams.services.yml in modules/apigee_edge_teams/apigee_edge_teams.services.yml
modules/apigee_edge_teams/apigee_edge_teams.services.yml
1 service uses TeamContextManager
apigee_edge_teams.context_manager in modules/apigee_edge_teams/apigee_edge_teams.services.yml
Drupal\apigee_edge_teams\TeamContextManager

File

modules/apigee_edge_teams/src/TeamContextManager.php, line 33

Namespace

Drupal\apigee_edge_teams
View source
class TeamContextManager implements TeamContextManagerInterface {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * TeamContextManager constructor.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentContextEntity() : ?EntityInterface {
    $context = NULL;
    if ($current_route_object = $this->routeMatch
      ->getRouteObject()) {

      // Check for developer/user route.
      if ($current_route_object
        ->hasOption(static::TEAM_ROUTE_OPTION_NAME)) {
        $context = $this->routeMatch
          ->getParameter('user');
      }

      // Check for team route.
      if ($current_route_object
        ->hasOption(static::DEVELOPER_ROUTE_OPTION_NAME)) {
        $context = $this->routeMatch
          ->getParameter('team');
      }
    }
    return $context instanceof EntityInterface ? $context : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getDestinationUrlForEntity(EntityInterface $entity) : ?Url {
    if ($corresponding_route_name = $this
      ->getCorrespondingRouteNameForEntity($entity)) {

      // Rebuild parameters for current context.
      $parameters = array_diff_key($this->routeMatch
        ->getRawParameters()
        ->all(), [
        $entity
          ->getEntityTypeId() === 'user' ? 'team' : 'user' => NULL,
      ]);
      $parameters[$entity
        ->getEntityTypeId()] = $entity
        ->id();
      return Url::fromRoute($corresponding_route_name, $parameters);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getCorrespondingRouteNameForEntity(EntityInterface $entity) : ?string {
    if ($current_route_object = $this->routeMatch
      ->getRouteObject()) {

      // If the route has same parameter type as entity, return current route.
      if ($this->routeMatch
        ->getRawParameters()
        ->has($entity
        ->getEntityTypeId())) {
        return $this->routeMatch
          ->getRouteName();
      }

      // Otherwise return the corresponding route if set.
      return $current_route_object
        ->getOption(static::DEVELOPER_ROUTE_OPTION_NAME) ?? $current_route_object
        ->getOption(static::TEAM_ROUTE_OPTION_NAME);
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TeamContextManager::$routeMatch protected property The current route match.
TeamContextManager::getCorrespondingRouteNameForEntity public function Gets the corresponding route name for the given entity. Overrides TeamContextManagerInterface::getCorrespondingRouteNameForEntity
TeamContextManager::getCurrentContextEntity public function Determines the current context from the route. Overrides TeamContextManagerInterface::getCurrentContextEntity
TeamContextManager::getDestinationUrlForEntity public function Returns the destination url for the given entity. Overrides TeamContextManagerInterface::getDestinationUrlForEntity
TeamContextManager::__construct public function TeamContextManager constructor.
TeamContextManagerInterface::DEVELOPER_ROUTE_OPTION_NAME constant The name of the route option for developer.
TeamContextManagerInterface::TEAM_ROUTE_OPTION_NAME constant The name of the route option for team.