UserDeveloperConverter.php in Apigee Edge 8
File
src/UserDeveloperConverter.php
View source
<?php
namespace Drupal\apigee_edge;
use Drupal\apigee_edge\Entity\DeveloperInterface;
use Drupal\apigee_edge\Exception\DeveloperToUserConversationInvalidValueException;
use Drupal\apigee_edge\Exception\DeveloperToUserConversionAttributeDoesNotExistException;
use Drupal\apigee_edge\Exception\UserDeveloperConversionNoStorageFormatterFoundException;
use Drupal\apigee_edge\Exception\UserDeveloperConversionUserFieldDoesNotExistException;
use Drupal\apigee_edge\Plugin\FieldStorageFormatManagerInterface;
use Drupal\apigee_edge\Plugin\Validation\Constraint\DeveloperEmailUniqueValidator;
use Drupal\apigee_edge\Structure\DeveloperToUserConversionResult;
use Drupal\apigee_edge\Structure\UserToDeveloperConversionResult;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\user\UserInterface;
class UserDeveloperConverter implements UserDeveloperConverterInterface {
protected $configFactory;
protected $fieldStorageFormatManager;
protected $entityTypeManager;
protected $fieldAttributeConverter;
public function __construct(ConfigFactory $config_factory, EntityTypeManagerInterface $entity_type_manager, FieldStorageFormatManagerInterface $field_storage_manager, FieldAttributeConverterInterface $field_attribute_converter) {
$this->configFactory = $config_factory;
$this->fieldStorageFormatManager = $field_storage_manager;
$this->entityTypeManager = $entity_type_manager;
$this->fieldAttributeConverter = $field_attribute_converter;
}
public function convertUser(UserInterface $user) : UserToDeveloperConversionResult {
$problems = [];
$successful_changes = 0;
$email = isset($user->original) ? $user->original
->getEmail() : $user
->getEmail();
$developer = $this->entityTypeManager
->getStorage('developer')
->load($email);
if (!$developer) {
$developer = $this->entityTypeManager
->getStorage('developer')
->create([]);
$developer
->setOwnerId($user
->id());
}
foreach (static::DEVELOPER_PROP_USER_BASE_FIELD_MAP as $developer_prop => $base_field) {
$setter = 'set' . ucfirst($developer_prop);
$getter = 'get' . ucfirst($developer_prop);
if ($user
->get($base_field)->value === NULL && ($base_field === "first_name" || $base_field === "last_name")) {
$base_field_value = $developer
->{$getter}() !== NULL ? $developer
->{$getter}() : ucfirst($developer_prop);
$user
->set($base_field, $base_field_value);
}
if ($user
->get($base_field)->value !== $developer
->{$getter}()) {
$developer
->{$setter}($user
->get($base_field)->value);
$successful_changes++;
}
}
if ($user
->isActive() && $developer
->getStatus() === DeveloperInterface::STATUS_INACTIVE) {
$developer
->setStatus(DeveloperInterface::STATUS_ACTIVE);
$successful_changes++;
}
elseif (!$user
->isActive() && $developer
->getStatus() === DeveloperInterface::STATUS_ACTIVE) {
$developer
->setStatus(DeveloperInterface::STATUS_INACTIVE);
$successful_changes++;
}
foreach ($this->configFactory
->get('apigee_edge.sync')
->get('user_fields_to_sync') as $field_name) {
$field_definition = $user
->getFieldDefinition($field_name);
$attribute_name = $this->fieldAttributeConverter
->getAttributeName($field_name);
if (!isset($field_definition)) {
$problems[] = new UserDeveloperConversionUserFieldDoesNotExistException($field_name);
continue;
}
$field_type = $field_definition
->getType();
$formatter = $this->fieldStorageFormatManager
->lookupPluginForFieldType($field_type);
if (!isset($formatter)) {
$problems[] = new UserDeveloperConversionNoStorageFormatterFoundException($field_definition);
continue;
}
$formatted_field_value = $formatter
->encode($user
->get($field_name)
->getValue());
if ($developer
->isNew() || $developer
->getAttributeValue($attribute_name) !== $formatted_field_value) {
if ($formatted_field_value === '') {
$developer
->deleteAttribute($attribute_name);
}
else {
$developer
->setAttribute($attribute_name, $formatted_field_value);
}
$successful_changes++;
}
}
return new UserToDeveloperConversionResult($developer, $successful_changes, $problems);
}
public function convertDeveloper(DeveloperInterface $developer) : DeveloperToUserConversionResult {
$successful_changes = 0;
$problems = [];
$user_storage = $this->entityTypeManager
->getStorage('user');
$users = $user_storage
->loadByProperties([
'mail' => $developer
->id(),
]);
$user = $users ? reset($users) : FALSE;
if (!$user) {
$user = $user_storage
->create([
'pass' => user_password(),
]);
DeveloperEmailUniqueValidator::whitelist($developer
->id());
}
foreach (static::DEVELOPER_PROP_USER_BASE_FIELD_MAP as $developer_prop => $base_field) {
$getter = 'get' . ucfirst($developer_prop);
if ($developer
->{$getter}() !== $user
->get($base_field)->value) {
$user
->set($base_field, $developer
->{$getter}());
$violations = $user
->get($base_field)
->validate();
if ($violations
->count() > 0) {
foreach ($violations as $violation) {
$problems[] = new DeveloperToUserConversationInvalidValueException($developer_prop, $base_field, $violation, $developer);
}
}
else {
$successful_changes++;
}
}
}
if ($developer
->getStatus() === DeveloperInterface::STATUS_INACTIVE && $user
->isActive()) {
$user
->block();
$successful_changes++;
}
elseif ($developer
->getStatus() === DeveloperInterface::STATUS_ACTIVE && $user
->isBlocked()) {
$user
->activate();
$successful_changes++;
}
foreach ($this->configFactory
->get('apigee_edge.sync')
->get('user_fields_to_sync') as $field_name) {
$attribute_name = $this->fieldAttributeConverter
->getAttributeName($field_name);
if (!$developer
->getAttributes()
->has($attribute_name)) {
$problems[] = new DeveloperToUserConversionAttributeDoesNotExistException($attribute_name, $developer);
continue;
}
$field_definition = $user
->getFieldDefinition($field_name);
if (!isset($field_definition)) {
$problems[] = new UserDeveloperConversionUserFieldDoesNotExistException($field_name);
continue;
}
$field_type = $field_definition
->getType();
$formatter = $this->fieldStorageFormatManager
->lookupPluginForFieldType($field_type);
if (!isset($formatter)) {
$problems[] = new UserDeveloperConversionNoStorageFormatterFoundException($field_definition);
continue;
}
$developer_attribute_value = $developer
->getAttributeValue($attribute_name);
$user
->set($field_name, $formatter
->decode($developer_attribute_value));
$violations = $user
->get($field_name)
->validate();
if ($violations
->count() > 0) {
$user
->set($field_name, NULL);
foreach ($violations as $violation) {
$problems[] = new DeveloperToUserConversationInvalidValueException($attribute_name, $field_name, $violation, $developer);
}
}
else {
$successful_changes++;
}
}
return new DeveloperToUserConversionResult($user, $successful_changes, $problems);
}
}