You are here

abstract class UserManager in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/custom/social_auth_extra/src/UserManager.php \Drupal\social_auth_extra\UserManager
  2. 8 modules/custom/social_auth_extra/src/UserManager.php \Drupal\social_auth_extra\UserManager
  3. 8.2 modules/custom/social_auth_extra/src/UserManager.php \Drupal\social_auth_extra\UserManager
  4. 8.3 modules/custom/social_auth_extra/src/UserManager.php \Drupal\social_auth_extra\UserManager
  5. 8.4 modules/custom/social_auth_extra/src/UserManager.php \Drupal\social_auth_extra\UserManager
  6. 8.5 modules/custom/social_auth_extra/src/UserManager.php \Drupal\social_auth_extra\UserManager
  7. 8.6 modules/custom/social_auth_extra/src/UserManager.php \Drupal\social_auth_extra\UserManager
  8. 8.8 modules/custom/social_auth_extra/src/UserManager.php \Drupal\social_auth_extra\UserManager

Class UserManager.

@package Drupal\social_auth_extra

Hierarchy

Expanded class hierarchy of UserManager

4 files declare their use of UserManager
FacebookUserManager.php in modules/custom/social_auth_facebook/src/FacebookUserManager.php
GoogleUserManager.php in modules/custom/social_auth_google/src/GoogleUserManager.php
LinkedInUserManager.php in modules/custom/social_auth_linkedin/src/LinkedInUserManager.php
TwitterUserManager.php in modules/custom/social_auth_twitter/src/TwitterUserManager.php

File

modules/custom/social_auth_extra/src/UserManager.php, line 21

Namespace

Drupal\social_auth_extra
View source
abstract class UserManager implements UserManagerInterface {

  /**
   * Object of a user account.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $account;

  /**
   * Object of a user profile.
   *
   * @var \Drupal\profile\Entity\ProfileInterface
   */
  protected $profile;

  /**
   * Contains the field definition with a profile picture.
   *
   * @var \Drupal\Core\Field\FieldDefinitionInterface
   */
  protected $fieldPicture;

  /**
   * Contains the profile type.
   *
   * @var string
   */
  protected $profileType = 'profile';
  protected $configFactory;
  protected $entityTypeManager;
  protected $languageManager;
  protected $entityFieldManager;
  protected $token;
  protected $transliteration;
  protected $loggerFactory;

  /**
   * UserManager constructor.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, EntityFieldManagerInterface $entity_field_manager, Token $token, TransliterationInterface $transliteration, LoggerChannelFactoryInterface $logger_factory) {
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
    $this->languageManager = $language_manager;
    $this->entityFieldManager = $entity_field_manager;
    $this->token = $token;
    $this->transliteration = $transliteration;
    $this->loggerFactory = $logger_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function createAccount(array $values = []) {
    $langcode = $this->languageManager
      ->getCurrentLanguage()
      ->getId();
    $values = array_merge([
      'name' => '',
      'mail' => '',
      'init' => '',
      'pass' => NULL,
      'status' => 1,
      'langcode' => $langcode,
      'preferred_langcode' => $langcode,
      'preferred_admin_langcode' => $langcode,
    ], $values);
    $this->account = $this->entityTypeManager
      ->getStorage('user')
      ->create($values);
    return $this->account;
  }

  /**
   * {@inheritdoc}
   */
  public function createProfile(array $values = []) {
    $values = array_merge([
      'uid' => $this->account ? $this->account
        ->id() : NULL,
      'type' => $this->profileType,
    ], $values);
    $this->profile = $this->entityTypeManager
      ->getStorage('profile')
      ->create($values);
    return $this->profile;
  }

  /**
   * {@inheritdoc}
   */
  public function setProfilePicture($url, $account_id) {
    if ($this->profile && ($file = $this
      ->downloadProfilePicture($url, $account_id))) {
      !$this->account ?: $file
        ->setOwner($this->account);
      $file
        ->save();
      $field_name = $this->fieldPicture
        ->getName();
      $this->profile
        ->get($field_name)
        ->setValue($file
        ->id());
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function downloadProfilePicture($url, $account_id) {
    $key = $this
      ->getSocialNetworkKey();
    if (!$url || !$account_id) {
      return FALSE;
    }
    if (!($directory = $this
      ->getPictureDirectory())) {
      return FALSE;
    }
    if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
      $this->loggerFactory
        ->get('social_auth_' . $key)
        ->error('The image could not be saved, the directory @directory is not valid.', [
        '@directory' => $directory,
      ]);
      return FALSE;
    }
    $filename = $this->transliteration
      ->transliterate($key . '_' . $account_id . '.jpg', 'en', '_', 50);
    $destination = "{$directory}/{$filename}";
    if (!($file = system_retrieve_file($url, $destination, TRUE, FILE_EXISTS_REPLACE))) {
      $this->loggerFactory
        ->get('social_auth_' . $key)
        ->error('The file @filename could not downloaded.', [
        '@filename' => $filename,
      ]);
      return FALSE;
    }
    return $file;
  }

  /**
   * {@inheritdoc}
   */
  public function getPictureDirectory() {
    if ($this->fieldPicture instanceof FieldDefinitionInterface) {

      // Prepare directory where downloaded image will be saved.
      $scheme = $this->configFactory
        ->get('system.file')
        ->get('default_scheme');
      $directory = $this->fieldPicture
        ->getSetting('file_directory');
      $directory = "{$scheme}://{$directory}";
      $directory = $this->token
        ->replace($directory);
      $directory = $this->transliteration
        ->transliterate($directory, 'en', '_', 50);
      return $directory;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setProfile(ProfileInterface $profile) {
    $this->profile = $profile;
  }

  /**
   * {@inheritdoc}
   */
  public function setAccount(UserInterface $account) {
    $this->account = $account;
  }

  /**
   * {@inheritdoc}
   */
  public function setFieldPicture(FieldDefinitionInterface $field) {
    $this->fieldPicture = $field;
  }

  /**
   * {@inheritdoc}
   */
  public function setProfileType($profile_type) {
    $this->profileType = $profile_type;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UserManager::$account protected property Object of a user account.
UserManager::$configFactory protected property
UserManager::$entityFieldManager protected property
UserManager::$entityTypeManager protected property
UserManager::$fieldPicture protected property Contains the field definition with a profile picture.
UserManager::$languageManager protected property
UserManager::$loggerFactory protected property
UserManager::$profile protected property Object of a user profile.
UserManager::$profileType protected property Contains the profile type.
UserManager::$token protected property
UserManager::$transliteration protected property
UserManager::createAccount public function Creates object of a new account. Overrides UserManagerInterface::createAccount
UserManager::createProfile public function Creates object of a new profile. Overrides UserManagerInterface::createProfile
UserManager::downloadProfilePicture public function Saves the picture from URL. Overrides UserManagerInterface::downloadProfilePicture
UserManager::getPictureDirectory public function Returns directory path to save picture. Overrides UserManagerInterface::getPictureDirectory
UserManager::setAccount public function Set an instance of user account to user manager to use it later. Overrides UserManagerInterface::setAccount
UserManager::setFieldPicture public function Set an instance of a field definition that contains picture. Overrides UserManagerInterface::setFieldPicture
UserManager::setProfile public function Set an instance of profile to user manager to use it later. Overrides UserManagerInterface::setProfile
UserManager::setProfilePicture public function Download and set the picture to profile. Overrides UserManagerInterface::setProfilePicture
UserManager::setProfileType public function Set the profile type. Overrides UserManagerInterface::setProfileType
UserManager::__construct public function UserManager constructor.
UserManagerInterface::getAccountId public function Get the account ID to the account on this site. 4
UserManagerInterface::getSocialNetworkKey public function Returns key-name of a social network. 4
UserManagerInterface::setAccountId public function Set the account ID to the account on this site. 4