Skip to content

Commit

Permalink
Not send the attributes of active record to the queue if event is not…
Browse files Browse the repository at this point in the history
… delete.
  • Loading branch information
petrabarus committed Jun 1, 2015
1 parent e5f1efc commit e79662b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 40 deletions.
68 changes: 45 additions & 23 deletions src/Behaviors/ActiveRecordDeferredEventBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,48 +55,70 @@ public static function getDefaultEvents() {
*/
public function postDeferredEvent($event) {
$class = get_class($this->owner);
$pk = $this->owner->getPrimaryKey();
$attributes = $this->owner->getAttributes();
$eventName = $event->name;
$handlers = ($this->_hasEventHandlers) ? $this->events : false;
if (isset($this->_serializer)) {
$serializer = $this->_serializer;
} else {
$serializer = null;
}
$this->queue->post(new \UrbanIndo\Yii2\Queue\Job([
'route' => function() use ($class, $pk, $attributes, $handlers, $eventName, $serializer) {
if ($eventName == ActiveRecord::EVENT_AFTER_DELETE) {

if ($eventName == ActiveRecord::EVENT_AFTER_DELETE) {
$attributes = $this->owner->getAttributes();
$this->queue->post(new \UrbanIndo\Yii2\Queue\Job([
'route' => function() use ($class, $attributes, $handlers, $eventName, $serializer) {
$object = \Yii::createObject($class);
/* @var $object ActiveRecord */
$object->setAttributes($attributes, false);
} else {
if ($handlers) {
$handler = $handlers[$eventName];
if ($serializer !== null) {
try {
$unserialized = $serializer->unserialize($handler);
$unserialized($object);
} catch (Exception $exc) {
return call_user_method($handler, $object);
}
} else {
return call_user_method($handler, $object);
}
} else if ($object instanceof DeferredEventInterface) {
/* @var $object DeferredEventInterface */
return $object->handleDeferredEvent($eventName);
} else {
throw new Exception("Model is not instance of DeferredEventInterface");
}
}
]));
} else {
$pk = $this->owner->getPrimaryKey();
$this->queue->post(new \UrbanIndo\Yii2\Queue\Job([
'route' => function() use ($class, $pk, $handlers, $eventName, $serializer) {
$object = $class::findOne($pk);
if ($object === null) {
throw new Exception("Model is not found");
}
}

if ($handlers) {
$handler = $handlers[$eventName];
if ($serializer !== null) {
try {
$unserialized = $serializer->unserialize($handler);
$unserialized($object);
} catch (Exception $exc) {
if ($handlers) {
$handler = $handlers[$eventName];
if ($serializer !== null) {
try {
$unserialized = $serializer->unserialize($handler);
$unserialized($object);
} catch (Exception $exc) {
return call_user_method($handler, $object);
}
} else {
return call_user_method($handler, $object);
}
} else if ($object instanceof DeferredEventInterface) {
/* @var $object DeferredEventInterface */
return $object->handleDeferredEvent($eventName);
} else {
return call_user_method($handler, $object);
throw new Exception("Model is not instance of DeferredEventInterface");
}
} else if ($object instanceof DeferredEventInterface) {
/* @var $object DeferredEventInterface */
return $object->handleDeferredEvent($eventName);
} else {
throw new Exception("Model is not instance of DeferredEventInterface");
}
}
]));
]));
}
}

}
22 changes: 14 additions & 8 deletions src/Behaviors/ActiveRecordDeferredEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,32 @@ public function deferEvent($event) {
$pk = $this->owner->getPrimaryKey();
$attributes = $this->owner->getAttributes();
$eventName = $event->name;

$queue = $this->queue;
$handler = clone $this;
$handler->queue = null;
$handler->owner = null;
/* @var $queue Queue */
$queue->post(new \UrbanIndo\Yii2\Queue\Job([
'route' => function() use ($class, $pk, $attributes, $handler, $eventName) {
if ($eventName == ActiveRecord::EVENT_AFTER_DELETE) {
if ($eventName == ActiveRecord::EVENT_AFTER_DELETE) {
$queue->post(new \UrbanIndo\Yii2\Queue\Job([
'route' => function() use ($class, $pk, $attributes, $handler, $eventName) {
$object = \Yii::createObject($class);
/* @var $object ActiveRecord */
$object->setAttributes($attributes, false);
} else {
$handler->handleEvent($object);
}
]));
} else {
$queue->post(new \UrbanIndo\Yii2\Queue\Job([
'route' => function() use ($class, $pk, $attributes, $handler, $eventName) {
$object = $class::findOne($pk);
if ($object === null) {
throw new Exception("Model is not found");
}
/* @var $object ActiveRecord */
$handler->handleEvent($object);
}
$handler->handleEvent($object);
}
]));
]));
}

}
}
67 changes: 58 additions & 9 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ abstract class Queue extends \yii\base\Component {
* @var \yii\base\Module
*/
public $module;

const SERIALIZER_JSON = 'json';

const SERIALIZER_PHP = 'php';

/**
* Choose the serializer.
* @var string
*/
public $serializer = 'json';

/**
* Initializes the module.
Expand Down Expand Up @@ -90,30 +100,50 @@ public abstract function delete(Job $job);
/**
* Deserialize job to be executed.
*
* @param string $json the json string
* @param string $message the json string
* @return \UrbanIndo\Yii2\Queue\Job the job
* @throws \yii\base\Exception if there is no route detected.
*/
protected function deserialize($json) {
$message = \yii\helpers\Json::decode($json);
if (!isset($message['route'])) {
protected function deserialize($message) {
$job = $this->deserializeMessage($message);
if (!isset($job['route'])) {
throw new \yii\base\Exception('No route detected');
}
$route = $message['route'];
if (isset($message['type']) && $message['type'] == Job::TYPE_CALLABLE) {
$route = $job['route'];
if (isset($job['type']) && $job['type'] == Job::TYPE_CALLABLE) {
$type = Job::TYPE_CALLABLE;
$serializer = new \SuperClosure\Serializer();
$route = $serializer->unserialize($route);
} else {
$type = Job::TYPE_REGULAR;
}
$data = \yii\helpers\ArrayHelper::getValue($message, 'data', []);
$data = \yii\helpers\ArrayHelper::getValue($job, 'data', []);
return new Job([
'route' => $route,
'data' => $data,
]);
}


/**
*
* @param type $array
* @return type
*/
protected function deserializeMessage($array) {
switch($this->serializer) {
case self::SERIALIZER_PHP:
$data = unserialize($array);
break;
case self::SERIALIZER_JSON:
$data = \yii\helpers\Json::decode($array);
break;
}
if (empty($data)) {
throw new Exception('Can not deserialize message');
}
return $data;
}

/**
* Pack job so that it can be send.
*
Expand All @@ -131,7 +161,26 @@ protected function serialize(Job $job) {
$return['route'] = $job->route;
}
$return['data'] = $job->data;
return \yii\helpers\Json::encode($return);
return $this->serializeMessage($return);
}

/**
*
* @param type $array
* @return type
*/
protected function serializeMessage($array) {
switch($this->serializer) {
case self::SERIALIZER_PHP:
$data = serialize($array);
break;
case self::SERIALIZER_JSON:
$data = \yii\helpers\Json::encode($array);
break;
}
if (empty($data)) {
throw new Exception('Can not deserialize message');
}
return $data;
}
}

0 comments on commit e79662b

Please sign in to comment.