You are here

class TeamInactiveStatusSubscriber in Apigee Edge 8

Displays an error message on team pages if team is inactive.

Hierarchy

Expanded class hierarchy of TeamInactiveStatusSubscriber

1 file declares its use of TeamInactiveStatusSubscriber
apigee_edge_teams.module in modules/apigee_edge_teams/apigee_edge_teams.module
Copyright 2018 Google Inc.
1 string reference to 'TeamInactiveStatusSubscriber'
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 TeamInactiveStatusSubscriber
apigee_edge_teams.team_inactive_status_subscriber in modules/apigee_edge_teams/apigee_edge_teams.services.yml
Drupal\apigee_edge_teams\EventSubscriber\TeamInactiveStatusSubscriber

File

modules/apigee_edge_teams/src/EventSubscriber/TeamInactiveStatusSubscriber.php, line 36

Namespace

Drupal\apigee_edge_teams\EventSubscriber
View source
class TeamInactiveStatusSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The class resolver service.
   *
   * @var \Drupal\Core\Controller\ControllerResolverInterface
   */
  protected $classResolver;

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

  /**
   * The available main content renderer services, keyed per format.
   *
   * @var array
   */
  protected $mainContentRenderers;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * TeamInactiveStatusSubscriber constructor.
   *
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
   *   The class resolver service.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match service.
   * @param array $main_content_renderers
   *   The available main content renderer service IDs.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(ClassResolverInterface $class_resolver, RouteMatchInterface $route_match, array $main_content_renderers, AccountInterface $current_user) {
    $this->classResolver = $class_resolver;
    $this->routeMatch = $route_match;
    $this->mainContentRenderers = $main_content_renderers;
    $this->currentUser = $current_user;
  }

  /**
   * Display an error message on inactive team routes.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {
    if ($this->currentUser
      ->isAnonymous() || !in_array($this->routeMatch
      ->getRouteName(), $this
      ->getDisabledRoutes())) {
      return;
    }

    /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */
    $team = $this->routeMatch
      ->getParameter('team');
    if (!$team || $team
      ->getStatus() !== TeamInterface::STATUS_INACTIVE) {
      return;
    }
    $content = [
      'content' => [
        '#theme' => 'status_messages',
        '#message_list' => [
          'error' => [
            $this
              ->t('The %team_name @team is inactive. This operation is now allowed.', [
              '%team_name' => $team
                ->label(),
              '@team' => $team
                ->getEntityType()
                ->getSingularLabel(),
            ]),
          ],
        ],
      ],
    ];
    $renderer = $this->classResolver
      ->getInstanceFromDefinition($this->mainContentRenderers['html']);

    /* @var \Symfony\Component\HttpFoundation\Response $response */
    $response = $renderer
      ->renderResponse($content, $event
      ->getRequest(), $this->routeMatch);
    $response
      ->setStatusCode(Response::HTTP_FORBIDDEN);
    $event
      ->setResponse($response);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
      5,
    ];
    return $events;
  }

  /**
   * Returns an array of route names for which the warning message should be displayed.
   *
   * @return array
   *   An array of route names.
   */
  public static function getDisabledRoutes() : array {
    return [
      // Team.
      'entity.team.edit_form',
      'entity.team.delete_form',
      // Team app.
      'entity.team_app.add_form_for_team',
      'entity.team_app.edit_form',
      'entity.team_app.delete_form',
      'entity.team_app.analytics',
      // Team member.
      'entity.team.add_members',
      'entity.team.member.edit',
      'entity.team.member.remove',
      // Team invitation.
      'entity.team_invitation.resend_form',
      'entity.team_invitation.delete_form',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TeamInactiveStatusSubscriber::$classResolver protected property The class resolver service.
TeamInactiveStatusSubscriber::$currentUser protected property The current user.
TeamInactiveStatusSubscriber::$mainContentRenderers protected property The available main content renderer services, keyed per format.
TeamInactiveStatusSubscriber::$routeMatch protected property The route match service.
TeamInactiveStatusSubscriber::getDisabledRoutes public static function Returns an array of route names for which the warning message should be displayed.
TeamInactiveStatusSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TeamInactiveStatusSubscriber::onRespond public function Display an error message on inactive team routes.
TeamInactiveStatusSubscriber::__construct public function TeamInactiveStatusSubscriber constructor.