From ae423bfb68f18983361f8638a665ff1f4d47b47d Mon Sep 17 00:00:00 2001 From: mokaddem Date: Fri, 18 Jun 2021 10:17:02 +0200 Subject: [PATCH] chg: [brood] Moved request sender handler in the brood table --- src/Model/Table/BroodsTable.php | 36 +++++++++++++++++++++++++++-- src/Model/Table/LocalToolsTable.php | 21 +---------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/Model/Table/BroodsTable.php b/src/Model/Table/BroodsTable.php index 7113d62..e1dbd27 100644 --- a/src/Model/Table/BroodsTable.php +++ b/src/Model/Table/BroodsTable.php @@ -228,11 +228,20 @@ class BroodsTable extends AppTable return $data; } - public function sendLocalToolConnectionRequest($params, $data): Response + public function sendLocalToolConnectionRequest($params, $data): array { $url = '/inbox/createInboxEntry/LocalTool/IncomingConnectionRequest'; $data = $this->injectRequiredData($params, $data); - return $this->sendRequest($params['remote_cerebrate'], $url, true, $data); + $response = $this->sendRequest($params['remote_cerebrate'], $url, true, $data); + try { + $jsonReply = $response->getJson(); + if (empty($jsonReply['success'])) { + $this->handleMessageNotCreated($response); + } + } catch (NotFoundException $e) { + $jsonReply = $this->handleSendingFailed($response); + } + return $jsonReply; } public function sendLocalToolAcceptedRequest($params, $data): Response @@ -248,4 +257,27 @@ class BroodsTable extends AppTable $data = $this->injectRequiredData($params, $data); return $this->sendRequest($params['remote_cerebrate'], $url, true, $data); } + + /** + * handleSendingFailed - Handle the case if the request could not be sent or if the remote rejected the connection request + * + * @param Object $response + * @return array + */ + private function handleSendingFailed(Object $response): array + { + // debug('sending failed. Modify state and add entry in outbox'); + throw new NotFoundException(__('sending failed. Modify state and add entry in outbox')); + } + + /** + * handleMessageNotCreated - Handle the case if the request was sent but the remote brood did not save the message in the inbox + * + * @param Object $response + * @return array + */ + private function handleMessageNotCreated(Object $response): array + { + // debug('Saving message failed. Modify state and add entry in outbox'); + } } diff --git a/src/Model/Table/LocalToolsTable.php b/src/Model/Table/LocalToolsTable.php index 159878a..876cda5 100644 --- a/src/Model/Table/LocalToolsTable.php +++ b/src/Model/Table/LocalToolsTable.php @@ -250,26 +250,7 @@ class LocalToolsTable extends AppTable public function sendEncodedConnection($params, $encodedConnection) { $this->Broods = \Cake\ORM\TableRegistry::getTableLocator()->get('Broods'); - try { - $response = $this->Broods->sendLocalToolConnectionRequest($params, $encodedConnection); - $jsonReply = $response->getJson(); - if (empty($jsonReply['success'])) { - $this->handleMessageNotCreated($response); - } - } catch (NotFoundException $e) { - return $this->handleSendingFailed($response); - } + $jsonReply = $this->Broods->sendLocalToolConnectionRequest($params, $encodedConnection); return $jsonReply; } - - public function handleSendingFailed($response) - { - // debug('sending failed. Modify state and add entry in outbox'); - throw new NotFoundException(__('sending failed. Modify state and add entry in outbox')); - } - - public function handleMessageNotCreated($response) - { - // debug('Saving message failed. Modify state and add entry in outbox'); - } }