You are here

protected function SocialAuthUserManager::downloadProfilePic in Social Auth 8

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 succesfully downloaded False otherwise

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

File

src/SocialAuthUserManager.php, line 582

Class

SocialAuthUserManager
Contains all logic that is related to Drupal user management.

Namespace

Drupal\social_auth

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
    ->filePrepareDirectory($directory, 1)) {
    $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 . '.jpg', 'en', '_', 50);
  $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;
}