You are here

protected function SimpleFbConnectUserManager::downloadProfilePic in Simple FB Connect 8.2

Same name and namespace in other branches
  1. 8.3 src/SimpleFbConnectUserManager.php \Drupal\simple_fb_connect\SimpleFbConnectUserManager::downloadProfilePic()

Downloads the profile picture to Drupal filesystem.

Parameters

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

string $fbid: Facebook ID of the user.

Return value

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

2 calls to SimpleFbConnectUserManager::downloadProfilePic()
SimpleFbConnectUserManager::setProfilePic in src/SimpleFbConnectUserManager.php
Downloads and sets user profile picture.
TestSimpleFbConnectUserManager::subDownloadProfilePic in tests/src/Unit/TestSimpleFbConnectUserManager.php
Public method to help unit testing downloadProfilePic().

File

src/SimpleFbConnectUserManager.php, line 365
Contains \Drupal\simple_fb_connect\SimpleFbConnectUserManager.

Class

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

Namespace

Drupal\simple_fb_connect

Code

protected function downloadProfilePic($picture_url, $fbid) {

  // Make sure that we have everything we need.
  if (!$picture_url || !$fbid) {
    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('simple_fb_connect')
      ->error('Could not save FB profile picture. Directory is not writeable: @directory', array(
      '@directory' => $directory,
    ));
    return FALSE;
  }

  // Generate filename and transliterate. FB API always serves JPG.
  $filename = $this->transliteration
    ->transliterate($fbid . '.jpg', 'en', '_', 50);
  $destination = $directory . '/' . $filename;

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