tx-io: Change envelope blob format to include branch id.

So, it is possible to retrieve the right wrapper plugin settings during
its instanciation.
skala
Marco Villegas 12 years ago
parent 3288569dcf
commit 5e8f843a82
  1. 78
      src/ChamiloLMS/Transaction/Envelope.php
  2. 18
      src/ChamiloLMS/Transaction/TransactionLogController.php

@ -40,8 +40,11 @@ class Envelope
/** /**
* A place to store the raw envelope when it is closed. * A place to store the raw envelope when it is closed.
* *
* It follows the format 'type:content', where type is the wrapper plugin * It follows the format 'type,origin_branch_id:content', where:
* machine name and content is the real blob as generated by the plugin. * - 'type': the wrapper plugin machine name.
* - 'origin_branch_id': The branch id of the branch where the blob was
* generated.
* - 'content': is the real blob as generated by the plugin.
* *
* @var string * @var string
*/ */
@ -58,16 +61,25 @@ class Envelope
* @var WrapperPluginInterface * @var WrapperPluginInterface
*/ */
protected $wrapperPlugin; protected $wrapperPlugin;
/**
* The branch id where the related envelope comes from if any.
*
* @var int
*/
protected $originBranchId = false;
/** /**
* Basic constructor. * Basic constructor.
* *
* @param WrapperPluginInterface $wrapper_plugin
* A ChamiloLMS\WrapperPluginInterface object.
* @param array $data * @param array $data
* Information to build the object. The supported array keys are: * Information to build the object. The supported array keys are:
* - 'transactions': An array of ChamiloLMS\Transaction\TransactionLog * - 'transactions': An array of ChamiloLMS\Transaction\TransactionLog
* objects to include. * objects to include. Required if 'blob' is not passed.
* - 'blob': A string containing the envelope in raw form. * - 'blob': A string containing the envelope in raw form. Required if
* - 'wrapper_plugin': A ChamiloLMS\WrapperPluginInterface object. * 'transactions' is not passed.
* - 'origin_branch_id': Associated branch id if any.
*/ */
public function __construct(WrapperPluginInterface $wrapper_plugin, $data) public function __construct(WrapperPluginInterface $wrapper_plugin, $data)
{ {
@ -83,6 +95,9 @@ class Envelope
$this->blob = $data['blob']; $this->blob = $data['blob'];
$this->state |= self::STATE_CLOSED; $this->state |= self::STATE_CLOSED;
} }
if (!empty($data['origin_branch_id'])) {
$this->originBranchId = $data['origin_branch_id'];
}
} }
/** /**
@ -95,7 +110,7 @@ class Envelope
if ($this->state & self::STATE_OPEN) { if ($this->state & self::STATE_OPEN) {
return $this->transactions; return $this->transactions;
} }
return NULL; return null;
} }
/** /**
@ -108,7 +123,30 @@ class Envelope
if ($this->state & self::STATE_CLOSED) { if ($this->state & self::STATE_CLOSED) {
return $this->blob; return $this->blob;
} }
return NULL; return null;
}
/**
* Get related branch id.
*
* @return mixed
* Branch id if defined or null if not available.
*/
public function getOriginBranchId() {
if (!empty($this->originBranchId)) {
return $this->originBranchId;
}
return null;
}
/**
* Set related branch id.
*
* @param integer $branch_id
* Related branch id.
*/
public function setOriginBranchId($branch_id) {
$this->originBranchId = $branch_id;
} }
/** /**
@ -117,20 +155,26 @@ class Envelope
* @param string $blob * @param string $blob
* A raw blob. * A raw blob.
* *
* @return mixed * @throws Exception
* The envelope wrapper plugin machine type or false if it is not * Cannot identify the blob correctly.
* possible to determine it. *
* @return array
* An array with the metadata. Contains the following keys:
* - 'type': The wrapper plugin machine name.
* - 'origin_branch_id': The branch where the blob was generated.
*/ */
public static function identifyBlobType($blob) { public static function identifyBlobMetadata($blob) {
$position = strpos($blob, ':'); $position = strpos($blob, ':');
if ($position === FALSE) { if ($position === FALSE) {
return FALSE; throw new Exception('blob identify: Cannot find ":" on the blob.');
} }
$blob_type = substr($blob, 0, $position); $blob_metadata = substr($blob, 0, $position);
if ($blob_type === FALSE) { if ($blob_metadata === FALSE) {
return FALSE; throw new Exception('blob identify: Cannot extract correctly the blob metadata.');
} }
return $blob_type; list($blob_type, $origin_branch_id) = explode(',', $blob_metadata);
return array('blob_type' => $blob_type, 'origin_branch_id' => $origin_branch_id);
} }
/** /**
@ -145,7 +189,7 @@ class Envelope
} }
try { try {
$this->prepare(); $this->prepare();
$this->blob = sprintf('%s:%s', $this->wrapperPlugin->getMachineName(), $this->wrapperPlugin->wrap($this->transactions)); $this->blob = sprintf('%s,%d:%s', $this->wrapperPlugin->getMachineName(), $this->getOriginBranchId(), $this->wrapperPlugin->wrap($this->transactions));
$this->state |= self::STATE_CLOSED; $this->state |= self::STATE_CLOSED;
} }
catch (Exception $exception) { catch (Exception $exception) {

@ -563,26 +563,22 @@ class TransactionLogController
$errors = array(); $errors = array();
foreach ($blobs as $blob) { foreach ($blobs as $blob) {
if (!$wrapper_plugin_name = Envelope::identifyBlobType($blob)) {
$errors[] = 'Unable to identify the blob type for raw envelope blob.';
continue;
}
try { try {
$wrapper_plugin = self::createPlugin('wrapper', $wrapper_plugin_name, $local_branch->getPluginData('wrapper')); $blob_metadata = Envelope::identifyBlobMetadata($blob);
$origin_branch = $this->branchRepository->find($blob_metadata['origin_branch_id']);
$wrapper_plugin = self::createPlugin('wrapper', $blob_metadata['type'], $origin_branch->getPluginData('wrapper'));
$envelope_data = array('blob' => $blob, 'origin_branch_id' => $blob_metadata['origin_branch_id']);
$envelope = new Envelope($wrapper_plugin, $envelope_data);
} }
catch (Exception $exception) { catch (Exception $exception) {
$errors[] = sprintf('Unable to create wrapper plugin with machine name "%s": %s', $wrapper_plugin_name, $exception->getMessage()); $errors[] = $exception->getMessage();
continue;
}
if (!$envelope = self::makeEnvelopeFromBlob($blob, $wrapper_plugin)) {
$errors[] = sprintf('Unable to create an envelope from blob with wrapper plugin "%s".', $wrapper_plugin_name);
continue; continue;
} }
$envelopes[] = $envelope; $envelopes[] = $envelope;
self::queueReceivedEnvelope($envelope); self::queueReceivedEnvelope($envelope);
} }
if (!empty($errors)) { if (!empty($errors)) {
$log_entry['message'] = sprintf('Problems processing received blobs: %s', implode(', ', $errors)); $log_entry['message'] = sprintf('Problems processing received blobs: %s', implode(' || ', $errors));
self::addImportLog($log_entry); self::addImportLog($log_entry);
} }

Loading…
Cancel
Save