View source
<?php
namespace Drupal\apigee_edge\Job;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Exception\DeveloperDoesNotExistException;
use Drupal\apigee_edge\Exception\DeveloperToUserConversationInvalidValueException;
use Drupal\apigee_edge\Exception\DeveloperToUserConversionAttributeDoesNotExistException;
use Drupal\apigee_edge\Exception\UserDeveloperConversionException;
use Drupal\apigee_edge\Exception\UserDeveloperConversionNoStorageFormatterFoundException;
use Drupal\apigee_edge\Exception\UserDeveloperConversionUserFieldDoesNotExistException;
use Drupal\apigee_edge\Structure\DeveloperToUserConversionResult;
use Drupal\Core\TypedData\Plugin\DataType\ItemList;
use Drupal\Core\Utility\Error;
abstract class UserCreateUpdate extends EdgeJob {
use UserDeveloperSyncJobTrait;
protected $email;
public function __construct(string $email) {
parent::__construct();
$this->email = $email;
}
protected function executeRequest() {
try {
$developer = Developer::load($this->email);
if ($developer === NULL) {
throw new DeveloperDoesNotExistException($this->email);
}
$result = $this
->userDeveloperConverter()
->convertDeveloper($developer);
$this
->beforeUserSave($result);
if ($result
->getSuccessfullyAppliedChanges() > 0) {
_apigee_edge_set_sync_in_progress(TRUE);
$result
->getUser()
->save();
}
} catch (\Exception $exception) {
$message = '@operation: Skipping %mail user. @message %function (line %line of %file). <pre>@backtrace_string</pre>';
$context = [
'%mail' => $this->email,
'@operation' => get_class($this),
];
$context += Error::decodeException($exception);
$this
->logger()
->error($message, $context);
$this
->recordMessage(t('Skipping %mail user: @message', $context)
->render());
} finally {
_apigee_edge_set_sync_in_progress(FALSE);
if (isset($result)) {
$this
->afterUserSave($result);
}
}
}
protected function beforeUserSave(DeveloperToUserConversionResult $result) : void {
foreach ($result
->getProblems() as $problem) {
if ($problem instanceof DeveloperToUserConversationInvalidValueException && $problem
->getTarget() === 'name') {
throw $problem;
}
}
$result
->getUser()
->setChangedTime(\Drupal::time()
->getCurrentTime());
}
protected function afterUserSave(DeveloperToUserConversionResult $result) : void {
}
protected function logConversionProblem(UserDeveloperConversionException $problem, array $context = []) : void {
$ro = new \ReflectionObject($this);
$context += [
'%mail' => $this->email,
'@operation' => $ro
->getShortName(),
];
if ($problem instanceof DeveloperToUserConversionAttributeDoesNotExistException) {
$message = '@operation: %field_name field value on %mail user has not changed because the relevant developer does not have %attribute_name attribute on Apigee Edge.';
$context += [
'%field_name' => $this
->fieldAttributeConverter()
->getFieldName($problem
->getAttributeName()),
'%attribute_name' => $problem
->getAttributeName(),
];
$this
->logger()
->warning($message, $context);
$this
->recordMessage(t("%field_name field value on %mail user has not changed because the relevant developer does not have %attribute_name attribute on Apigee Edge.", $context)
->render());
}
elseif ($problem instanceof UserDeveloperConversionUserFieldDoesNotExistException) {
$message = '@operation: %attribute_name attribute has been skipped because %field_name field does not exist on user.';
$context += [
'%field_name' => $problem
->getFieldName(),
'%attribute_name' => $this
->fieldAttributeConverter()
->getAttributeName($problem
->getFieldName()),
];
$this
->logger()
->warning($message, $context);
$this
->recordMessage(t("%attribute_name attribute has been skipped because %field_name field does not exist on user.", $context)
->render());
}
elseif ($problem instanceof UserDeveloperConversionNoStorageFormatterFoundException) {
$message = '@operation: %field_name field has been skipped because there is no available storage formatter for %field_type field type.';
$context += [
'%field_name' => $problem
->getFieldDefinition()
->getName(),
'%field_type' => $problem
->getFieldDefinition()
->getType(),
];
$this
->logger()
->warning($message, $context);
$this
->recordMessage(t('%field_name field has been skipped because there is no available storage formatter for %field_type field type.', $context)
->render());
}
elseif ($problem instanceof DeveloperToUserConversationInvalidValueException) {
$message = "@operation: %field_name field value on %mail user has not changed because %attribute_name attribute's value is invalid as a field value: %message";
$context += [
'%field_name' => $problem
->getTarget(),
'%attribute_name' => $problem
->getSource(),
'%field_value' => is_object($problem
->getViolation()
->getInvalidValue()) ? $problem
->getViolation()
->getInvalidValue() instanceof ItemList ? var_export($problem
->getViolation()
->getInvalidValue()
->getValue(), TRUE) : $problem
->getViolation()
->getInvalidValue()->value : $problem
->getViolation()
->getInvalidValue(),
'%message' => $problem
->getViolation()
->getMessage(),
];
$this
->logger()
->warning($message, $context);
$this
->recordMessage(t("%field_name field value on %mail user has not changed because %attribute_name attribute's value is invalid as a field value: %message", $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 user: @message", $context)
->render());
}
}
}