From 414ac9a59fc7daa1999b8ab80c20dc3d711711f1 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Thu, 18 Mar 2021 08:51:11 +0100 Subject: [PATCH] chg: [requestProcessor] Refactoring code organisation --- .../GenericRequestProcessor.php | 16 +++- .../UserRequestProcessor.php | 9 +-- src/Controller/InboxController.php | 54 +++++++++---- src/Controller/UsersController.php | 4 +- src/Model/Table/InboxTable.php | 51 ------------ src/Model/Table/RequestProcessorTable.php | 81 +++++++++++++++++++ templates/genericTemplates/index_simple.php | 12 +++ 7 files changed, 149 insertions(+), 78 deletions(-) create mode 100644 src/Model/Table/RequestProcessorTable.php create mode 100644 templates/genericTemplates/index_simple.php diff --git a/libraries/RequestProcessors/GenericRequestProcessor.php b/libraries/RequestProcessors/GenericRequestProcessor.php index 614de69..8f1893d 100644 --- a/libraries/RequestProcessors/GenericRequestProcessor.php +++ b/libraries/RequestProcessors/GenericRequestProcessor.php @@ -14,7 +14,7 @@ interface GenericProcessorActionI class GenericRequestProcessor { - public $Inbox; + protected $Inbox; protected $registeredActions = []; protected $validator; private $processingTemplate = '/genericTemplates/confirm'; @@ -56,7 +56,7 @@ class GenericRequestProcessor $request = $this->Inbox->newEmptyEntity(); $request = $this->Inbox->patchEntity($request, $requestData); if ($request->getErrors()) { - throw new MethodNotAllowed(__('Could not create request.{0}Reason: {1}', PHP_EOL, json_encode($request->getErrors())), 1); + throw new Exception(__('Could not create request.{0}Reason: {1}', PHP_EOL, json_encode($request->getErrors())), 1); } return $request; } @@ -103,6 +103,18 @@ class GenericRequestProcessor } public function create($requestData) + { + $requestData['scope'] = $this->scope; + $requestData['action'] = $this->action; + $requestData['description'] = $this->description; + $request = $this->generateRequest($requestData); + $savedRequest = $this->Inbox->save($request); + if ($savedRequest !== false) { + // log here + } + } + + public function discard($requestData) { $request = $this->generateRequest($requestData); $savedRequest = $this->Inbox->save($request); diff --git a/libraries/RequestProcessors/UserRequestProcessor.php b/libraries/RequestProcessors/UserRequestProcessor.php index 8cb805c..230e2fe 100644 --- a/libraries/RequestProcessors/UserRequestProcessor.php +++ b/libraries/RequestProcessors/UserRequestProcessor.php @@ -6,8 +6,8 @@ require_once(ROOT . DS . 'libraries' . DS . 'RequestProcessors' . DS . 'GenericR class UserRequestProcessor extends GenericRequestProcessor { protected $scope = 'User'; - protected $action = 'overridden-in-processor-action'; - protected $description = 'overridden-in-processor-action'; + protected $action = 'not-specified'; //overriden when extending + protected $description = ''; // overriden when extending protected $registeredActions = [ 'Registration' ]; @@ -18,15 +18,12 @@ class UserRequestProcessor extends GenericRequestProcessor public function create($requestData) { - $requestData['scope'] = $this->scope; - $requestData['action'] = $this->action; - $requestData['description'] = $this->description; parent::create($requestData); } } class RegistrationProcessor extends UserRequestProcessor implements GenericProcessorActionI { - protected $action = 'Registration'; + public $action = 'Registration'; protected $description; public function __construct() { diff --git a/src/Controller/InboxController.php b/src/Controller/InboxController.php index dd00a5c..92f8dab 100644 --- a/src/Controller/InboxController.php +++ b/src/Controller/InboxController.php @@ -3,10 +3,11 @@ namespace App\Controller; use App\Controller\AppController; -use Cake\Utility\Hash; -use Cake\Utility\Text; use Cake\Database\Expression\QueryExpression; use Cake\Event\EventInterface; +use Cake\ORM\TableRegistry; +use Cake\Utility\Hash; +use Cake\Utility\Text; use Cake\Http\Exception\NotFoundException; use Cake\Http\Exception\MethodNotAllowedException; use Cake\Http\Exception\ForbiddenException; @@ -47,15 +48,6 @@ class InboxController extends AppController $this->CRUD->filtering(); } - // public function add() - // { - // $this->CRUD->add(); - // $responsePayload = $this->CRUD->getResponsePayload(); - // if (!empty($responsePayload)) { - // return $responsePayload; - // } - // } - public function view($id) { $this->CRUD->view($id); @@ -79,7 +71,8 @@ class InboxController extends AppController $request = $this->Inbox->get($id); $scope = $request->scope; $action = $request->action; - $processor = $this->Inbox->getRequestProcessor($scope, $action); + $this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor'); + $processor = $this->requestProcessor->getProcessor($scope, $action); if ($this->request->is('post')) { $processResult = $processor->process($id, $this->request); if ($processResult['success']) { @@ -91,11 +84,38 @@ class InboxController extends AppController } return $response; } else { - $processor->setViewVariables($this, $request); - $processingTemplate = $processor->getProcessingTemplate(); - $this->set('request', $request); - $this->viewBuilder()->setLayout('ajax'); - $this->render($processingTemplate); + $this->requestProcessor->render($this, $processor, $request); } } + + public function listProcessors() + { + $this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor'); + $requestProcessors = $this->requestProcessor->listProcessors(); + if ($this->ParamHandler->isRest()) { + return $this->RestResponse->viewData($requestProcessors, 'json'); + } + $data = []; + foreach ($requestProcessors as $scope => $processors) { + foreach ($processors as $processor) { + $data[] = [ + 'scope' => $scope, + 'action' => $processor->action + ]; + } + } + $this->set('title', 'Available request processors'); + $this->set('fields', [ + [ + 'name' => 'Processor scope', + 'data_path' => 'scope', + ], + [ + 'name' => 'Processor action', + 'data_path' => 'action', + ] + ]); + $this->set('data', $data); + $this->render('/genericTemplates/index_simple'); + } } diff --git a/src/Controller/UsersController.php b/src/Controller/UsersController.php index 742e247..f5a4597 100644 --- a/src/Controller/UsersController.php +++ b/src/Controller/UsersController.php @@ -138,8 +138,8 @@ class UsersController extends AppController public function register() { - $this->Inbox = TableRegistry::getTableLocator()->get('Inbox'); - $processor = $this->Inbox->getRequestProcessor('User', 'Registration'); + $this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor'); + $processor = $this->requestProcessor->getProcessor('User', 'Registration'); $data = [ 'origin' => '127.0.0.1', 'comment' => 'Hi there!, please create an account', diff --git a/src/Model/Table/InboxTable.php b/src/Model/Table/InboxTable.php index 0a04b9a..93a7f22 100644 --- a/src/Model/Table/InboxTable.php +++ b/src/Model/Table/InboxTable.php @@ -4,7 +4,6 @@ namespace App\Model\Table; use App\Model\Table\AppTable; use Cake\Database\Schema\TableSchemaInterface; use Cake\Database\Type; -use Cake\Filesystem\Folder; use Cake\ORM\Table; use Cake\ORM\RulesChecker; use Cake\Validation\Validator; @@ -13,8 +12,6 @@ Type::map('json', 'Cake\Database\Type\JsonType'); class InboxTable extends AppTable { - private $processorsDirectory = ROOT . '/libraries/RequestProcessors'; - private $requestProcessors; public function initialize(array $config): void { @@ -65,52 +62,4 @@ class InboxTable extends AppTable return $rules; } - - public function getRequestProcessor($name, $action=null) - { - if (!isset($this->requestProcessors)) { - $this->loadRequestProcessors(); - } - if (isset($this->requestProcessors[$name])) { - if (is_null($action)) { - return $this->requestProcessors[$name]; - } else if (!empty($this->requestProcessors[$name]->{$action})) { - return $this->requestProcessors[$name]->{$action}; - } else { - throw new \Exception(__('Processor {0}.{1} not found', $name, $action)); - } - } - throw new \Exception(__('Processor not found'), 1); - } - - private function loadRequestProcessors() - { - $processorDir = new Folder($this->processorsDirectory); - $processorFiles = $processorDir->find('.*RequestProcessor\.php', true); - foreach ($processorFiles as $processorFile) { - if ($processorFile == 'GenericRequestProcessor.php') { - continue; - } - $processorMainClassName = str_replace('.php', '', $processorFile); - $processorMainClassNameShort = str_replace('RequestProcessor.php', '', $processorFile); - $processorMainClass = $this->getProcessorClass($processorDir->pwd() . DS . $processorFile, $processorMainClassName); - if ($processorMainClass !== false) { - $this->requestProcessors[$processorMainClassNameShort] = $processorMainClass; - } - } - } - - private function getProcessorClass($filePath, $processorMainClassName) - { - require_once($filePath); - $reflection = new \ReflectionClass($processorMainClassName); - $processorMainClass = $reflection->newInstance(true); - if ($processorMainClass->checkLoading() === 'Assimilation successful!') { - return $processorMainClass; - } - try { - } catch (Exception $e) { - return false; - } - } } diff --git a/src/Model/Table/RequestProcessorTable.php b/src/Model/Table/RequestProcessorTable.php new file mode 100644 index 0000000..25485a7 --- /dev/null +++ b/src/Model/Table/RequestProcessorTable.php @@ -0,0 +1,81 @@ +loadProcessors(); + } + + public function getProcessor($scope, $action=null) + { + if (isset($this->requestProcessors[$scope])) { + if (is_null($action)) { + return $this->requestProcessors[$scope]; + } else if (!empty($this->requestProcessors[$scope]->{$action})) { + return $this->requestProcessors[$scope]->{$action}; + } else { + throw new \Exception(__('Processor {0}.{1} not found', $scope, $action)); + } + } + throw new \Exception(__('Processor not found'), 1); + } + + public function render($controller, $processor, $request=[]) + { + $processor->setViewVariables($controller, $request); + $controller->set('request', $request); + $controller->viewBuilder()->setLayout('ajax'); + $processingTemplate = $processor->getProcessingTemplate(); + $controller->render($processingTemplate); + } + + public function listProcessors($scope=null) + { + if (is_null($scope)) { + return $this->requestProcessors; + } else { + if (isset($this->requestProcessors[$scope])) { + return $this->requestProcessors[$scope]; + } else { + throw new \Exception(__('Processors for {0} not found', $scope)); + } + } + } + + private function loadProcessors() + { + $processorDir = new Folder($this->processorsDirectory); + $processorFiles = $processorDir->find('.*RequestProcessor\.php', true); + foreach ($processorFiles as $processorFile) { + if ($processorFile == 'GenericRequestProcessor.php') { + continue; + } + $processorMainClassName = str_replace('.php', '', $processorFile); + $processorMainClassNameShort = str_replace('RequestProcessor.php', '', $processorFile); + $processorMainClass = $this->getProcessorClass($processorDir->pwd() . DS . $processorFile, $processorMainClassName); + if ($processorMainClass !== false) { + $this->requestProcessors[$processorMainClassNameShort] = $processorMainClass; + } + } + } + + private function getProcessorClass($filePath, $processorMainClassName) + { + require_once($filePath); + $reflection = new \ReflectionClass($processorMainClassName); + $processorMainClass = $reflection->newInstance(true); + if ($processorMainClass->checkLoading() === 'Assimilation successful!') { + return $processorMainClass; + } + } +} diff --git a/templates/genericTemplates/index_simple.php b/templates/genericTemplates/index_simple.php new file mode 100644 index 0000000..d0f2c0b --- /dev/null +++ b/templates/genericTemplates/index_simple.php @@ -0,0 +1,12 @@ +element('genericElements/IndexTable/index_table', [ + 'data' => [ + 'skip_pagination' => true, + 'data' => !empty($data) ? $data : [], + 'top_bar' => [], + 'fields' => !empty($fields) ? $fields : [], + 'title' => !empty($title) ? h($title) : __('Index'), + 'description' => !empty($description) ? h($description) : '', + 'actions' => !empty($actions) ? $actions : [] + ], +]); \ No newline at end of file