1

I'm building an web shop with Zend Framework 2 and this web shop will work with an API which the supplier provides.

We want to sell custom car products like air intakes, air suspension parts & kits, body kits, exhausts, grilles, steering wheels, etc.

The supplier offers an API(just GET calls with params and comma-separated responses) with the following functions

  • Products

    • Search by category;
    • Search by car brand --> model --> type;
    • Search by id;
    • Search by name;
  • Orders

    • Add;
    • View;
    • Delete.
  • Invoices

    • View;
  • Stock get the current values

  • Delivery view current status by orderId.

I have build a module called SupplierName. I have created a Module.php file:

<?php
/**
* This file is placed here for compatibility with ZendFramework 2's ModuleManager.
* It allows usage of this module even without composer.
* The original Module.php is in 'src/SupplierName/Module.php' in order to
* respect the PSR-0 convention
*/
require_once __DIR__ . '/src/SupplierName/Module.php';

vendor/SupplierName/src/SupplierName/Module.php

<?php

namespace SupplierName;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/../../config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__,
                ),
            ),
        );
    }
}

In the indexAction of my IndexController of the main module I use the code beneath to load my module.

$supplierNameClient = $this->getServiceLocator()->get('SupplierName\Client');

And now my problem comes... This is the first time I'll be building a module which only will be used to load data from an external resource and pass it on to my own system. The problem is that I did not manage to think of a good structure of my module... I think there should be a class for every product type(grill, exhaust, steeringWheel, etc), order, invoice, etc.

I've been searching and looked at http://modules.zendframework.com/page/2?query=api. There's a list with modules which should be used to communicate with a webservice.

Could anyone tell me where to look for information / samples to build a module like this? Or could anyone give me an example how I should structure this module?

Many thanks in advance and if something is unclear, please let me know so I can explain it!

1 Answer 1

2

The way I would approach this is to create an entity class for each product type. These are simple PHP classes with properties and relevant methods that represent the product. e.g.

namespace SupplierName\Entity;

class SteeringWheel
{
    protected $diameter;

    public function getDiameter()
    {
        return $this->diameter;
    }

    public function setDiameter($diameter)
    {
        $this->diameter = $diameter;
        return $this;
    }

}

To fetch these entities, I would use a mapper class that talks to the external. e.g.

namespace SupplierName\Mapper;

class SteeringWheelMapper extends AbstractWebServiceMapper
{
    public function fetchbyDiameter($diameter) {
        // construct search query and then call up a parent
        // class method that calls the vendor's webservice
        // that presumably returns an array of data.
        //
        // Within this method, convert that array of data into a
        // set of SteeringWheel objects and return.
    }
}

The common code for talking to the external resource is stored in AbstractVendorWebServiceMapper (and maybe supporting classes), with the specialisation of SteeringWheelMapper dealing solely with steering wheels and creating SteeringWheel entities.

I would then probably add an EventManager to the mapper so that I could set up events and listeners to cache the data from the vendor's web service.

4
  • Thank you, Rob! When I want to show steering wheels, the API wants a brand name, series name and model name. I can call the apiurl.com/steering-wheels/car-brand-list. That URL gives me a list of car brands. Should I just create a method within the SteeringWheelMapper and return an array? Is that the way to do it?
    – ivodvb
    Mar 8, 2013 at 10:18
  • I'd like to use \Zend\Http\Client for the HTTP calls. I have created a file in vendor/SupplierName/src/Suppliername/Mapper/AbstractWebServiceMapper. Should I inject a var with an instance of \Zend\Http\Client into AbstractWebServiceMapper and let Suppliername\Mapper\SteeringWheelMapper extend Suppliername\Mapper\AbstrachtWebServicemapper? Just checkin' if I properly understand your idea...
    – ivodvb
    Mar 8, 2013 at 10:44
  • 1
    That's what I'd do, yes. Inject a configured Client via ServiceManager.
    – Rob Allen
    Mar 8, 2013 at 10:58
  • Thanks! I'm going to accept your answers, but to those of you who think about it different, please let me know!
    – ivodvb
    Mar 8, 2013 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.