You are here

final class TeamAppNameConverter in Apigee Edge 8

Resolves "team_app_by_name" type parameters in routes.

Hierarchy

Expanded class hierarchy of TeamAppNameConverter

See also

\Drupal\apigee_edge_teams\Entity\TeamApp::urlRouteParameters()

\Drupal\apigee_edge_teams\Routing\TeamAppByNameRouteAlterSubscriber

1 string reference to 'TeamAppNameConverter'
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 TeamAppNameConverter
paramconverter.team_app_name in modules/apigee_edge_teams/apigee_edge_teams.services.yml
Drupal\apigee_edge_teams\ParamConverter\TeamAppNameConverter

File

modules/apigee_edge_teams/src/ParamConverter/TeamAppNameConverter.php, line 35

Namespace

Drupal\apigee_edge_teams\ParamConverter
View source
final class TeamAppNameConverter implements ParamConverterInterface {

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

  /**
   * The logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  private $logger;

  /**
   * Constructs a TeamAppNameConverter.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   The logger.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, LoggerChannelInterface $logger) {
    $this->entityTypeManager = $entity_type_manager;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public function convert($value, $definition, $name, array $defaults) {
    if (empty($defaults['team'])) {
      return NULL;
    }
    $entity = NULL;

    /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */

    // If {team} parameter is before the {team_app} in the route then
    // entity parameter converter should have already up-casted it to
    // a team object if not then let's try to up-cast it here.
    $team = is_object($defaults['team']) ? $defaults['team'] : $this->entityTypeManager
      ->getStorage('team')
      ->load($defaults['team']);
    if ($team) {
      $app_storage = $this->entityTypeManager
        ->getStorage('team_app');
      $app_ids = $app_storage
        ->getQuery()
        ->condition('companyName', $team
        ->id())
        ->condition('name', $value)
        ->execute();
      if (!empty($app_ids)) {
        $app_id = reset($app_ids);

        // Load the entity directly from Apigee Edge if needed.
        // @see \Drupal\apigee_edge\ParamConverter\ApigeeEdgeLoadUnchangedEntity
        if (!empty($defaults['_route_object']
          ->getOption('apigee_edge_load_unchanged_entity'))) {
          if ($app_storage instanceof AppStorage) {
            $entity = $app_storage
              ->loadUnchangedByUuid($app_id);
          }
          else {
            $entity = $app_storage
              ->loadUnchanged($app_id);
          }
        }
        else {
          $entity = $app_storage
            ->load($app_id);
        }
      }
      if ($entity === NULL) {

        // App may have been deleted on Apigee Edge, that is a smaller
        // problem.
        $this->logger
          ->error('%class: Unable to load team app with %name name owned by %team team.', [
          '%class' => get_called_class(),
          '%name' => $value,
          '%team' => $team
            ->id(),
        ]);
      }
    }
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function applies($definition, $name, Route $route) {
    return !empty($definition['type']) && $definition['type'] == 'team_app_by_name';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TeamAppNameConverter::$entityTypeManager protected property The entity type manager.
TeamAppNameConverter::$logger private property The logger.
TeamAppNameConverter::applies public function Determines if the converter applies to a specific route and variable. Overrides ParamConverterInterface::applies
TeamAppNameConverter::convert public function Converts path variables to their corresponding objects. Overrides ParamConverterInterface::convert
TeamAppNameConverter::__construct public function Constructs a TeamAppNameConverter.