DeveloperCreateUpdate.php in Apigee Edge 8
File
src/Job/DeveloperCreateUpdate.php
View source
<?php
namespace Drupal\apigee_edge\Job;
use Drupal\apigee_edge\Exception\UserDeveloperConversionException;
use Drupal\apigee_edge\Exception\UserDeveloperConversionNoStorageFormatterFoundException;
use Drupal\apigee_edge\Exception\UserDeveloperConversionUserFieldDoesNotExistException;
use Drupal\apigee_edge\Exception\UserDoesNotExistWithEmail;
use Drupal\apigee_edge\Structure\UserToDeveloperConversionResult;
use Drupal\Core\Utility\Error;
use Drupal\user\UserInterface;
abstract class DeveloperCreateUpdate extends EdgeJob {
use UserDeveloperSyncJobTrait;
protected $email;
public function __construct(string $email) {
parent::__construct();
$this->email = $email;
}
protected function executeRequest() {
try {
$account = user_load_by_mail($this->email);
if (!$account) {
throw new UserDoesNotExistWithEmail($this->email);
}
$result = $this
->userDeveloperConverter()
->convertUser($account);
$this
->beforeDeveloperSave($result, $account);
if ($result
->getSuccessfullyAppliedChanges() > 0) {
$result
->getDeveloper()
->save();
}
} catch (\Exception $exception) {
$message = '@operation: Skipping %mail developer. @message %function (line %line of %file). <pre>@backtrace_string</pre>';
$context = [
'%mail' => $this->email,
'link' => $account
->toLink(t('View user'))
->toString(),
'@operation' => get_class($this),
];
$context += Error::decodeException($exception);
$this
->logger()
->error($message, $context);
$this
->recordMessage(t('Skipping %mail developer: @message', $context)
->render());
} finally {
if (isset($result)) {
$this
->afterDeveloperSave($result, $account);
}
}
}
protected function beforeDeveloperSave(UserToDeveloperConversionResult $result, UserInterface $user) : void {
$context = [
'link' => $user
->toLink(t('View user'))
->toString(),
];
$this
->logConversionProblems($result
->getProblems(), $context);
}
protected function afterDeveloperSave(UserToDeveloperConversionResult $result, UserInterface $user) : void {
}
protected function logConversionProblem(UserDeveloperConversionException $problem, array $context = []) : void {
$ro = new \ReflectionObject($this);
$context = [
'%mail' => $this->email,
'@operation' => $ro
->getShortName(),
];
if ($problem instanceof UserDeveloperConversionUserFieldDoesNotExistException) {
$message = "@operation: %mail developer's %attribute_name attribute has been skipped because %field_name field does not exist.";
$context['%field_name'] = $problem
->getFieldName();
$context['%attribute_name'] = $this
->fieldAttributeConverter()
->getAttributeName($problem
->getFieldName());
$this
->logger()
->warning($message, $context);
$this
->recordMessage(t("%mail developer's %attribute_name attribute has been skipped because %field_name field does not exist.", $context)
->render());
}
elseif ($problem instanceof UserDeveloperConversionNoStorageFormatterFoundException) {
$message = "@operation: %mail developer's %attribute_name attribute has been skipped because there is no available storage formatter for %field_type field type.";
$context['%field_type'] = $problem
->getFieldDefinition()
->getType();
$context['%attribute_name'] = $this
->fieldAttributeConverter()
->getAttributeName($problem
->getFieldDefinition()
->getName());
$this
->logger()
->warning($message, $context);
$this
->recordMessage(t("%mail developer's %attribute_name attribute has been skipped because there is no available storage formatter for %field_type field type.", $context)
->render());
}
else {
$context += Error::decodeException($problem);
$this
->logger()
->warning('@operation: Unexpected problem occurred while creating %mail user: @message %function (line %line of %file). <pre>@backtrace_string</pre>');
$this
->recordMessage(t("Unexpected problem occurred while processing %mail developer: @message", $context)
->render());
}
}
}