chg: [brood] Moved request sender handler in the brood table

pull/59/head
mokaddem 2021-06-18 10:17:02 +02:00
parent 49a4c37eb6
commit ae423bfb68
2 changed files with 35 additions and 22 deletions

View File

@ -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');
}
}

View File

@ -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');
}
}