2021-03-18 08:51:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Model\Table;
|
|
|
|
|
|
|
|
use App\Model\Table\AppTable;
|
|
|
|
use Cake\Filesystem\Folder;
|
2021-06-12 12:09:54 +02:00
|
|
|
use Cake\Http\Exception\MethodNotAllowedException;
|
|
|
|
use Cake\Core\Exception\Exception;
|
|
|
|
|
2021-06-18 11:09:19 +02:00
|
|
|
class MissingInboxProcessorException extends Exception
|
2021-06-12 12:09:54 +02:00
|
|
|
{
|
|
|
|
protected $_defaultCode = 404;
|
|
|
|
}
|
2021-03-18 08:51:11 +01:00
|
|
|
|
2021-06-18 11:59:25 +02:00
|
|
|
class InboxProcessorsTable extends AppTable
|
2021-03-18 08:51:11 +01:00
|
|
|
{
|
2021-06-18 11:09:19 +02:00
|
|
|
private $processorsDirectory = ROOT . '/libraries/default/InboxProcessors';
|
|
|
|
private $inboxProcessors;
|
2021-03-18 15:17:39 +01:00
|
|
|
private $enabledProcessors = [ // to be defined in config
|
|
|
|
'Brood' => [
|
|
|
|
'ToolInterconnection' => false,
|
|
|
|
'OneWaySynchronization' => false,
|
|
|
|
],
|
|
|
|
'Proposal' => [
|
|
|
|
'ProposalEdit' => false,
|
|
|
|
],
|
|
|
|
'Synchronisation' => [
|
|
|
|
'DataExchange' => false,
|
|
|
|
],
|
|
|
|
'User' => [
|
|
|
|
'Registration' => true,
|
|
|
|
],
|
|
|
|
];
|
2021-03-18 08:51:11 +01:00
|
|
|
|
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
parent::initialize($config);
|
|
|
|
$this->loadProcessors();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProcessor($scope, $action=null)
|
|
|
|
{
|
2021-06-18 11:09:19 +02:00
|
|
|
if (isset($this->inboxProcessors[$scope])) {
|
2021-03-18 08:51:11 +01:00
|
|
|
if (is_null($action)) {
|
2021-06-18 11:09:19 +02:00
|
|
|
return $this->inboxProcessors[$scope];
|
|
|
|
} else if (!empty($this->inboxProcessors[$scope]->{$action})) {
|
|
|
|
return $this->inboxProcessors[$scope]->{$action};
|
2021-03-18 08:51:11 +01:00
|
|
|
} else {
|
|
|
|
throw new \Exception(__('Processor {0}.{1} not found', $scope, $action));
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 11:09:19 +02:00
|
|
|
throw new MissingInboxProcessorException(__('Processor not found'));
|
2021-06-12 12:09:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:46:53 +02:00
|
|
|
public function getLocalToolProcessor($action, $connectorName)
|
2021-06-12 12:09:54 +02:00
|
|
|
{
|
|
|
|
$scope = "LocalTool";
|
2021-06-14 16:46:53 +02:00
|
|
|
$specificScope = "{$connectorName}LocalTool";
|
2021-06-12 12:09:54 +02:00
|
|
|
try { // try to get specific processor for module name or fall back to generic local tool processor
|
|
|
|
$processor = $this->getProcessor($specificScope, $action);
|
2021-06-18 11:09:19 +02:00
|
|
|
} catch (MissingInboxProcessorException $e) {
|
2021-06-12 12:09:54 +02:00
|
|
|
$processor = $this->getProcessor($scope, $action);
|
|
|
|
}
|
|
|
|
return $processor;
|
2021-03-18 08:51:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function listProcessors($scope=null)
|
|
|
|
{
|
|
|
|
if (is_null($scope)) {
|
2021-06-18 11:09:19 +02:00
|
|
|
return $this->inboxProcessors;
|
2021-03-18 08:51:11 +01:00
|
|
|
} else {
|
2021-06-18 11:09:19 +02:00
|
|
|
if (isset($this->inboxProcessors[$scope])) {
|
|
|
|
return $this->inboxProcessors[$scope];
|
2021-03-18 08:51:11 +01:00
|
|
|
} else {
|
2021-06-18 11:09:19 +02:00
|
|
|
throw new MissingInboxProcessorException(__('Processors for {0} not found', $scope));
|
2021-03-18 08:51:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadProcessors()
|
|
|
|
{
|
|
|
|
$processorDir = new Folder($this->processorsDirectory);
|
2021-06-18 11:09:19 +02:00
|
|
|
$processorFiles = $processorDir->find('.*InboxProcessor\.php', true);
|
2021-03-18 08:51:11 +01:00
|
|
|
foreach ($processorFiles as $processorFile) {
|
2021-06-18 11:09:19 +02:00
|
|
|
if ($processorFile == 'GenericInboxProcessor.php') {
|
2021-03-18 08:51:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$processorMainClassName = str_replace('.php', '', $processorFile);
|
2021-06-18 11:09:19 +02:00
|
|
|
$processorMainClassNameShort = str_replace('InboxProcessor.php', '', $processorFile);
|
2021-03-18 08:51:11 +01:00
|
|
|
$processorMainClass = $this->getProcessorClass($processorDir->pwd() . DS . $processorFile, $processorMainClassName);
|
2021-06-12 12:09:54 +02:00
|
|
|
if (is_object($processorMainClass)) {
|
2021-06-18 11:09:19 +02:00
|
|
|
$this->inboxProcessors[$processorMainClassNameShort] = $processorMainClass;
|
|
|
|
foreach ($this->inboxProcessors[$processorMainClassNameShort]->getRegisteredActions() as $registeredAction) {
|
|
|
|
$scope = $this->inboxProcessors[$processorMainClassNameShort]->getScope();
|
2021-03-18 15:17:39 +01:00
|
|
|
if (!empty($this->enabledProcessors[$scope][$registeredAction])) {
|
2021-06-18 11:09:19 +02:00
|
|
|
$this->inboxProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = true;
|
2021-03-18 15:17:39 +01:00
|
|
|
} else {
|
2021-06-18 11:09:19 +02:00
|
|
|
$this->inboxProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = false;
|
2021-03-18 15:17:39 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-12 12:09:54 +02:00
|
|
|
} else {
|
2021-06-18 11:09:19 +02:00
|
|
|
$this->inboxProcessors[$processorMainClassNameShort] = new \stdClass();
|
|
|
|
$this->inboxProcessors[$processorMainClassNameShort]->{$registeredAction} = new \stdClass();
|
|
|
|
$this->inboxProcessors[$processorMainClassNameShort]->{$registeredAction}->action = "N/A";
|
|
|
|
$this->inboxProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = false;
|
|
|
|
$this->inboxProcessors[$processorMainClassNameShort]->{$registeredAction}->error = $processorMainClass;
|
2021-03-18 08:51:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-12 12:09:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getProcessorClass
|
|
|
|
*
|
|
|
|
* @param string $filePath
|
|
|
|
* @param string $processorMainClassName
|
|
|
|
* @return object|string Object loading success, string containing the error if failure
|
|
|
|
*/
|
2021-03-18 08:51:11 +01:00
|
|
|
private function getProcessorClass($filePath, $processorMainClassName)
|
|
|
|
{
|
2021-06-12 12:09:54 +02:00
|
|
|
try {
|
|
|
|
require_once($filePath);
|
|
|
|
try {
|
|
|
|
$reflection = new \ReflectionClass($processorMainClassName);
|
|
|
|
} catch (\ReflectionException $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
$processorMainClass = $reflection->newInstance(true);
|
|
|
|
if ($processorMainClass->checkLoading() === 'Assimilation successful!') {
|
|
|
|
return $processorMainClass;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* createInboxEntry
|
|
|
|
*
|
|
|
|
* @param Object|Array $processor can either be the processor object or an array containing data to fetch it
|
|
|
|
* @param Array $data
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function createInboxEntry($processor, $data)
|
|
|
|
{
|
|
|
|
if (!is_object($processor) && !is_array($processor)) {
|
|
|
|
throw new MethodNotAllowedException(__("Invalid processor passed"));
|
|
|
|
}
|
|
|
|
if (is_array($processor)) {
|
|
|
|
if (empty($processor['scope']) || empty($processor['action'])) {
|
|
|
|
throw new MethodNotAllowedException(__("Invalid data passed. Missing either `scope` or `action`"));
|
|
|
|
}
|
|
|
|
$processor = $this->getProcessor('User', 'Registration');
|
2021-03-18 08:51:11 +01:00
|
|
|
}
|
2021-06-12 12:09:54 +02:00
|
|
|
return $processor->create($data);
|
2021-03-18 08:51:11 +01:00
|
|
|
}
|
|
|
|
}
|