You are here

abstract class UserManager in Social API 3.x

Same name and namespace in other branches
  1. 8.2 src/User/UserManager.php \Drupal\social_api\User\UserManager

Manages database related tasks.

Hierarchy

Expanded class hierarchy of UserManager

File

src/User/UserManager.php, line 12

Namespace

Drupal\social_api\User
View source
abstract class UserManager implements UserManagerInterface {

  /**
   * The Drupal Entity Manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * The Drupal logger factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $loggerFactory;

  /**
   * The entity type.
   *
   * @var string
   */
  protected $entityType;

  /**
   * The implementer plugin id.
   *
   * @var string
   */
  protected $pluginId;

  /**
   * Constructor.
   *
   * @param string $entity_type
   *   The entity table.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Used for loading and creating Social API-related entities.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   Used to display messages to user.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   Used for logging errors.
   */
  public function __construct($entity_type, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger_factory) {
    $this->entityType = $entity_type;
    $this->entityTypeManager = $entity_type_manager;
    $this->messenger = $messenger;
    $this->loggerFactory = $logger_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginId() {
    return $this->pluginId;
  }

  /**
   * {@inheritdoc}
   */
  public function setPluginId($plugin_id) {
    $this->pluginId = $plugin_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getDrupalUserId($provider_user_id) {
    try {

      /** @var \Drupal\social_api\Entity\SocialApi[] $user */
      $user = $this->entityTypeManager
        ->getStorage($this->entityType)
        ->loadByProperties([
        'plugin_id' => $this->pluginId,
        'provider_user_id' => $provider_user_id,
      ]);
      if (!empty($user)) {
        return current($user)
          ->getUserId();
      }
    } catch (\Exception $ex) {
      $this->loggerFactory
        ->get($this
        ->getPluginId())
        ->error('Failed to to query entity. Exception: @message', [
        '@message' => $ex
          ->getMessage(),
      ]);
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UserManager::$entityType protected property The entity type.
UserManager::$entityTypeManager protected property The Drupal Entity Manager.
UserManager::$loggerFactory protected property The Drupal logger factory.
UserManager::$messenger protected property The Messenger service.
UserManager::$pluginId protected property The implementer plugin id.
UserManager::getDrupalUserId public function Gets the Drupal user id based on the provider user id. Overrides UserManagerInterface::getDrupalUserId
UserManager::getPluginId public function Gets the implementer plugin id. Overrides UserManagerInterface::getPluginId
UserManager::setPluginId public function Sets the implementer plugin id. Overrides UserManagerInterface::setPluginId
UserManager::__construct public function Constructor.