Skip to content

Commit

Permalink
Merge pull request #17 from M6Web/feature/client-name-in-event
Browse files Browse the repository at this point in the history
Add client name in request event
  • Loading branch information
t-geindre committed Sep 16, 2015
2 parents 732fa6e + 50edd5c commit 0cf8687
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/M6WebGuzzleHttpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function loadClient(ContainerBuilder $container, $clientId, array $con
$handlerStackReference = new Reference('m6web_guzzlehttp.guzzle.handlerstack.'.$clientId);

$middlewareEventDispatcherDefinition = new Definition('%m6web_guzzlehttp.middleware.eventdispatcher.class%');
$middlewareEventDispatcherDefinition->setArguments([new Reference('event_dispatcher')]);
$middlewareEventDispatcherDefinition->setArguments([new Reference('event_dispatcher'), $clientId]);
$middlewareEventDispatcherDefinition->addMethodCall('push', [$handlerStackReference]);

// we must assign middleware for build process
Expand Down
29 changes: 29 additions & 0 deletions src/EventDispatcher/GuzzleHttpEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class GuzzleHttpEvent extends Event
*/
protected $response;

/**
* @var string
*/
protected $clientId;

/**
* Set request
*
Expand Down Expand Up @@ -134,4 +139,28 @@ public function getTiming()
{
return $this->getExecutionTime() * 1000;
}

/**
* Get client ID
*
* @return string
*/
public function getClientId()
{
return $this->clientId;
}

/**
* Set client ID
*
* @param string $clientId
*
* @return GuzzleHttpEvent
*/
public function setClientId($clientId)
{
$this->clientId = $clientId;

return $this;
}
}
10 changes: 9 additions & 1 deletion src/Middleware/EventDispatcherMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ class EventDispatcherMiddleware implements MiddlewareInterface
*/
protected $events;

/**
* @var string
*/
protected $clientId;

/**
* Constructor
*
* @param EventDispatcherInterface $eventDispatcher
* @param string $clientId
*/
public function __construct(EventDispatcherInterface $eventDispatcher)
public function __construct(EventDispatcherInterface $eventDispatcher, $clientId)
{

$this->eventDispatcher = $eventDispatcher;
$this->events = [];
$this->clientId = $clientId;
}

/**
Expand Down Expand Up @@ -96,6 +103,7 @@ protected function initEvent(Request $request)
$event = new GuzzleHttpEvent();
$event->setExecutionStart();
$event->setRequest($request);
$event->setClientId($this->clientId);

$this->events[$this->getEventKey($request)] = $event;
}
Expand Down

0 comments on commit 0cf8687

Please sign in to comment.