You are here

final class DeveloperAppNameConverter in Apigee Edge 8

Resolves "developer_app_by_name" type parameters in routes.

Hierarchy

Expanded class hierarchy of DeveloperAppNameConverter

See also

\Drupal\apigee_edge\Entity\DeveloperApp::urlRouteParameters()

\Drupal\apigee_edge\Routing\DeveloperAppByNameRouteAlterSubscriber

1 string reference to 'DeveloperAppNameConverter'
apigee_edge.services.yml in ./apigee_edge.services.yml
apigee_edge.services.yml
1 service uses DeveloperAppNameConverter
paramconverter.developer_app_name in ./apigee_edge.services.yml
Drupal\apigee_edge\ParamConverter\DeveloperAppNameConverter

File

src/ParamConverter/DeveloperAppNameConverter.php, line 36

Namespace

Drupal\apigee_edge\ParamConverter
View source
final class DeveloperAppNameConverter implements ParamConverterInterface {

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

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

  /**
   * Constructs a DeveloperAppNameParameterConverter.
   *
   * @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['user'])) {
      return NULL;
    }
    $entity = NULL;

    /** @var \Drupal\user\UserInterface $user */

    // If {user} parameter is before the {app} in the route then
    // entity parameter converter should have already up-casted it to
    // a user object if not then let's try to up-cast it here.
    $user = is_object($defaults['user']) ? $defaults['user'] : $this->entityTypeManager
      ->getStorage('user')
      ->load($defaults['user']);
    if ($user) {
      $developer_id = $user
        ->get('apigee_edge_developer_id')->value;
      if ($developer_id) {
        $app_storage = $this->entityTypeManager
          ->getStorage('developer_app');
        $app_ids = $app_storage
          ->getQuery()
          ->condition('developerId', $developer_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 developer app with %name name owned by %email.', [
            '%class' => get_called_class(),
            '%name' => $value,
            '%email' => $user
              ->getEmail(),
          ]);
        }
      }
      else {

        // Developer does not exists (anymore) on Apigee Edge however it seems
        // it has existed before because someone knows the URL of the view
        // app page of one of its app.
        $this->logger
          ->critical('%class: Unable to find developer id for %user user.', [
          '%class' => get_called_class(),
          '%user' => $user
            ->getDisplayName(),
        ]);
        throw new DeveloperDoesNotExistException($user
          ->getEmail());
      }
    }
    return $entity;
  }

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

}

Members

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