vendor/doctrine/mongodb-odm-bundle/APM/StopwatchCommandLogger.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\MongoDBBundle\APM;
  4. use Doctrine\ODM\MongoDB\APM\CommandLoggerInterface;
  5. use MongoDB\Driver\Monitoring\CommandFailedEvent;
  6. use MongoDB\Driver\Monitoring\CommandStartedEvent;
  7. use MongoDB\Driver\Monitoring\CommandSucceededEvent;
  8. use Symfony\Component\Stopwatch\Stopwatch;
  9. use function MongoDB\Driver\Monitoring\addSubscriber;
  10. use function MongoDB\Driver\Monitoring\removeSubscriber;
  11. use function sprintf;
  12. final class StopwatchCommandLogger implements CommandLoggerInterface
  13. {
  14.     /** @var bool */
  15.     private $registered false;
  16.     /** @var Stopwatch|null */
  17.     private $stopwatch;
  18.     public function __construct(?Stopwatch $stopwatch)
  19.     {
  20.         $this->stopwatch $stopwatch;
  21.     }
  22.     public function register(): void
  23.     {
  24.         if ($this->stopwatch === null || $this->registered) {
  25.             return;
  26.         }
  27.         $this->registered true;
  28.         addSubscriber($this);
  29.     }
  30.     public function unregister(): void
  31.     {
  32.         if (! $this->registered) {
  33.             return;
  34.         }
  35.         removeSubscriber($this);
  36.         $this->registered false;
  37.     }
  38.     public function commandStarted(CommandStartedEvent $event)
  39.     {
  40.         if (! $this->stopwatch) {
  41.             return;
  42.         }
  43.         $this->stopwatch->start(sprintf('mongodb_%s'$event->getRequestId()), 'doctrine_mongodb');
  44.     }
  45.     public function commandSucceeded(CommandSucceededEvent $event)
  46.     {
  47.         if (! $this->stopwatch) {
  48.             return;
  49.         }
  50.         $this->stopwatch->stop(sprintf('mongodb_%s'$event->getRequestId()));
  51.     }
  52.     public function commandFailed(CommandFailedEvent $event)
  53.     {
  54.         if (! $this->stopwatch) {
  55.             return;
  56.         }
  57.         $this->stopwatch->stop(sprintf('mongodb_%s'$event->getRequestId()));
  58.     }
  59. }