Skip to content

Commit

Permalink
[FEATURE#56754] gestion des messages dans le proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Leosten committed Aug 30, 2017
1 parent 3d4b0d9 commit 970d451
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 4 additions & 0 deletions src/ConversationProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
55 changes: 55 additions & 0 deletions src/MessageManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace ETNA\Silex\Provider\ConversationProxy;

use Guzzle\Http\Message\Request as GuzzleRequest;

use GuzzleHttp\Cookie\CookieJar;
use Silex\Application;

use Symfony\Component\HttpFoundation\Request;

class MessageManager
{
private $app;

public function __construct(Application $app = null)
{
if (null === $app) {
throw new \Exception("MessageManager requires $app to be set");
}
$this->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()
);
}
}
}

0 comments on commit 970d451

Please sign in to comment.