vendor/eightpoints/guzzle-bundle/src/Middleware/ProfileMiddleware.php line 42

Open in your IDE?
  1. <?php
  2. namespace EightPoints\Bundle\GuzzleBundle\Middleware;
  3. use Symfony\Component\Stopwatch\Stopwatch;
  4. /**
  5.  * This Middleware is used to render request time slot on "Performance" tab in "Symfony Profiler".
  6.  */
  7. class ProfileMiddleware
  8. {
  9.     /**
  10.      * @var \Symfony\Component\Stopwatch\Stopwatch
  11.      */
  12.     private $stopwatch;
  13.     /**
  14.      * @param \Symfony\Component\Stopwatch\Stopwatch $stopwatch
  15.      */
  16.     public function __construct(Stopwatch $stopwatch)
  17.     {
  18.         $this->stopwatch $stopwatch;
  19.     }
  20.     /**
  21.      * Profiling each Request
  22.      *
  23.      * @return \Closure
  24.      */
  25.     public function profile() : \Closure
  26.     {
  27.         $stopwatch $this->stopwatch;
  28.         return function (callable $handler) use ($stopwatch) {
  29.             return function ($request, array $options) use ($handler$stopwatch) {
  30.                 $event $stopwatch->start(
  31.                     sprintf('%s %s'$request->getMethod(), $request->getUri()),
  32.                     'eight_points_guzzle'
  33.                 );
  34.                 return $handler($request$options)->then(
  35.                     function ($response) use ($event) {
  36.                         $event->stop();
  37.                         return $response;
  38.                     },
  39.                     function ($reason) use ($event) {
  40.                         $event->stop();
  41.                         return \GuzzleHttp\Promise\Create::rejectionFor($reason);
  42.                     }
  43.                 );
  44.             };
  45.         };
  46.     }
  47. }