diff --git a/src/DataCollector/GuzzleHttpDataCollector.php b/src/DataCollector/GuzzleHttpDataCollector.php index 92fd316..2b9562f 100644 --- a/src/DataCollector/GuzzleHttpDataCollector.php +++ b/src/DataCollector/GuzzleHttpDataCollector.php @@ -4,7 +4,7 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollector; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; -use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleHttpEvent; +use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\AbstractGuzzleHttpEvent; /** * Collect information about guzzlehttp client @@ -54,9 +54,9 @@ public function getName() /** * Collect data for GuzzleHttp * - * @param GuzzleHttpEvent $event + * @param AbstractGuzzleHttpEvent $event */ - public function onGuzzleHttpCommand(GuzzleHttpEvent $event) + public function onGuzzleHttpCommand(AbstractGuzzleHttpEvent $event) { $request = $event->getRequest(); $response = $event->getResponse(); diff --git a/src/EventDispatcher/AbstractGuzzleCacheEvent.php b/src/EventDispatcher/AbstractGuzzleCacheEvent.php new file mode 100644 index 0000000..69d43e3 --- /dev/null +++ b/src/EventDispatcher/AbstractGuzzleCacheEvent.php @@ -0,0 +1,73 @@ +request = $request; + } + + /** + * Get Request + * + * @return RequestInterface + */ + public function getRequest(): RequestInterface + { + return $this->request; + } + + /** + * Set Exception + * + * @param \Exception $e + */ + public function setException(\Exception $e) + { + $this->exception = $e; + } + + /** + * Get Exception if isset + * + * @return \Exception|null + */ + public function getException() : ?\Exception + { + return $this->exception; + } + + /** + * Get domain from Request, with graphite compatibility (remove point) + * + * @return string + */ + public function getDomain(): string + { + return str_replace('.', '_', $this->request->getUri()->getHost()); + } +} diff --git a/src/EventDispatcher/AbstractGuzzleHttpEvent.php b/src/EventDispatcher/AbstractGuzzleHttpEvent.php new file mode 100644 index 0000000..3aa878c --- /dev/null +++ b/src/EventDispatcher/AbstractGuzzleHttpEvent.php @@ -0,0 +1,182 @@ +request = $request; + + return $this; + } + + /** + * Return request + * + * @return RequestInterface + */ + public function getRequest() : RequestInterface + { + return $this->request; + } + + /** + * Set Response + * + * @param ResponseInterface $response + * + * @return static + */ + public function setResponse(ResponseInterface $response) + { + $this->response = $response; + + return $this; + } + + /** + * Return response + * + * @return ResponseInterface + */ + public function getResponse() : ResponseInterface + { + return $this->response; + } + + /** + * Set reason + * + * @param \Exception $reason + * + * @return static + */ + public function setReason(\Exception $reason) + { + $this->reason = $reason; + + return $this; + } + + /** + * Return reason + * + * @return \Exception|null + */ + public function getReason() : ?\Exception + { + return $this->reason; + } + + /** + * @return float + */ + public function getExecutionStart() : float + { + return $this->executionStart; + } + + /** + * Set execution start of a request + * + * @return static + */ + public function setExecutionStart() + { + $this->executionStart = microtime(true); + + return $this; + } + + /** + * Stop the execution of a request + * and set the request execution time + * + * @return static + */ + public function setExecutionStop() + { + $this->executionTime = microtime(true) - $this->executionStart; + + return $this; + } + + /** + * @return float + */ + public function getExecutionTime() : float + { + return $this->executionTime; + } + + /** + * Return execution time in milliseconds + * + * @return float + */ + public function getTiming() : float + { + return $this->getExecutionTime() * 1000; + } + + /** + * Get client ID + * + * @return string + */ + public function getClientId() : string + { + return $this->clientId; + } + + /** + * Set client ID + * + * @param string $clientId + * + * @return static + */ + public function setClientId($clientId) + { + $this->clientId = $clientId; + + return $this; + } +} diff --git a/src/EventDispatcher/GuzzleCacheErrorEvent.php b/src/EventDispatcher/GuzzleCacheErrorEvent.php new file mode 100644 index 0000000..05a9da3 --- /dev/null +++ b/src/EventDispatcher/GuzzleCacheErrorEvent.php @@ -0,0 +1,12 @@ +request = $request; - } - - /** - * Get Request - * - * @return RequestInterface - */ - public function getRequest(): RequestInterface - { - return $this->request; - } - - /** - * Set Exception - * - * @param \Exception $e - */ - public function setException(\Exception $e) - { - $this->exception = $e; - } - - /** - * Get Exception if isset - * - * @return mixed - */ - public function getException() - { - return $this->exception; - } - - /** - * Get domain from Request, with graphite compatibility (remove point) - * - * @return string - */ - public function getDomain(): string - { - return str_replace('.', '_', $this->request->getUri()->getHost()); - } } diff --git a/src/EventDispatcher/GuzzleHttpErrorEvent.php b/src/EventDispatcher/GuzzleHttpErrorEvent.php new file mode 100644 index 0000000..36074fe --- /dev/null +++ b/src/EventDispatcher/GuzzleHttpErrorEvent.php @@ -0,0 +1,12 @@ +request = $request; - - return $this; - } - - /** - * Return request - * - * @return Request - */ - public function getRequest() - { - return $this->request; - } - - /** - * Set Response - * - * @param Response $response - * - * @return $this - */ - public function setResponse(ResponseInterface $response) - { - $this->response = $response; - - return $this; - } - - /** - * Return response - * - * @return Response - */ - public function getResponse() - { - return $this->response; - } - - /** - * Set reason - * - * @param mixed $reason - * - * @return $this - */ - public function setReason($reason) - { - $this->reason = $reason; - - return $this; - } - - /** - * Return reason - * - * @return mixed - */ - public function getReason() - { - return $this->reason; - } - - /** - * @return float - */ - public function getExecutionStart() - { - return $this->executionStart; - } - - /** - * Set execution start of a request - * - * @return GuzzleHttpEvent - */ - public function setExecutionStart() - { - $this->executionStart = microtime(true); - - return $this; - } - - /** - * Stop the execution of a request - * and set the request execution time - * - * @return GuzzleHttpEvent - */ - public function setExecutionStop() - { - $this->executionTime = microtime(true) - $this->executionStart; - - return $this; - } - - /** - * @return float - */ - public function getExecutionTime() - { - return $this->executionTime; - } - - /** - * Return execution time in milliseconds - * - * @return float - */ - 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; - } } diff --git a/src/Handler/CacheTrait.php b/src/Handler/CacheTrait.php index ee4e880..b071a43 100644 --- a/src/Handler/CacheTrait.php +++ b/src/Handler/CacheTrait.php @@ -3,7 +3,7 @@ use M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface; use GuzzleHttp\Psr7\Response; -use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleCacheEvent; +use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleCacheErrorEvent; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use GuzzleHttp\Promise\FulfilledPromise; @@ -229,9 +229,9 @@ public function __invoke(RequestInterface $request, array $options) } // Send event, when cache error is ignored. - $ignoreCacheEvent = new GuzzleCacheEvent($request); + $ignoreCacheEvent = new GuzzleCacheErrorEvent($request); $ignoreCacheEvent->setException($e); - $this->getEventDispatcher()->dispatch(GuzzleCacheEvent::NAME_ERROR, $ignoreCacheEvent); + $this->getEventDispatcher()->dispatch(GuzzleCacheErrorEvent::NAME_ERROR, $ignoreCacheEvent); } // no response in cache so we ask parent for response diff --git a/src/Middleware/EventDispatcherMiddleware.php b/src/Middleware/EventDispatcherMiddleware.php index 76c6314..2c843fc 100644 --- a/src/Middleware/EventDispatcherMiddleware.php +++ b/src/Middleware/EventDispatcherMiddleware.php @@ -5,10 +5,12 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; +use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleHttpErrorEvent; +use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleHttpEvent; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleHttpEvent; +use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\AbstractGuzzleHttpEvent; /** * Handler for event dispatching @@ -54,12 +56,6 @@ public function __construct(EventDispatcherInterface $eventDispatcher, $clientId */ public function push(HandlerStack $stack) { - $stack->push(Middleware::mapRequest(function (RequestInterface $request) { - $this->initEvent($request); - - return $request; - }), 'eventDispatcher_initEvent'); - $stack->push(function (callable $handler) { return function ( RequestInterface $request, @@ -97,21 +93,6 @@ protected function getEventKey(RequestInterface $request) return spl_object_hash($request); } - /** - * Initialize event - * - * @param RequestInterface $request - */ - protected function initEvent(RequestInterface $request) - { - $event = new GuzzleHttpEvent(); - $event->setExecutionStart(); - $event->setRequest($request); - $event->setClientId($this->clientId); - - $this->events[$this->getEventKey($request)] = $event; - } - /** * Dispatch event * @@ -120,11 +101,10 @@ protected function initEvent(RequestInterface $request) */ protected function sendEvent(RequestInterface $request, ResponseInterface $response) { - $key = $this->getEventKey($request); - $event = $this->events[$key]; - - unset($this->events[$key]); - + $event = new GuzzleHttpEvent(); + $event->setExecutionStart(); + $event->setRequest($request); + $event->setClientId($this->clientId); $event->setExecutionStop(); $event->setResponse($response); $this->eventDispatcher->dispatch(GuzzleHttpEvent::EVENT_NAME, $event); @@ -138,13 +118,12 @@ protected function sendEvent(RequestInterface $request, ResponseInterface $respo */ protected function sendErrorEvent(RequestInterface $request, $reason) { - $key = $this->getEventKey($request); - $event = $this->events[$key]; - - unset($this->events[$key]); - + $event = new GuzzleHttpEvent(); + $event->setExecutionStart(); + $event->setRequest($request); + $event->setClientId($this->clientId); $event->setExecutionStop(); $event->setReason($reason); - $this->eventDispatcher->dispatch(GuzzleHttpEvent::EVENT_ERROR_NAME, $event); + $this->eventDispatcher->dispatch(GuzzleHttpErrorEvent::EVENT_ERROR_NAME, $event); } } diff --git a/tests/Units/Middleware/EventDispatcherMiddleware.php b/tests/Units/Middleware/EventDispatcherMiddleware.php index 2d29332..00ff1a8 100644 --- a/tests/Units/Middleware/EventDispatcherMiddleware.php +++ b/tests/Units/Middleware/EventDispatcherMiddleware.php @@ -2,8 +2,10 @@ namespace M6Web\Bundle\GuzzleHttpBundle\tests\Units\Middleware; use atoum\test; -use M6Web\Bundle\GuzzleHttpBundle\Middleware\EventDispatcherMiddleware as Base; +use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleHttpErrorEvent; use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleHttpEvent; +use M6Web\Bundle\GuzzleHttpBundle\Middleware\EventDispatcherMiddleware as Base; +use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\AbstractGuzzleHttpEvent; /** * Class EventDispatcherMiddleware test @@ -20,14 +22,9 @@ public function testPush() }; // Mock HandlerStack - $initCallable = null; $eventCallable = null; $handlerStackMock = new \mock\GuzzleHttp\HandlerStack(); - $handlerStackMock->getMockController()->push = function($callable, $str) use (&$initCallable, &$eventCallable) { - if ($str == "eventDispatcher_initEvent") { - $initCallable = $callable; - } - + $handlerStackMock->getMockController()->push = function($callable, $str) use (&$eventCallable) { if ($str == "eventDispatcher_dispatch") { $eventCallable = $callable; } @@ -46,11 +43,6 @@ public function testPush() $errorCallable = $error; }; - // Handler for init of event - $handlerInit = function($request) { - return $request; - }; - // Handler for end of event $handlerEvent = function() use( $promiseMock) { return $promiseMock; @@ -64,12 +56,7 @@ public function testPush() ->isEqualTo($handlerStackMock) ->mock($handlerStackMock) ->call('push') - ->twice() - - ->object($callableHandler = $initCallable($handlerInit)) - ->isCallable() - ->object($callableHandler($requestMock, [])) - ->isEqualTo($requestMock) + ->once() ->object($callableHandler = $eventCallable($handlerEvent)) ->isCallable() @@ -88,18 +75,13 @@ public function testPush() ->once() ->withArguments(GuzzleHttpEvent::EVENT_NAME, $eventSend) ->once() - ->withArguments(GuzzleHttpEvent::EVENT_ERROR_NAME) + ->withArguments(GuzzleHttpErrorEvent::EVENT_ERROR_NAME) ->never() // 2nd event : error ->object($eventMid->push($handlerStackMock)) ->isEqualTo($handlerStackMock) - ->object($callableHandler = $initCallable($handlerInit)) - ->isCallable() - ->object($callableHandler($requestMock, [])) - ->isEqualTo($requestMock) - ->object($callableHandler = $eventCallable($handlerEvent)) ->isCallable() ->variable($callableHandler($requestMock, [])) @@ -118,7 +100,7 @@ function() use ($errorCallable) { ->hasMessage('connexion error') ->mock($dispatcherMock) ->call('dispatch') - ->withArguments(GuzzleHttpEvent::EVENT_ERROR_NAME, $eventSend) + ->withArguments(GuzzleHttpErrorEvent::EVENT_ERROR_NAME, $eventSend) ->once() ; }