chg: [requestProcessor] Added placeholder for future request processors

pull/41/head
mokaddem 2021-03-18 15:17:39 +01:00
parent f3f73a475b
commit e7a0f3f2ff
7 changed files with 310 additions and 0 deletions

View File

@ -0,0 +1,116 @@
<?php
use Cake\ORM\TableRegistry;
require_once(ROOT . DS . 'libraries' . DS . 'RequestProcessors' . DS . 'GenericRequestProcessor.php');
class BroodRequestProcessor extends GenericRequestProcessor
{
protected $scope = 'Brood';
protected $action = 'not-specified'; //overriden when extending
protected $description = ''; // overriden when extending
protected $registeredActions = [
'ToolInterconnection',
'OneWaySynchronization',
];
public function __construct($loadFromAction=false) {
parent::__construct($loadFromAction);
}
public function create($requestData)
{
return parent::create($requestData);
}
}
class ToolInterconnectionProcessor extends BroodRequestProcessor implements GenericProcessorActionI {
public $action = 'ToolInterconnection';
protected $description;
public function __construct() {
parent::__construct();
$this->description = __('Handle tool interconnection request from other cerebrate instance');
$this->Broods = TableRegistry::getTableLocator()->get('Broods');
}
protected function addValidatorRules($validator)
{
return $validator;
}
public function create($requestData) {
$this->validateRequestData($requestData);
$requestData['title'] = __('Cerebrate instance {0} requested interconnection for tool {1}', 'Insert brood name', 'Insert tool name');
return parent::create($requestData);
}
public function setViewVariables($controller, $request)
{
}
public function process($id, $requestData)
{
$connectionSuccessfull = false;
$interConnectionResult = [];
if ($connectionSuccessfull) {
$this->discard($id, $requestData);
}
return $this->genActionResult(
$interConnectionResult,
$connectionSuccessfull,
$connectionSuccessfull ? __('Interconnection for `{0}` created', 'Insert tool name') : __('Could interconnect tool `{0}`.', 'Insert tool name'),
[]
);
}
public function discard($id, $requestData)
{
return parent::discard($id, $requestData);
}
}
class OneWaySynchronizationProcessor extends BroodRequestProcessor implements GenericProcessorActionI {
public $action = 'OneWaySynchronization';
protected $description;
public function __construct() {
parent::__construct();
$this->description = __('Handle cerebrate connection request for another cerebrate instance');
$this->Broods = TableRegistry::getTableLocator()->get('Broods');
}
protected function addValidatorRules($validator)
{
return $validator;
}
public function create($requestData) {
$this->validateRequestData($requestData);
$requestData['title'] = __('Cerebrate instance {0} requested interconnection', 'Insert cerebrate name');
return parent::create($requestData);
}
public function setViewVariables($controller, $request)
{
}
public function process($id, $requestData)
{
$connectionSuccessfull = false;
$interConnectionResult = [];
if ($connectionSuccessfull) {
$this->discard($id, $requestData);
}
return $this->genActionResult(
$interConnectionResult,
$connectionSuccessfull,
$connectionSuccessfull ? __('Interconnection with `{0}` created', 'Insert cerebrate name') : __('Could interconnect with `{0}`.', 'Insert cerebrate name'),
[]
);
}
public function discard($id, $requestData)
{
return parent::discard($id, $requestData);
}
}

View File

@ -33,6 +33,15 @@ class GenericRequestProcessor
$file->close();
}
public function getRegisteredActions()
{
return $this->registeredActions;
}
public function getScope()
{
return $this->scope;
}
private function getProcessingTemplatePath()
{
$class = str_replace('RequestProcessor', '', get_parent_class($this));

View File

@ -0,0 +1,69 @@
<?php
use Cake\ORM\TableRegistry;
require_once(ROOT . DS . 'libraries' . DS . 'RequestProcessors' . DS . 'GenericRequestProcessor.php');
class ProposalRequestProcessor extends GenericRequestProcessor
{
protected $scope = 'Proposal';
protected $action = 'not-specified'; //overriden when extending
protected $description = ''; // overriden when extending
protected $registeredActions = [
'ProposalEdit'
];
public function __construct($loadFromAction=false) {
parent::__construct($loadFromAction);
}
public function create($requestData)
{
return parent::create($requestData);
}
}
class ProposalEditProcessor extends ProposalRequestProcessor implements GenericProcessorActionI {
public $action = 'ProposalEdit';
protected $description;
public function __construct() {
parent::__construct();
$this->description = __('Handle proposal from users for this cerebrate instance');
$this->Users = TableRegistry::getTableLocator()->get('Users');
}
protected function addValidatorRules($validator)
{
return $validator;
}
public function create($requestData) {
$this->validateRequestData($requestData);
$requestData['title'] = __('User `{0}` would like to modify record `{0}`', 'username', 'recordname');
return parent::create($requestData);
}
public function setViewVariables($controller, $request)
{
}
public function process($id, $requestData)
{
$proposalAccepted = false;
$saveResult = [];
if ($proposalAccepted) {
$this->discard($id, $requestData);
}
return $this->genActionResult(
$saveResult,
$proposalAccepted,
$proposalAccepted ? __('Record `{0}` modify', 'recordname') : __('Could modify record `{0}`.', 'recordname'),
[]
);
}
public function discard($id, $requestData)
{
return parent::discard($id, $requestData);
}
}

View File

@ -0,0 +1,69 @@
<?php
use Cake\ORM\TableRegistry;
require_once(ROOT . DS . 'libraries' . DS . 'RequestProcessors' . DS . 'GenericRequestProcessor.php');
class SynchronisationRequestProcessor extends GenericRequestProcessor
{
protected $scope = 'Synchronisation';
protected $action = 'not-specified'; //overriden when extending
protected $description = ''; // overriden when extending
protected $registeredActions = [
'DataExchange'
];
public function __construct($loadFromAction=false) {
parent::__construct($loadFromAction);
}
public function create($requestData)
{
return parent::create($requestData);
}
}
class DataExchangeProcessor extends SynchronisationRequestProcessor implements GenericProcessorActionI {
public $action = 'DataExchange';
protected $description;
public function __construct() {
parent::__construct();
$this->description = __('Handle exchange of data between two cerebrate instances');
$this->Users = TableRegistry::getTableLocator()->get('Users');
}
protected function addValidatorRules($validator)
{
return $validator;
}
public function create($requestData) {
$this->validateRequestData($requestData);
$requestData['title'] = __('Data exchange requested for record `{0}`', 'recordname');
return parent::create($requestData);
}
public function setViewVariables($controller, $request)
{
}
public function process($id, $requestData)
{
$dataExchangeAccepted = false;
$saveResult = [];
if ($dataExchangeAccepted) {
$this->discard($id, $requestData);
}
return $this->genActionResult(
$saveResult,
$dataExchangeAccepted,
$dataExchangeAccepted ? __('Record `{0}` exchanged', 'recordname') : __('Could not exchange record `{0}`.', 'recordname'),
[]
);
}
public function discard($id, $requestData)
{
return parent::discard($id, $requestData);
}
}

View File

@ -6,6 +6,7 @@ use App\Controller\AppController;
use Cake\Utility\Hash;
use Cake\Utility\Text;
use \Cake\Database\Expression\QueryExpression;
use Cake\ORM\TableRegistry;
class BroodsController extends AppController
{
@ -154,4 +155,21 @@ class BroodsController extends AppController
$this->redirect($this->referer());
}
}
public function interconnectTools()
{
$this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor');
$processor = $this->requestProcessor->getProcessor('Brood', 'ToolInterconnection');
$data = [
'origin' => '127.0.0.1',
'comment' => 'Test comment',
'data' => [
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
],
];
$processorResult = $processor->create($data);
return $processor->genHTTPReply($this, $processorResult, ['scope' => 'Brood', 'action' => 'ToolInterconnection'], ['controller' => 'Broods', 'action' => 'index']);
}
}

View File

@ -102,6 +102,7 @@ class InboxController extends AppController
foreach ($requestProcessors as $scope => $processors) {
foreach ($processors as $processor) {
$data[] = [
'enabled' => $processor->enabled,
'scope' => $scope,
'action' => $processor->action
];
@ -109,6 +110,11 @@ class InboxController extends AppController
}
$this->set('title', 'Available request processors');
$this->set('fields', [
[
'name' => 'Enabled',
'data_path' => 'enabled',
'element' => 'boolean'
],
[
'name' => 'Processor scope',
'data_path' => 'scope',

View File

@ -9,6 +9,21 @@ class RequestProcessorTable extends AppTable
{
private $processorsDirectory = ROOT . '/libraries/RequestProcessors';
private $requestProcessors;
private $enabledProcessors = [ // to be defined in config
'Brood' => [
'ToolInterconnection' => false,
'OneWaySynchronization' => false,
],
'Proposal' => [
'ProposalEdit' => false,
],
'Synchronisation' => [
'DataExchange' => false,
],
'User' => [
'Registration' => true,
],
];
public function initialize(array $config): void
{
@ -65,6 +80,14 @@ class RequestProcessorTable extends AppTable
$processorMainClass = $this->getProcessorClass($processorDir->pwd() . DS . $processorFile, $processorMainClassName);
if ($processorMainClass !== false) {
$this->requestProcessors[$processorMainClassNameShort] = $processorMainClass;
foreach ($this->requestProcessors[$processorMainClassNameShort]->getRegisteredActions() as $registeredAction) {
$scope = $this->requestProcessors[$processorMainClassNameShort]->getScope();
if (!empty($this->enabledProcessors[$scope][$registeredAction])) {
$this->requestProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = true;
} else {
$this->requestProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = false;
}
}
}
}
}