You are here

public function UserManager::addUserRecord in Social Post 8.2

Same name and namespace in other branches
  1. 3.x src/User/UserManager.php \Drupal\social_post\User\UserManager::addUserRecord()

Add user record in Social Post Entity.

Parameters

string $name: The user name in the provider.

int $user_id: The Drupal User ID associated to the record.

int|string $provider_user_id: Unique Social ID returned by social network.

string $url: The URL to the profile in the provider.

string $token: Token to be used for autoposting.

Return value

bool True if User record was created or False otherwise

File

src/User/UserManager.php, line 68

Class

UserManager
Manages database related tasks.

Namespace

Drupal\social_post\User

Code

public function addUserRecord($name, $user_id, $provider_user_id, $url, $token) {
  if ($this
    ->getDrupalUserId($provider_user_id)) {
    return FALSE;
  }
  $values = [
    'user_id' => $user_id,
    'plugin_id' => $this->pluginId,
    'provider_user_id' => $provider_user_id,
    'name' => $name,
    'token' => $token,
  ];

  // If URL to profile is provided.
  if ($url) {
    $values['link'] = [
      'uri' => $url,
      'title' => $name,
    ];
  }
  try {
    $user_info = SocialPost::create($values);

    // Save the entity.
    $user_info
      ->save();
  } catch (\Exception $ex) {
    $this->loggerFactory
      ->get($this
      ->getPluginId())
      ->error('Failed to add user record in Social Auth entity.
            Exception: @message', [
      '@message' => $ex
        ->getMessage(),
    ]);
    $this->messenger
      ->addError($this
      ->t('You could not be authenticated, please contact the administrator.'));
    return FALSE;
  }
  return TRUE;
}