Magento & the PHP/Java Bridge for External Integrations

Home / Blog / Magento & the PHP/Java Bridge for External Integrations

A current client of ours had the requirement to integrate with a 3rd party fulfillment system via web services. Since this client is mostly familiar with Java, they had previously created all the web service client code in this language, and they had hopes that they could in some way retrieve the Magento data in Java and directly communicate to the fulfillment system.

This is where the PHP/Java bridge comes into play. They identified that they could use this bridge to access Magento data, but they required some guidance on the best approach to get this working.

Brim was able to develop an elegant solution, that we believe could help others in similar situations. The approach requires 3 pieces:

1. Magento module

The module will be used to hook events that you would like to propagate to your 3rd party systems. In our case, we had to hook into an event that is fired after an order is placed. We created an observer that listed for the event, retrieved the order object, wrapped this object in an adapter (see step 2) that exposed Magento methods to Java, and invoked the necessary java object.

2. Adapter class

We wrote a class that exposed the Magento Varian_Object’s “getData” method as methods that returned java castings. For example, by wrapping the shipping address object in the wrapper, we could introduce a method such as :


class Company_Bridge_Model_Adapter_Object
{

	protected $_object;

	/**
	 *
	 * @param Varien_Object $object
	 */
	public function __construct($object){
		$this->_object = $object;
	}

	/**
	 *
	 * @param java $key
	 */
	protected function getData($key){
		$key = java_values($key);

		if($key){
			return $this->_object->getData($key);
		}
		return null;
	}

	public function getString($key){
		if($value = $this->getData($key)){
			return yasmin online cheap java_cast($value, "string");
		}
		return null;
	}
}

As you can see, our adapter wraps a normal Magento Varian object, and exposes this to Java via the getString method. In practice, you can expose methods for each of the Java casting types.

3. Java interfaces

To instruct the PHP/Java bridge how to correctly create our PHP objects, we created interfaces that would logically map to our PHP adapter classes. Meaning, we would create a Java interface for each Magento object we wished to share (i.e. Order, Item, Address, etc..), and each interface would extend a base interface, which would contain the methods exposed by the PHP adapters (i.e. getString, getLong, getObject, etc…).

Example:


public interface MagentoObject {

    public String getString(String key);
}

With these pieces in place, our Observer class is now able to get a reference to a Java class, pass any Magento object (wrapped by our adapter) to the Java class, and then within Java, we can code just as any other Java class against are interfaces we created.

From PHP/Magento, we can code something like this:


// this is where we wrap our magento object
$objectAdapter = new Company_Bridge_Model_Adapter_Object($varianObject);

// get a reference to some java class
$externalObject = new Java("com.company.magento.Test");

// pass our magento wrapper to the java object. From Java, we can call getString() on the 
// magento object since we defined this method in our java interface. Here, we instruct
// the PHP/Java bridge that our magento object abides by the MagentoObject Interface contract
$externalObject->test(java_closure($objectAdapter, null, java('com.company.magento.MagentoObject')));

If anyone would like additional example code, we would be happy to mock up some. Let us know.

Showing 3 comments
  • badmash

    I just signed up to your blogs rss feed. Will you post more on this subject?

  • tmillhouse

    Is there any scenario you’d like us to over? I’m sure there will be more content as we identify helpful scenarios.

  • mackdaniel

    this was a really nice post, thanks

Leave a Comment