ExistingUser.php in Acquia Content Hub 8.2
File
src/EventSubscriber/Cdf/ExistingUser.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\Cdf;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\ParseCdfEntityEvent;
use Drupal\user\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ExistingUser implements EventSubscriberInterface {
public const GENERATED_USER_PATTERN = '%s (%s)';
public const PATTERN_SPECIFIERS = [
'%s',
'%d',
'%u',
'%c',
'%o',
'%x',
'%X',
'%b',
'%g',
'%G',
'%e',
'%E',
'%f',
'%F',
];
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::PARSE_CDF][] = [
'onParseCdf',
90,
];
return $events;
}
public function onParseCdf(ParseCdfEntityEvent $event) {
$cdf = $event
->getCDF();
if ($cdf
->getAttribute('entity_type')
->getValue()['und'] !== 'user') {
return;
}
$username = $cdf
->getAttribute('username')
->getValue()['und'];
$account = user_load_by_name($username);
if (!$account) {
return;
}
if ($account
->uuid() === $event
->getEntity()
->uuid()) {
return;
}
if ($account
->getEmail() !== $event
->getEntity()
->getEmail()) {
$entity = $event
->getEntity();
$username = $this
->generateUsername(self::GENERATED_USER_PATTERN, $cdf
->getUuid(), $username);
$entity
->setUsername($username);
$event
->setEntity($entity);
}
}
public function generateUsername(string $pattern, string ...$pattern_arguments) {
if (empty($pattern)) {
throw new \Exception("No pattern could be found for the generated username.");
}
$count = 0;
foreach (self::PATTERN_SPECIFIERS as $specifier) {
$count += substr_count($pattern, $specifier);
}
$arguments_count = count($pattern_arguments);
if ($count !== $arguments_count) {
throw new \Exception(sprintf("Mismatched number of pattern arguments to pattern expectations while attempting to generate username. Expected %d; received %d", $count, $arguments_count));
}
$username = sprintf($pattern, ...$pattern_arguments);
if (empty($username)) {
throw new \Exception("Could not generate a username.");
}
$max_length = User::USERNAME_MAX_LENGTH;
if (strlen($username) > $max_length) {
return substr($username, 0, $max_length);
}
return $username;
}
}