public function UserDeveloperConverter::convertDeveloper in Apigee Edge 8
Converts a developer entity to a Drupal user entity.
Creates a new user entity if it did not exist for a developer or update properties of the existing developer entity.
It modifies only those properties that changed.
Parameters
\Drupal\apigee_edge\Entity\DeveloperInterface $developer: The developer entity.
Return value
\Drupal\apigee_edge\Structure\DeveloperToUserConversionResult The result of the conversion.
Overrides UserDeveloperConverterInterface::convertDeveloper
File
- src/
UserDeveloperConverter.php, line 167
Class
- UserDeveloperConverter
- Default user-developer converter service implementation.
Namespace
Drupal\apigee_edgeCode
public function convertDeveloper(DeveloperInterface $developer) : DeveloperToUserConversionResult {
$successful_changes = 0;
$problems = [];
$user_storage = $this->entityTypeManager
->getStorage('user');
// id() always contains the original, unchanged email address of a
// developer.
$users = $user_storage
->loadByProperties([
'mail' => $developer
->id(),
]);
$user = $users ? reset($users) : FALSE;
/** @var \Drupal\user\UserInterface $user */
if (!$user) {
// Initialize new user object with minimum data.
$user = $user_storage
->create([
'pass' => user_password(),
]);
// Suppress invalid email validation errors.
DeveloperEmailUniqueValidator::whitelist($developer
->id());
}
// Validate base field values that we care about.
foreach (static::DEVELOPER_PROP_USER_BASE_FIELD_MAP as $developer_prop => $base_field) {
$getter = 'get' . ucfirst($developer_prop);
// Do not change the value of the base field unless it has not changed.
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++;
}
}
}
// Synchronise statuses.
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 the field does not exist, then skip it.
if (!isset($field_definition)) {
$problems[] = new UserDeveloperConversionUserFieldDoesNotExistException($field_name);
continue;
}
$field_type = $field_definition
->getType();
$formatter = $this->fieldStorageFormatManager
->lookupPluginForFieldType($field_type);
// If there is no available storage formatter for the field then skip
// it.
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) {
// Clear invalid field value.
$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);
}