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.
*
* It follows the format 'type:content', where type is the wrapper plugin
* machine name and content is the real blob as generated by the plugin.
* It follows the format 'type,origin_branch_id:content', where:
* - '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
*/
@ -58,16 +61,25 @@ class Envelope
* @var WrapperPluginInterface
*/
protected $wrapperPlugin;
/**
* The branch id where the related envelope comes from if any.
*
* @var int
*/
protected $originBranchId = false;
/**
* Basic constructor.
*
* @param WrapperPluginInterface $wrapper_plugin
* A ChamiloLMS\WrapperPluginInterface object.
* @param array $data
* Information to build the object. The supported array keys are:
* - 'transactions': An array of ChamiloLMS\Transaction\TransactionLog
* objects to include.
* - 'blob': A string containing the envelope in raw form.
* - 'wrapper_plugin': A ChamiloLMS\WrapperPluginInterface object.
* objects to include. Required if 'blob' is not passed.
* - 'blob': A string containing the envelope in raw form. Required if
* 'transactions' is not passed.
* - 'origin_branch_id': Associated branch id if any.
*/
public function __construct(WrapperPluginInterface $wrapper_plugin, $data)
{
@ -83,6 +95,9 @@ class Envelope
$this->blob = $data['blob'];
$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) {
return $this->transactions;
}
return NULL;
return null;
}
/**
@ -108,7 +123,30 @@ class Envelope
if ($this->state & self::STATE_CLOSED) {
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
* A raw blob.
*
* @return mixed
* The envelope wrapper plugin machine type or false if it is not
* possible to determine it.
* @throws Exception
* Cannot identify the blob correctly.
*
* @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, ':');
if ($position === FALSE) {
return FALSE;
throw new Exception('blob identify: Cannot find ":" on the blob.');
}
$blob_type = substr($blob, 0, $position);
if ($blob_type === FALSE) {
return FALSE;
$blob_metadata = substr($blob, 0, $position);
if ($blob_metadata === 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 {
$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;
}
catch (Exception $exception) {

@ -563,26 +563,22 @@ class TransactionLogController
$errors = array();
foreach ($blobs as $blob) {
if (!$wrapper_plugin_name = Envelope::identifyBlobType($blob)) {
$errors[] = 'Unable to identify the blob type for raw envelope blob.';
continue;
}
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) {
$errors[] = sprintf('Unable to create wrapper plugin with machine name "%s": %s', $wrapper_plugin_name, $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);
$errors[] = $exception->getMessage();
continue;
}
$envelopes[] = $envelope;
self::queueReceivedEnvelope($envelope);
}
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);
}

Loading…
Cancel
Save