chuk-shirley
2/9/2015 - 2:02 PM

Domain Design for Apigility on IBM i

Domain Design for Apigility on IBM i

<?php
// /module/Acme/src/Acme/V1/Rest/Customer/CustomerResourceFactory.php

namespace Acme\V1\Rest\Customer;

class CustomerResourceFactory
{
	public function __invoke($services)
	{
		$toolkitService = $services->get('tkconn');
		return new CustomerResource($toolkitService);
	}
}
<?php
// module/Acme/src/Acme/V1/Rest/Customer/CustomerResource.php

namespace Acme\V1\Rest\Customer;

class CustomerResource extends AbstractResourceListener
{
	protected $toolkitService;
	
	public function __construct($toolkitService)
	{
		$this->toolkitService = $toolkitService;
	}
	
	/**
	* Create a customer
	*
	* @param mixed $data
	* return ApiProblem|mixed
	*/
	public function create($data)
	{
		// Construct parameters for toolkit call
		$params = [
			$this->toolkitService->AddParameterChar('both', 25, 'name', 'customer name', $data->name),
			$this->toolkitService->AddparameterChar('both', 50, 'address', 'customer address', $data->address),
		];
		
		// Call program
		$result = $this->toolkitService->PgmCall('PGMNAME', 'LIBNAME', $params, null, null);
		
		// Error handling for toolkit
		if (!$result)
		{
			return new ApiProblem(500, 'Problem calling program. Code:'.$this->toolkitService->getErrorCode().'. Text: '.$this->toolkitService->getErrorMsg());
		}
		
		// Error handling for everything else
		if ($result['io_param']['errorCode'] > 0)
		{
			// TODO: Delegate error handling to another class
			return new ApiProblem(
				$result['io_param']['errorCode'],
				$result['io_param']['errorMessage']
			);
		}
		
		$customer = $result['io_param'];
		
		return $customer;
	}
}
<?php
// module/Acme/src/Acme/V1/Rest/Customer/CustomerEntity.php

namespace Acme\V1\Rest\Customer;

class CustomerEntity
{
	public $id;
	public $name;
	public $address;
}
<?php
// module/Acme/src/Acme/V1/Rest/Customer/CustomerCollection.php
namespace Acme\V1\Rest\PeddlerPricingFormula;

use Zend\Paginator\Paginator;

class CustomerCollection extends Paginator
{
	
}