You are here

DefaultProfileLoad.php in Open Social 10.0.x

File

modules/social_features/social_profile/src/Plugin/GraphQL/DataProducer/DefaultProfileLoad.php
View source
<?php

namespace Drupal\social_profile\Plugin\GraphQL\DataProducer;

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\graphql\GraphQL\Buffers\EntityBuffer;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use GraphQL\Deferred;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Loads the default profile for a user.
 *
 * On many Open Social platforms there is only one profile.
 *
 * @DataProducer(
 *   id = "default_profile_load",
 *   name = @Translation("Load default profile"),
 *   description = @Translation("Loads the default profile for a user."),
 *   produces = @ContextDefinition("entity:profile",
 *     label = @Translation("Profile")
 *   ),
 *   consumes = {
 *     "user" = @ContextDefinition("entity:user",
 *       label = @Translation("User")
 *     )
 *   }
 * )
 */
class DefaultProfileLoad extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

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

  /**
   * The entity buffer service.
   *
   * @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer
   */
  protected $entityBuffer;

  /**
   * {@inheritdoc}
   *
   * @codeCoverageIgnore
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('graphql.buffer.entity'));
  }

  /**
   * EntityLoad constructor.
   *
   * @param array $configuration
   *   The plugin configuration array.
   * @param string $pluginId
   *   The plugin id.
   * @param array $pluginDefinition
   *   The plugin definition array.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   * @param \Drupal\graphql\GraphQL\Buffers\EntityBuffer $entityBuffer
   *   The entity buffer service.
   *
   * @codeCoverageIgnore
   */
  public function __construct(array $configuration, $pluginId, array $pluginDefinition, EntityTypeManagerInterface $entityTypeManager, EntityBuffer $entityBuffer) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->entityTypeManager = $entityTypeManager;
    $this->entityBuffer = $entityBuffer;
  }

  /**
   * Resolve to a value.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account to fetch the profile for.
   * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
   *   Cacheability metadata.
   *
   * @return \GraphQL\Deferred|null
   *   A promise that resolves to the loaded profile or null if the user does
   *   not have a published profile.
   */
  public function resolve(AccountInterface $account, RefinableCacheableDependencyInterface $metadata) {
    $storage = $this->entityTypeManager
      ->getStorage('profile');
    $type = $storage
      ->getEntityType();
    $query = $storage
      ->getQuery()
      ->currentRevision()
      ->accessCheck()
      ->condition('uid', $account
      ->id())
      ->condition('type', 'profile')
      ->condition('status', TRUE)
      ->sort('is_default', 'DESC')
      ->sort('profile_id', 'DESC')
      ->range(0, 1);
    $metadata
      ->addCacheTags($type
      ->getListCacheTags());
    $metadata
      ->addCacheContexts($type
      ->getListCacheContexts());
    $result = $query
      ->execute();
    if (empty($result)) {
      return NULL;
    }
    $resolver = $this->entityBuffer
      ->add('profile', reset($result));
    return new Deferred(function () use ($resolver, $metadata) {
      $entity = $resolver();
      $metadata
        ->addCacheableDependency($entity);
      return $entity;
    });
  }

}

Classes

Namesort descending Description
DefaultProfileLoad Loads the default profile for a user.