View source
<?php
namespace Drupal\social_auth_extra;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Utility\Token;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\profile\Entity\ProfileInterface;
use Drupal\user\UserInterface;
abstract class UserManager implements UserManagerInterface {
protected $account;
protected $profile;
protected $fieldPicture;
protected $profileType = 'profile';
protected $configFactory;
protected $entityTypeManager;
protected $languageManager;
protected $entityFieldManager;
protected $token;
protected $transliteration;
protected $loggerFactory;
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;
}
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;
}
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;
}
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;
}
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;
}
public function getPictureDirectory() {
if ($this->fieldPicture instanceof FieldDefinitionInterface) {
$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;
}
public function setProfile(ProfileInterface $profile) {
$this->profile = $profile;
}
public function setAccount(UserInterface $account) {
$this->account = $account;
}
public function setFieldPicture(FieldDefinitionInterface $field) {
$this->fieldPicture = $field;
}
public function setProfileType($profile_type) {
$this->profileType = $profile_type;
}
}