Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xcommerce-gists/5629486 to your computer and use it in GitHub Desktop.
Save xcommerce-gists/5629486 to your computer and use it in GitHub Desktop.
Magento JSON formatter, Mage_Webhook_Model_Formatter_Json: Magento extensibility alpha-1
class Mage_Webhook_Model_Formatter_Json implements Mage_Webhook_Model_Formatter_Interface
{
const CONTENT_TYPE = 'application/json';
const FORMAT = "json";
/**
* @param Mage_Webhook_Model_Event_Interface $event
* @return Mage_Webhook_Model_Message
*/
public function format(Mage_Webhook_Model_Event_Interface $event)
{
$message = $this->newMessage();
$message->setMapping($event->getMapping());
$headers = $event->getHeaders();
$headers[Mage_Webhook_Model_Formatter_Interface::CONTENT_TYPE_HEADER] = self::CONTENT_TYPE;
$bodyData = $event->getBodyData();
$encodedData = json_encode($bodyData);
if (false === $encodedData) {
throw new LogicException("The data provided cannot be encoded as json.");
}
$message->setHeaders($headers);
$message->setBody($encodedData);
return $message;
}
/**
* @param Mage_Webhook_Model_Message_Interface $message
* @return Mage_Webhook_Model_Message_Interface
*/
public function decode(Mage_Webhook_Model_Message_Interface $message)
{
$message->setResponseData(
json_decode($message->getResponseBody(), true)
);
return $message;
}
public function newMessage() {
return Mage::getModel('Mage_Webhook_Model_Message');
}
}
Data Format Factory
Data format factory will be used to create the data format based on the configuration. A data format factory needs to implement interface 'Mage_Webhook_Model_Formatter_Factory_Interface'. 'getFormatter' method needs to be implemented in the data format factory. Here is the details of interface 'Mage_Webhook_Model_Formatter_Factory_Interface'.
interface Mage_Webhook_Model_Formatter_Factory_Interface
{
/**
* @param $format string indicating the format used
* @return Mage_Webhook_Model_Formatter_Interface
*/
public function getFormatter($format);
}
Here is the implementation of JSON format factory that is provided by default in Magento.
class Mage_Webhook_Model_Formatter_Factory_Json implements Mage_Webhook_Model_Formatter_Factory_Interface
{
const XML_PATH_DEFAULT_OPTIONS = 'global/webhook/formats/json/options/';
protected $_config;
public function __construct(Mage_Core_Model_Config $config)
{
$this->_config = $config;
}
/**
* @param $format string indicating the format used
* @return Mage_Webhook_Model_Formatter_Interface
*/
public function getFormatter($format)
{
$modelName = (string) $this->_config->getNode(
self::XML_PATH_DEFAULT_OPTIONS . 'format/' . $format . '/formatter'
);
if (!$modelName) {
$modelName = (string) $this->_config->getNode(self::XML_PATH_DEFAULT_OPTIONS . 'default_formatter');
}
if (!$modelName) {
throw new LogicException(
"There is no specific formatter for the format given $format and no default formatter."
);
}
$formatter = $this->getModel($modelName);
if (!$formatter instanceof Mage_Webhook_Model_Formatter_Interface) {
throw new LogicException("Wrong Formatter type for the model found given the format $format.");
}
return $formatter;
}
protected function getModel($modelName)
{
// TODO: probably here we should pass only format configuration options
return Mage::getModel($modelName, $this->_config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment