You are here

class TeamStatusWarningSubscriber in Apigee Edge 8

Displays a warning message on team app pages if app owner is inactive.

Hierarchy

Expanded class hierarchy of TeamStatusWarningSubscriber

1 string reference to 'TeamStatusWarningSubscriber'
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 TeamStatusWarningSubscriber
apigee_edge_teams.event_subscriber.team_status_warning_subscriber in modules/apigee_edge_teams/apigee_edge_teams.services.yml
Drupal\apigee_edge_teams\EventSubscriber\TeamStatusWarningSubscriber

File

modules/apigee_edge_teams/src/EventSubscriber/TeamStatusWarningSubscriber.php, line 38

Namespace

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

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

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

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  private $messenger;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  private $entityTypeManager;

  /**
   * TeamStatusWarningSubscriber constructor.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\apigee_edge_teams\TeamMembershipManagerInterface $team_membership_manager
   *   The team membership manager service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translations service.
   */
  public function __construct(AccountInterface $current_user, RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager, TeamMembershipManagerInterface $team_membership_manager, MessengerInterface $messenger, TranslationInterface $string_translation) {
    $this->routeMatch = $route_match;
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
    $this->messenger = $messenger;
    $this->stringTranslation = $string_translation;
  }

  /**
   * Display's a warning message if team's status is inactive.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {

    // Anonymous user's does not have access to these routes.
    if ($this->currentUser
      ->isAuthenticated() && strpos($this->routeMatch
      ->getRouteName(), 'entity.team_app.') === 0) {

      // Team is available in most of the team app routes as a route parameter.

      /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface|NULL $team */
      $team = $this->routeMatch
        ->getParameter('team');
      if ($team === NULL) {

        /** @var \Drupal\apigee_edge_teams\Entity\TeamAppInterface $app */
        $app = $this->routeMatch
          ->getParameter('team_app') ?? $this->routeMatch
          ->getParameter('app');
        if ($app) {
          $team = $this->entityTypeManager
            ->getStorage('team')
            ->load($app
            ->getCompanyName());
        }
      }
      if ($team && $team
        ->getStatus() === TeamInterface::STATUS_INACTIVE) {
        $this->messenger
          ->addWarning($this
          ->t('This @team has inactive status so @team members will not be able to use @team_app credentials until the @team gets activated. Please contact support for further assistance.', [
          '@team' => mb_strtolower($this->entityTypeManager
            ->getDefinition('team')
            ->getSingularLabel()),
          '@team_app' => mb_strtolower($this->entityTypeManager
            ->getDefinition('team_app')
            ->getSingularLabel()),
        ]));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // We had to increase the weight to get the current route and not the
    // referer.
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
      5,
    ];
    return $events;
  }

}

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.
TeamStatusWarningSubscriber::$currentUser private property The current user.
TeamStatusWarningSubscriber::$entityTypeManager private property The entity type manager.
TeamStatusWarningSubscriber::$messenger private property The messenger service.
TeamStatusWarningSubscriber::$routeMatch private property The route match service.
TeamStatusWarningSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TeamStatusWarningSubscriber::onRespond public function Display's a warning message if team's status is inactive.
TeamStatusWarningSubscriber::__construct public function TeamStatusWarningSubscriber constructor.