You are here

protected function UserManager::downloadProfilePic in Social Auth 8.2

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

Downloads the profile picture to Drupal filesystem.

Parameters

string $picture_url: Absolute URL where to download the profile picture.

string $id: Social network ID of the user.

Return value

\Drupal\file\FileInterface|false FileInterface object if file was successfully downloaded False otherwise

1 call to UserManager::downloadProfilePic()
UserManager::setProfilePic in src/User/UserManager.php
Downloads and sets user profile picture.

File

src/User/UserManager.php, line 392

Class

UserManager
Manages database related tasks.

Namespace

Drupal\social_auth\User

Code

protected function downloadProfilePic($picture_url, $id) {

  // Make sure that we have everything we need.
  if (!$picture_url || !$id) {
    return FALSE;
  }

  // Determine target directory.
  $scheme = $this->configFactory
    ->get('system.file')
    ->get('default_scheme');
  $file_directory = $this
    ->getPictureDirectory();
  if (!$file_directory) {
    return FALSE;
  }
  $directory = $scheme . '://' . $file_directory;

  // Replace tokens.
  $directory = $this->token
    ->replace($directory);

  // Transliterate directory name.
  $directory = $this->transliteration
    ->transliterate($directory, 'en', '_', 50);
  if (!$this->fileSystem
    ->prepareDirectory($directory, $this->fileSystem::CREATE_DIRECTORY)) {
    $this->loggerFactory
      ->get($this
      ->getPluginId())
      ->error('Could not save @plugin_id\'s provider profile picture. Directory is not writable: @directory', [
      '@directory' => $directory,
      '@provider' => $this
        ->getPluginId(),
    ]);
    return FALSE;
  }

  // Generate filename and transliterate.
  $filename = $this->transliteration
    ->transliterate($this
    ->getPluginId() . '_' . $id, 'en', '_', 50) . '.jpg';
  $destination = $directory . DIRECTORY_SEPARATOR . $filename;

  // Download the picture to local filesystem.
  if (!($file = $this
    ->systemRetrieveFile($picture_url, $destination, TRUE, 1))) {
    $this->loggerFactory
      ->get($this
      ->getPluginId())
      ->error('Could not download @plugin_id\'s provider profile picture from url: @url', [
      '@url' => $picture_url,
      '@plugin_id' => $this
        ->getPluginId(),
    ]);
    return FALSE;
  }
  return $file;
}