Skip to content
Snippets Groups Projects
Commit 2405d421 authored by Christian Spoo's avatar Christian Spoo
Browse files

Skeleton implementation for an example endpoint

parents
No related branches found
No related tags found
No related merge requests found
api/**
bin/**
build/**
.buildpath
composer.lock
composer.phar
db/*
dist/**
docs/**
install/db/*
logs/*
nbproject/**
netbeans/**
.htaccess
.project
.settings/**
.idea/**
tmp/**
vendor/**
*.sublime-project
*.sublime-workspace
sftp-config.json
\ No newline at end of file
{
"name" : "jtl/connector-example",
"type" : "project",
"description" : "JTL-Connector Example endpoint",
"keywords" : ["jtl", "connector", "example"],
"homepage" : "http://www.jtl-software.de",
"license" : "LGPL",
"authors" : [
{
"name" : "Daniel Böhmer",
"email" : "daniel.boehmer@jtl-software.com",
"homepage" : "http://www.jtl-software.de",
"role" : "Developer"
},
{
"name" : "Daniel Hoffmann",
"email" : "daniel.hoffmann@jtl-software.com",
"homepage" : "http://www.jtl-software.de",
"role" : "Developer"
},
{
"name" : "Christian Spoo",
"email" : "christian.spoo@jtl-software.com",
"homepage" : "http://www.jtl-software.de",
"role" : "Developer"
}
],
"repositories" : [
{
"type" : "git",
"url" : "git@gitlab.jtl-software.de:jtl-software-gmbh/jtlconnector.git",
"reference" : "origin/master"
}
],
"require" : {
"php" : ">=5.4.0",
"jtl/connector" : "dev-master"
},
"autoload" : {
"psr-4" : {
"jtl\\Connector\\Example\\" : "src/"
}
}
}
\ No newline at end of file
<?php
/**
* @copyright 2010-2015 JTL-Software GmbH
* @package jtl\Connector\Example
*/
defined('CONNECTOR_DIR') || define("CONNECTOR_DIR", __DIR__);
include (__DIR__ . "/src/bootstrap.php");
\ No newline at end of file
<?php
namespace jtl\Connector\Example;
use jtl\Connector\Checksum\IChecksumLoader;
class ChecksumLoader implements IChecksumLoader
{
/**
* Loads the checksum
*
* @param string $endpointId
* @param int $type
* @return string
*/
public function read($endpointId, $type)
{
// TODO: Implement read() method.
}
/**
* Loads the checksum
*
* @param string $endpointId
* @param int $type
* @param string $checksum
* @return boolean
*/
public function write($endpointId, $type, $checksum)
{
// TODO: Implement write() method.
}
/**
* Loads the checksum
*
* @param string $endpointId
* @param int $type
* @return boolean
*/
public function delete($endpointId, $type)
{
// TODO: Implement delete() method.
}
}
<?php
/**
*
* @copyright 2010-2015 JTL-Software GmbH
* @package jtl\Connector\Example
*/
namespace jtl\Connector\Example;
use jtl\Connector\Base\Connector as BaseConnector;
use jtl\Connector\Core\Exception\TransactionException;
use jtl\Connector\Core\Rpc\Method;
use jtl\Connector\Core\Rpc\RequestPacket;
use jtl\Connector\Core\Utilities\RpcMethod;
use jtl\Connector\Core\Controller\Controller as CoreController;
use jtl\Connector\Example\ChecksumLoader;
use jtl\Connector\Example\PrimaryKeyMapper;
use jtl\Connector\Example\TokenLoader;
use jtl\Connector\Result\Action;
/**
* Example Connector
*
* @access public
* @author Christian Spoo <christian.spoo@jtl-software.com>
*/
class Connector extends BaseConnector
{
/**
* Current Controller
*
* @var jtl\Connector\Core\Controller\Controller
*/
protected $_controller;
/**
*
* @var string
*/
protected $_action;
/**
*
* @var string
*/
protected $_config;
protected function __construct()
{
// Destroy Magento's session
if ('' != session_id()) {
session_destroy();
}
$this->setPrimaryKeyMapper(new PrimaryKeyMapper());
$this->setTokenLoader(new TokenLoader());
$this->setChecksumLoader(new ChecksumLoader());
}
/**
* (non-PHPdoc)
*
* @see \jtl\Connector\Application\IEndpointConnector::canHandle()
*/
public function canHandle()
{
$controller = RpcMethod::buildController($this->getMethod()->getController());
$class = "\\jtl\\Connector\\Magento\\Controller\\{$controller}";
if (class_exists($class)) {
$this->_controller = $class::getInstance();
$this->_action = RpcMethod::buildAction($this->getMethod()->getAction());
return is_callable(array($this->_controller, $this->_action));
}
return false;
}
/**
* (non-PHPdoc)
*
* @see \jtl\Connector\Application\IEndpointConnector::handle()
*/
public function handle(RequestPacket $requestpacket)
{
$config = $this->getConfig();
// Set the config to our controller
$this->_controller->setConfig($config);
// Set the method to our controller
$this->_controller->setMethod($this->getMethod());
if ($this->_action === Method::ACTION_PUSH || $this->_action === Method::ACTION_DELETE) {
if ($this->getMethod()->getController() === 'image') {
return $this->_controller->{$this->_action}($requestpacket->getParams());
}
if (!is_array($requestpacket->getParams())) {
throw new \Exception("Expecting request array, invalid data given");
}
$action = new Action();
$results = array();
$errors = array();
if ($this->_action === Method::ACTION_PUSH && $this->getMethod()->getController() === 'product_price') {
$params = $requestpacket->getParams();
$result = $this->_controller->update($params);
$results[] = $result->getResult();
}
else {
foreach ($requestpacket->getParams() as $param) {
$result = $this->_controller->{$this->_action}($param);
$results[] = $result->getResult();
}
}
$action->setHandled(true)
->setResult($results)
->setError($result->getError()); // @todo: refactor to array of errors
return $action;
}
else {
return $this->_controller->{$this->_action}($requestpacket->getParams());
}
}
/**
* Getter Controller
*
* @return \jtl\Core\Controller\Controller
*/
public function getController()
{
return $this->_controller;
}
/**
* Setter Controller
*
* @param \jtl\Core\Controller\Controller $controller
*/
public function setController(CoreController $controller)
{
$this->_controller = $controller;
}
/**
* Getter Action
*
* @return string
*/
public function getAction()
{
return $this->_action;
}
/**
* Setter Action
*
* @param string $action
*/
public function setAction($action)
{
$this->_action = $action;
}
}
<?php
namespace jtl\Connector\Example\Controller;
use jtl\Connector\Core\Controller\Controller;
use jtl\Connector\Core\Model\DataModel;
use jtl\Connector\Core\Model\QueryFilter;
use jtl\Connector\Core\Rpc\Error;
use jtl\Connector\Result\Action;
use jtl\Connector\Model\ConnectorIdentification;
class Connector extends Controller
{
private $controllers = array(
'Product'
);
public function push(DataModel $model)
{
}
public function delete(DataModel $model)
{
}
public function pull(QueryFilter $filter)
{
}
public function statistic(QueryFilter $filter)
{
$action = new Action();
$action->setHandled(true);
try {
$result = array();
foreach ($this->controllers as $controller) {
$controller = __NAMESPACE__ . '\\' . $controller;
$obj = new $controller();
if (method_exists($obj, 'statistic')) {
$method_result = $obj->statistic($filter);
$result[] = $method_result->getResult();
}
}
$action->setResult($result);
}
catch (\Exception $exc) {
$err = new Error();
$err->setCode($exc->getCode());
$err->setMessage($exc->getMessage());
$action->setError($err);
}
return $action;
}
/**
* Identify
*
* @return \jtl\Connector\Result\Action
*/
public function identify()
{
$action = new Action();
$action->setHandled(true);
$identification = new ConnectorIdentification();
$identification->setEndpointVersion('1.0.0.0')
->setPlatformName('Example')
->setPlatformVersion('1.0')
->setProtocolVersion(Application()->getProtocolVersion());
$action->setResult($identification);
return $action;
}
/**
* Finish
*
* @return \jtl\Connector\Result\Action
*/
public function finish()
{
return $action;
}
}
<?php
namespace jtl\Connector\Example\Controller;
use jtl\Connector\Core\Controller\Controller;
use jtl\Connector\Core\Model\DataModel;
use jtl\Connector\Core\Model\QueryFilter;
class Product extends Controller
{
public function push(DataModel $model)
{
}
public function delete(DataModel $model)
{
}
public function pull(QueryFilter $filter)
{
}
public function statistic(QueryFilter $filter)
{
}
}
<?php
namespace jtl\Connector\Example;
use jtl\Connector\Core\Logger\Logger;
use jtl\Connector\Drawing\ImageRelationType;
use jtl\Connector\Linker\IdentityLinker;
use jtl\Connector\Mapper\IPrimaryKeyMapper;
class PrimaryKeyMapper implements IPrimaryKeyMapper
{
/**
* Host ID getter
*
* @param string $endpointId
* @param integer $type
* @return integer|null
*/
public function getHostId($endpointId, $type)
{
// TODO: Implement getHostId() method.
}
/**
* Endpoint ID getter
*
* @param integer $hostId
* @param integer $type
* @return string|null
*/
public function getEndpointId($hostId, $type)
{
// TODO: Implement getEndpointId() method.
}
/**
* Save link to database
*
* @param string $endpointId
* @param integer $hostId
* @param integer $type
* @return boolean
*/
public function save($endpointId, $hostId, $type)
{
// TODO: Implement save() method.
}
/**
* Delete link from database
*
* @param string $endpointId
* @param integer $hostId
* @param integer $type
* @return boolean
*/
public function delete($endpointId = null, $hostId = null, $type)
{
// TODO: Implement delete() method.
}
/**
* Clears the entire link table
*
* @return boolean
*/
public function clear()
{
// TODO: Implement clear() method.
}
/**
* Garbage Collect the entire link table
*
* @return boolean
*/
public function gc()
{
// TODO: Implement gc() method.
}
}
<?php
namespace jtl\Connector\Example;
use jtl\Connector\Authentication\ITokenLoader;
class TokenLoader implements ITokenLoader
{
/**
* Loads the connector token
*
* @return string
*/
public function load()
{
// Static example token
// TODO: Replace by a more secure one
return 'miesu5eicaech6ohy5aigh0aiz6toh7O';
}
}
<?php
/**
* @copyright 2010-2015 JTL-Software GmbH
* @package jtl\Connector\Example
*/
require_once (__DIR__ . "/../vendor/autoload.php");
use jtl\Connector\Application\Application;
use jtl\Connector\Core\Rpc\RequestPacket;
use jtl\Connector\Core\Rpc\ResponsePacket;
use jtl\Connector\Core\Rpc\Error;
use jtl\Connector\Core\Http\Response;
use jtl\Connector\Example\Connector;
function exception_handler(\Exception $exception)
{
$trace = $exception->getTrace();
if (isset($trace[0]['args'][0])) {
$requestpacket = $trace[0]['args'][0];
}
$error = new Error();
$error->setCode($exception->getCode())
->setData("Exception: " . substr(strrchr(get_class($exception), "\\"), 1) . " - File: {$exception->getFile()} - Line: {$exception->getLine()}")
->setMessage($exception->getMessage());
$responsepacket = new ResponsePacket();
$responsepacket->setError($error)
->setJtlrpc("2.0");
if (isset($requestpacket) && $requestpacket !== null && is_object($requestpacket) && get_class($requestpacket) == "jtl\\Core\\Rpc\\RequestPacket") {
$responsepacket->setId($requestpacket->getId());
}
Response::send($responsepacket);
}
set_exception_handler('exception_handler');
try
{
$logDir = CONNECTOR_DIR . DIRECTORY_SEPARATOR . 'logs';
if (!is_dir($logDir)) {
mkdir($logDir);
chmod($logDir, 0777);
}
// Connector instance
$connector = Connector::getInstance();
$application = Application::getInstance();
$application->register($connector);
$application->run();
}
catch (\Exception $e)
{
exception_handler($e);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment