From 970d451b7dba2954d1c7551978ccf603b05d0456 Mon Sep 17 00:00:00 2001 From: Leonard LEUNG CHONG WO Date: Wed, 30 Aug 2017 15:16:45 +0200 Subject: [PATCH] [FEATURE#56754] gestion des messages dans le proxy --- src/Conversation.php | 12 +++++++++ src/ConversationProxy.php | 4 +++ src/MessageManager.php | 55 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/MessageManager.php diff --git a/src/Conversation.php b/src/Conversation.php index c62480d..572211b 100644 --- a/src/Conversation.php +++ b/src/Conversation.php @@ -263,6 +263,18 @@ public function getMessages() return $this->messages; } + /** + * Set conversation messages + * + * @param array $messages + * + * @return array + */ + public function setMessages($messages) + { + $this->messages = $messages; + } + /** * Get conversation's last message * diff --git a/src/ConversationProxy.php b/src/ConversationProxy.php index 50ec14f..71a732d 100644 --- a/src/ConversationProxy.php +++ b/src/ConversationProxy.php @@ -47,6 +47,10 @@ public function register(Container $app) return new ConversationManager($app); }; + $app["messages"] = function ($app) { + return new MessageManager($app); + }; + $app->mount("/", $this->controller_instance); } } diff --git a/src/MessageManager.php b/src/MessageManager.php new file mode 100644 index 0000000..bb0011a --- /dev/null +++ b/src/MessageManager.php @@ -0,0 +1,55 @@ +app = $app; + } + + public function getConversationMessages($conversation_id) + { + $response = $this->fireRequest("GET", "/conversations/{$conversation_id}/messages"); + + return $response; + } + + private function fireRequest($method, $uri, $body = []) + { + $method = strtoupper($method); + + if (false === in_array($method, ["GET", "POST", "PUT", "DELETE", "OPTIONS"])) { + return $this->app->abort(405, "ConversationProxy can not fire request of method : {$method}"); + } + + $domain = getenv("TRUSTED_DOMAIN"); + $jar = CookieJar::fromArray(["authenticator" => $this->app["cookies.authenticator"]], $domain); + + try { + $response = $this->app["conversation_proxy"]->request($method, $uri, [ + "cookies" => $jar, + "json" => $body + ]); + return json_decode($response->getBody(), true); + } catch (\GuzzleHttp\Exception\RequestException $client_error) { + return $this->app->abort( + $client_error->getResponse()->getStatusCode(), + $client_error->getResponse()->getReasonPhrase() + ); + } + } +}